From aea0fb2e4b14a16dbd86328d869f3307b641035d Mon Sep 17 00:00:00 2001 From: jiro4989 Date: Tue, 7 Sep 2021 08:40:55 +0900 Subject: [PATCH 1/2] :+1: split files and add tests --- cli.go | 62 +++++++++++++++++++++++++++++++++++++++++++++++ cli_test.go | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++ log.go | 10 ++++++++ main.go | 48 ++++-------------------------------- 4 files changed, 147 insertions(+), 43 deletions(-) create mode 100644 cli.go create mode 100644 cli_test.go create mode 100644 log.go diff --git a/cli.go b/cli.go new file mode 100644 index 0000000..6a6b5fd --- /dev/null +++ b/cli.go @@ -0,0 +1,62 @@ +package main + +import ( + "flag" + "fmt" + "os" +) + +type CmdArgs struct { + Level string + Args []string +} + +func ParseArgs() (*CmdArgs, error) { + opts := CmdArgs{} + + flag.Usage = flagHelpMessage + flag.StringVar(&opts.Level, "level", "normal", "slot difficulty. [easy|normal|hard]") + flag.Parse() + opts.Args = flag.Args() + + if err := opts.Validate(); err != nil { + return nil, err + } + + return &opts, nil +} + +func flagHelpMessage() { + cmd := os.Args[0] + fmt.Fprintln(os.Stderr, fmt.Sprintf("%s changes file permissions with a slot", cmd)) + fmt.Fprintln(os.Stderr, "") + fmt.Fprintln(os.Stderr, "Usage:") + fmt.Fprintln(os.Stderr, fmt.Sprintf(" %s [OPTIONS] [files...]", cmd)) + fmt.Fprintln(os.Stderr, "") + fmt.Fprintln(os.Stderr, "Examples:") + fmt.Fprintln(os.Stderr, fmt.Sprintf(" %s sample.txt", cmd)) + fmt.Fprintln(os.Stderr, "") + fmt.Fprintln(os.Stderr, "Options:") + + flag.PrintDefaults() +} + +func (c *CmdArgs) Validate() error { + if len(c.Args) < 1 { + return fmt.Errorf("Must need files") + } + + _, ok := slotIntervalTime[c.Level] + if !ok { + return fmt.Errorf("-level must be 'eash' or 'normal' or 'hard'.") + } + + for _, file := range c.Args { + _, err := os.Stat(file) + if os.IsNotExist(err) { + return fmt.Errorf("%s file doesn't exist.", file) + } + } + + return nil +} diff --git a/cli_test.go b/cli_test.go new file mode 100644 index 0000000..948bfaf --- /dev/null +++ b/cli_test.go @@ -0,0 +1,70 @@ +package main + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestCmdArgsValidate(t *testing.T) { + tests := []struct { + desc string + args CmdArgs + wantErr bool + }{ + { + desc: "ok", + args: CmdArgs{ + Level: "normal", + Args: []string{"LICENSE"}, + }, + wantErr: false, + }, + { + desc: "ok: level is hard", + args: CmdArgs{ + Level: "hard", + Args: []string{"LICENSE"}, + }, + wantErr: false, + }, + { + desc: "ng: arguments count is 0", + args: CmdArgs{ + Level: "normal", + Args: []string{}, + }, + wantErr: true, + }, + { + desc: "ng: illegal level string", + args: CmdArgs{ + Level: "aiueo", + Args: []string{"LICENSE"}, + }, + wantErr: true, + }, + { + desc: "ng: file doesn't exist", + args: CmdArgs{ + Level: "aiueo", + Args: []string{"LICENSE", "sushi.txt"}, + }, + wantErr: true, + }, + } + + for _, tt := range tests { + t.Run(tt.desc, func(t *testing.T) { + assert := assert.New(t) + + err := tt.args.Validate() + if tt.wantErr { + assert.Error(err) + return + } + + assert.NoError(err) + }) + } +} diff --git a/log.go b/log.go new file mode 100644 index 0000000..a048b6e --- /dev/null +++ b/log.go @@ -0,0 +1,10 @@ +package main + +import ( + "fmt" + "os" +) + +func Err(err error) { + fmt.Fprintln(os.Stderr, "[ERR] "+err.Error()) +} diff --git a/main.go b/main.go index 0a26de4..6d2d376 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,6 @@ package main import ( - "flag" "fmt" "os" "strconv" @@ -10,10 +9,6 @@ import ( termbox "github.com/nsf/termbox-go" ) -type CmdOpts struct { - Level string -} - var ( slotIntervalTime = map[string]int{ "easy": 200, @@ -23,31 +18,13 @@ var ( ) func main() { - opts := CmdOpts{} - flag.Usage = flagHelpMessage - flag.StringVar(&opts.Level, "level", "normal", "slot difficulty. [easy|normal|hard]") - flag.Parse() - args := flag.Args() - - if len(args) < 1 { - fmt.Fprintln(os.Stderr, "[ERR] Must need files") + args, err := ParseArgs() + if err != nil { + Err(err) os.Exit(1) } - interval, ok := slotIntervalTime[opts.Level] - if !ok { - fmt.Fprintln(os.Stderr, "[ERR] -level must be 'eash' or 'normal' or 'hard'.") - os.Exit(2) - } - - for _, file := range args { - _, err := os.Stat(file) - if os.IsNotExist(err) { - fmt.Fprintln(os.Stderr, fmt.Sprintf("[ERR] %s file doesn't exist.", file)) - os.Exit(3) - } - } - + interval := slotIntervalTime[args.Level] slot := NewSlot(0, interval) if err := termbox.Init(); err != nil { @@ -61,22 +38,7 @@ func main() { waitKeyInput(slot) termbox.Close() - changeMode(slot, args) -} - -func flagHelpMessage() { - cmd := os.Args[0] - fmt.Fprintln(os.Stderr, fmt.Sprintf("%s changes file permissions with a slot", cmd)) - fmt.Fprintln(os.Stderr, "") - fmt.Fprintln(os.Stderr, "Usage:") - fmt.Fprintln(os.Stderr, fmt.Sprintf(" %s [OPTIONS] [files...]", cmd)) - fmt.Fprintln(os.Stderr, "") - fmt.Fprintln(os.Stderr, "Examples:") - fmt.Fprintln(os.Stderr, fmt.Sprintf(" %s sample.txt", cmd)) - fmt.Fprintln(os.Stderr, "") - fmt.Fprintln(os.Stderr, "Options:") - - flag.PrintDefaults() + changeMode(slot, args.Args) } func clock(s *Slot) { From 0bab012bc266368666ca275d7389814a7fd58ed1 Mon Sep 17 00:00:00 2001 From: jiro4989 Date: Tue, 7 Sep 2021 08:42:00 +0900 Subject: [PATCH 2/2] :+1: change function name --- main.go | 2 +- view.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index 6d2d376..1aecfef 100644 --- a/main.go +++ b/main.go @@ -44,7 +44,7 @@ func main() { func clock(s *Slot) { for !s.IsFinished() { s.Switch() - drawSlot(s) + DrawSlot(s) time.Sleep(time.Duration(s.IntervalTime()) * time.Millisecond) } } diff --git a/view.go b/view.go index cfaf4ca..2f11983 100644 --- a/view.go +++ b/view.go @@ -6,7 +6,7 @@ import ( termbox "github.com/nsf/termbox-go" ) -func drawSlot(s *Slot) { +func DrawSlot(s *Slot) { termbox.Clear(termbox.ColorDefault, termbox.ColorDefault) idx := s.CurrentSlotIndex()