Skip to content

Commit

Permalink
Merge pull request #5 from jiro4989/chore/split_file
Browse files Browse the repository at this point in the history
👍 Split files and add test codes
  • Loading branch information
jiro4989 authored Sep 6, 2021
2 parents 7a59089 + 0bab012 commit 413eb59
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 45 deletions.
62 changes: 62 additions & 0 deletions cli.go
Original file line number Diff line number Diff line change
@@ -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
}
70 changes: 70 additions & 0 deletions cli_test.go
Original file line number Diff line number Diff line change
@@ -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)
})
}
}
10 changes: 10 additions & 0 deletions log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package main

import (
"fmt"
"os"
)

func Err(err error) {
fmt.Fprintln(os.Stderr, "[ERR] "+err.Error())
}
50 changes: 6 additions & 44 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"flag"
"fmt"
"os"
"strconv"
Expand All @@ -10,10 +9,6 @@ import (
termbox "github.com/nsf/termbox-go"
)

type CmdOpts struct {
Level string
}

var (
slotIntervalTime = map[string]int{
"easy": 200,
Expand All @@ -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 {
Expand All @@ -61,28 +38,13 @@ 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) {
for !s.IsFinished() {
s.Switch()
drawSlot(s)
DrawSlot(s)
time.Sleep(time.Duration(s.IntervalTime()) * time.Millisecond)
}
}
Expand Down
2 changes: 1 addition & 1 deletion view.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 413eb59

Please sign in to comment.