-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from jiro4989/chore/split_file
👍 Split files and add test codes
- Loading branch information
Showing
5 changed files
with
149 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters