Skip to content

Commit

Permalink
Merge pull request #10 from Connehito/working-hours
Browse files Browse the repository at this point in the history
Add checking safety hour
  • Loading branch information
itosho authored Apr 13, 2020
2 parents 7c7bdaa + b1d0d05 commit fa9ab8a
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 3 deletions.
47 changes: 45 additions & 2 deletions cli.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package main

import (
"bufio"
"flag"
"fmt"
"io"

"github.com/mitchellh/colorstring"
"io"
"os"
)

// CLI has stdout/stderr's writer and Gdp's interface.
Expand All @@ -27,6 +28,12 @@ const (
CommandPublish = "publish"
)

// Safety Hour.
const (
SafetyHourStart = 9
SafetyHourEnd = 19
)

// Run invokes depoloy and publish's process.
func (cli *CLI) Run(args []string) int {
var version bool
Expand Down Expand Up @@ -126,6 +133,14 @@ func (cli *CLI) Run(args []string) int {

// execution
if subCommand == CommandDeploy {
if !isSafetyHour() {
fmt.Fprintln(cli.outStream, "It's past the regular time. Is this a hot-fix release?")
fmt.Fprint(cli.outStream, "> ")
if !yesOrNo(cli) {
return ExitError
}
}

if err := cli.gdp.Deploy(tag); err != nil {
printError(cli.errStream, fmt.Sprintf("Deploy execution error: %s.", err.Error()))
return ExitError
Expand Down Expand Up @@ -173,3 +188,31 @@ func validate(cli *CLI, subCommand string, tag string) bool {

return true
}

func isSafetyHour() bool {
t := Now()
if t.Hour() > SafetyHourStart && t.Hour() < SafetyHourEnd {
return true
}

return false
}

func yesOrNo(cli *CLI) bool {
reader := bufio.NewReader(os.Stdin)
s, err := reader.ReadByte()
if err != nil {
return false
}

if s == []byte("Y")[0] || s == []byte("y")[0] {
fmt.Fprintln(cli.outStream, "OK. Take time.")
return true
} else if s == []byte("N")[0] || s == []byte("n")[0] {
printError(cli.errStream, fmt.Sprintf("Good choice."))
return false
}

printError(cli.errStream, fmt.Sprintf("Please enter y or n."))
return false
}
5 changes: 5 additions & 0 deletions cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"strings"
"testing"
"time"
)

// Tests for flag
Expand Down Expand Up @@ -183,6 +184,7 @@ func TestRun_Deploy(t *testing.T) {
errStream: err,
gdp: &FakeGdpDeploy{},
}
Set(time.Date(2020, 4, 1, 17, 00, 00, 0, time.Local))

args := strings.Split("gdp deploy -t v1.2.4", " ")
code := cli.Run(args)
Expand Down Expand Up @@ -223,6 +225,7 @@ func TestRun_DeployNoSpecifiedTag(t *testing.T) {
errStream: err,
gdp: &FakeGdpDeploy{},
}
Set(time.Date(2020, 4, 1, 17, 00, 00, 0, time.Local))

args := strings.Split("gdp deploy", " ")

Expand Down Expand Up @@ -258,6 +261,7 @@ func TestRun_DeployForce(t *testing.T) {
errStream: err,
gdp: &FakeGdpDeployForce{},
}
Set(time.Date(2020, 4, 1, 17, 00, 00, 0, time.Local))

args := strings.Split("gdp deploy -t v1.2.4 -f", " ")
code := cli.Run(args)
Expand Down Expand Up @@ -432,6 +436,7 @@ func TestRun_DeployErrorInDeploy(t *testing.T) {
errStream: err,
gdp: &FakeGdpDeployErrorInDeploy{},
}
Set(time.Date(2020, 4, 1, 17, 00, 00, 0, time.Local))

args := strings.Split("gdp deploy -t v1.2.4", " ")
code := cli.Run(args)
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

// Version is GDP's version.
const Version string = "v0.2.2"
const Version string = "v0.2.3"

// Usage is GDP's usage.
const Usage string = "usage: gdp deploy|publish [options]"
Expand Down
16 changes: 16 additions & 0 deletions time.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package main

import "time"

var fakeTime time.Time

func Now() time.Time {
if !fakeTime.IsZero() {
return fakeTime
}
return time.Now()
}

func Set(t time.Time) {
fakeTime = t
}

0 comments on commit fa9ab8a

Please sign in to comment.