From 997da0935c5c14f634f17a2ed08d547085b62b64 Mon Sep 17 00:00:00 2001 From: budougumi0617 Date: Sun, 11 Oct 2020 02:07:04 +0900 Subject: [PATCH 1/8] :sparkles: main: call help if no argument --- cmd/leetgode/main.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/cmd/leetgode/main.go b/cmd/leetgode/main.go index 3d5a53c..908fc56 100644 --- a/cmd/leetgode/main.go +++ b/cmd/leetgode/main.go @@ -20,7 +20,10 @@ func main() { flag.Parse() sub := flag.Arg(0) if len(sub) == 0 { - fmt.Printf("TODO: show help\n") + cmd := &leetgode.HelpCmd{} + if err := cmd.Run(context.Background(), []string{}); err !=nil{ + fmt.Printf("help comamnd is faield: %v", err) + } return } // TODO: set os.Stderr if set debug mode From df1af564443778a63ccdbe38a0a7ae4eb97d2df5 Mon Sep 17 00:00:00 2001 From: budougumi0617 Date: Mon, 12 Oct 2020 02:12:01 +0900 Subject: [PATCH 2/8] :recycle: change stream by args --- cmd/leetgode/main.go | 55 +++++++++++++++++++++++++++++--------------- 1 file changed, 36 insertions(+), 19 deletions(-) diff --git a/cmd/leetgode/main.go b/cmd/leetgode/main.go index 908fc56..5e298f6 100644 --- a/cmd/leetgode/main.go +++ b/cmd/leetgode/main.go @@ -4,6 +4,7 @@ import ( "context" "flag" "fmt" + "io" "io/ioutil" "log" "os" @@ -12,35 +13,51 @@ import ( ) func main() { - flag.Usage = func() { - if err := leetgode.ShowUsage(os.Stdout); err != nil { - panic(err) + os.Exit(run(os.Stdout, os.Stderr, os.Args)) +} + +func run(stdout, stderr io.Writer, args []string) int { + if len(args) < 2 { + cmd := &leetgode.HelpCmd{} + if err := cmd.Run(context.Background(), []string{}); err != nil { + fmt.Fprintf(stderr, "help comamnd is faield: %v", err) } + return 1 } - flag.Parse() - sub := flag.Arg(0) - if len(sub) == 0 { - cmd := &leetgode.HelpCmd{} - if err := cmd.Run(context.Background(), []string{}); err !=nil{ - fmt.Printf("help comamnd is faield: %v", err) + sub := args[1] + f := flag.NewFlagSet(sub, flag.ContinueOnError) + f.Usage = func() { + if err := leetgode.ShowUsage(stdout); err != nil { + fmt.Fprintf(stderr, "failed show useage: %v\n", err) } - return } - // TODO: set os.Stderr if set debug mode - log.SetOutput(ioutil.Discard) + var v bool + f.BoolVar(&v, "v", false, "show debug print") + if err := f.Parse(args[2:]); err == flag.ErrHelp { + return 1 + } else if err != nil { + fmt.Fprintf(stderr, "%s with invalid args: %v\n", sub, err) + return 1 + } + + log.SetOutput(stderr) + if !v { + log.SetOutput(ioutil.Discard) + } if cmd, ok := leetgode.CmdMap[leetgode.CmdName(sub)]; ok { - args := flag.Args()[1:] + args := f.Args() if len(args) != cmd.MaxArg() { - fmt.Printf("%s expects %d options, but %d options\n", cmd.Name(), cmd.MaxArg(), len(args)) - os.Exit(1) + fmt.Fprintf(stderr, "%s expects %d options, but %d options\n", cmd.Name(), cmd.MaxArg(), len(args)) + return 1 } if err := cmd.Run(context.Background(), args); err != nil { - log.Printf("main: err %v", err) - os.Exit(1) + fmt.Fprintf(stderr, "main: err %v", err) + return 1 } } else { - log.Printf("unknown command %q", sub) - os.Exit(1) + fmt.Fprintf(stderr, "unknown command %q", sub) + return 1 } + return 0 } From b206460739cd22e138d928e95800ac73dddc8852 Mon Sep 17 00:00:00 2001 From: budougumi0617 Date: Tue, 13 Oct 2020 14:10:37 +0900 Subject: [PATCH 3/8] :recycle: refactoring error print --- cmd/leetgode/main.go | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/cmd/leetgode/main.go b/cmd/leetgode/main.go index 5e298f6..e9d6334 100644 --- a/cmd/leetgode/main.go +++ b/cmd/leetgode/main.go @@ -13,31 +13,34 @@ import ( ) func main() { - os.Exit(run(os.Stdout, os.Stderr, os.Args)) + if err := run(os.Stdout, os.Stderr, os.Args); err != nil { + fmt.Fprintf(os.Stderr, "%v\n", err) + os.Exit(1) + } } -func run(stdout, stderr io.Writer, args []string) int { +func run(stdout, stderr io.Writer, args []string) error { if len(args) < 2 { cmd := &leetgode.HelpCmd{} if err := cmd.Run(context.Background(), []string{}); err != nil { - fmt.Fprintf(stderr, "help comamnd is faield: %v", err) + return fmt.Errorf("help command is failed: %w", err) } - return 1 + return nil } sub := args[1] + // implement into each sub command if use different options by each sub command f := flag.NewFlagSet(sub, flag.ContinueOnError) f.Usage = func() { if err := leetgode.ShowUsage(stdout); err != nil { - fmt.Fprintf(stderr, "failed show useage: %v\n", err) + fmt.Fprintf(stderr, "failed show useage: %w\n", err) } } var v bool f.BoolVar(&v, "v", false, "show debug print") if err := f.Parse(args[2:]); err == flag.ErrHelp { - return 1 + return err } else if err != nil { - fmt.Fprintf(stderr, "%s with invalid args: %v\n", sub, err) - return 1 + return fmt.Errorf("%q command with invalid args(%q): %w", sub, args[2:], err) } log.SetOutput(stderr) @@ -48,16 +51,13 @@ func run(stdout, stderr io.Writer, args []string) int { if cmd, ok := leetgode.CmdMap[leetgode.CmdName(sub)]; ok { args := f.Args() if len(args) != cmd.MaxArg() { - fmt.Fprintf(stderr, "%s expects %d options, but %d options\n", cmd.Name(), cmd.MaxArg(), len(args)) - return 1 + return fmt.Errorf("%q command expects %d options, but %d options\n", cmd.Name(), cmd.MaxArg(), len(args)) } if err := cmd.Run(context.Background(), args); err != nil { - fmt.Fprintf(stderr, "main: err %v", err) - return 1 + return fmt.Errorf("%q command failed: %w", sub, err) } } else { - fmt.Fprintf(stderr, "unknown command %q", sub) - return 1 + return fmt.Errorf("unknown command %q", sub) } - return 0 + return nil } From cba4d80b4b7528d278ef4eb7605c5c1f10eee3c7 Mon Sep 17 00:00:00 2001 From: budougumi0617 Date: Tue, 13 Oct 2020 14:33:44 +0900 Subject: [PATCH 4/8] :sparkles: pick: make a fair of copy of output --- pick.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pick.go b/pick.go index c99a13e..c149ef2 100644 --- a/pick.go +++ b/pick.go @@ -38,8 +38,8 @@ func (c *PickCmd) Run(ctx context.Context, args []string) error { return err } - // FIXME: pretty print - fmt.Printf("result: %#v\n", q) + // FIXME: pretty print for HTML + fmt.Fprintf(out, "%s: %s\n%s\n%s", q.QuestionID, q.Slug, q.Referer, q.Content) return nil } From e5b250ebf0114bfc98764e451a2b2f6c3db5e81e Mon Sep 17 00:00:00 2001 From: budougumi0617 Date: Tue, 13 Oct 2020 14:37:29 +0900 Subject: [PATCH 5/8] :recycle: cmd: change interface --- cmd/leetgode/main.go | 4 ++-- core.go | 3 ++- exec.go | 3 ++- generate.go | 3 ++- help.go | 5 ++--- list.go | 3 ++- pick.go | 3 ++- test.go | 3 ++- 8 files changed, 16 insertions(+), 11 deletions(-) diff --git a/cmd/leetgode/main.go b/cmd/leetgode/main.go index e9d6334..05ee101 100644 --- a/cmd/leetgode/main.go +++ b/cmd/leetgode/main.go @@ -22,7 +22,7 @@ func main() { func run(stdout, stderr io.Writer, args []string) error { if len(args) < 2 { cmd := &leetgode.HelpCmd{} - if err := cmd.Run(context.Background(), []string{}); err != nil { + if err := cmd.Run(context.Background(), stdout, []string{}); err != nil { return fmt.Errorf("help command is failed: %w", err) } return nil @@ -53,7 +53,7 @@ func run(stdout, stderr io.Writer, args []string) error { if len(args) != cmd.MaxArg() { return fmt.Errorf("%q command expects %d options, but %d options\n", cmd.Name(), cmd.MaxArg(), len(args)) } - if err := cmd.Run(context.Background(), args); err != nil { + if err := cmd.Run(context.Background(), stdout, args); err != nil { return fmt.Errorf("%q command failed: %w", sub, err) } } else { diff --git a/core.go b/core.go index eae60d8..ee69b32 100644 --- a/core.go +++ b/core.go @@ -3,6 +3,7 @@ package leetgode import ( "context" "fmt" + "io" ) type CmdName string @@ -20,7 +21,7 @@ type Cmd interface { Name() string Usage() string MaxArg() int - Run(context.Context, []string) error + Run(context.Context, io.Writer, []string) error } var CmdMap = map[CmdName]Cmd{ diff --git a/exec.go b/exec.go index 2cd2578..cbed8eb 100644 --- a/exec.go +++ b/exec.go @@ -3,6 +3,7 @@ package leetgode import ( "context" "fmt" + "io" "io/ioutil" "os" "strconv" @@ -26,7 +27,7 @@ func (c *ExecCmd) Usage() string { } // TODO: refactoring exec and test. -func (c *ExecCmd) Run(ctx context.Context, args []string) error { +func (c *ExecCmd) Run(ctx context.Context, out io.Writer, args []string) error { id, err := strconv.Atoi(args[0]) if err != nil { return err diff --git a/generate.go b/generate.go index 127b6c5..ecdd842 100644 --- a/generate.go +++ b/generate.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "fmt" + "io" "io/ioutil" "strconv" "text/template" @@ -50,7 +51,7 @@ func (g *GenerateCmd) Usage() string { return "Generate the skeleton code with the test file by id" } -func (g *GenerateCmd) Run(ctx context.Context, args []string) error { +func (g *GenerateCmd) Run(ctx context.Context, out io.Writer, args []string) error { id, err := strconv.Atoi(args[0]) if err != nil { return err diff --git a/help.go b/help.go index 9d65052..08c900e 100644 --- a/help.go +++ b/help.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "io" - "os" "sort" "text/tabwriter" ) @@ -49,6 +48,6 @@ func ShowUsage(w io.Writer) error { return tw.Flush() } -func (c *HelpCmd) Run(ctx context.Context, args []string) error { - return ShowUsage(os.Stdout) +func (c *HelpCmd) Run(ctx context.Context, out io.Writer, args []string) error { + return ShowUsage(out) } diff --git a/list.go b/list.go index 846a328..f04486b 100644 --- a/list.go +++ b/list.go @@ -3,6 +3,7 @@ package leetgode import ( "context" "fmt" + "io" "os" "sort" "text/tabwriter" @@ -24,7 +25,7 @@ func (c *ListCmd) Usage() string { return "List problems" } -func (c *ListCmd) Run(ctx context.Context, _ []string) error { +func (c *ListCmd) Run(ctx context.Context, out io.Writer, _ []string) error { cli, err := NewLeetCode() if err != nil { return err diff --git a/pick.go b/pick.go index c149ef2..cfed951 100644 --- a/pick.go +++ b/pick.go @@ -3,6 +3,7 @@ package leetgode import ( "context" "fmt" + "io" "strconv" ) @@ -23,7 +24,7 @@ func (c *PickCmd) MaxArg() int { return 1 } -func (c *PickCmd) Run(ctx context.Context, args []string) error { +func (c *PickCmd) Run(ctx context.Context, out io.Writer, args []string) error { cli, err := NewLeetCode() if err != nil { return err diff --git a/test.go b/test.go index ea76a14..2ca7117 100644 --- a/test.go +++ b/test.go @@ -3,6 +3,7 @@ package leetgode import ( "context" "fmt" + "io" "io/ioutil" "os" "strconv" @@ -25,7 +26,7 @@ func (c *TestCmd) Usage() string { return "Test solution" } -func (c *TestCmd) Run(ctx context.Context, args []string) error { +func (c *TestCmd) Run(ctx context.Context, out io.Writer, args []string) error { id, err := strconv.Atoi(args[0]) if err != nil { return err From c45fa74861c49026ec282694c6de3c687f74b84f Mon Sep 17 00:00:00 2001 From: budougumi0617 Date: Tue, 13 Oct 2020 14:38:11 +0900 Subject: [PATCH 6/8] :recycle: exec: generate: test: format output --- exec.go | 6 +++--- generate.go | 7 ++++--- test.go | 6 +++--- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/exec.go b/exec.go index cbed8eb..0c10a7e 100644 --- a/exec.go +++ b/exec.go @@ -52,7 +52,7 @@ func (c *ExecCmd) Run(ctx context.Context, out io.Writer, args []string) error { if err != nil { return err } - fmt.Print("now sending") + fmt.Fprint(out, "now sending") for { res, err := cli.Check(ctx, q, tr) if err != nil { @@ -60,14 +60,14 @@ func (c *ExecCmd) Run(ctx context.Context, out io.Writer, args []string) error { } // FIXME: pretty print if res.State == "SUCCESS" { - fmt.Printf(` + fmt.Fprintf(out, ` executed id: %s problem title: %s result: %s `, q.QuestionID, q.Slug, res.StatusMsg) break } else { - fmt.Print(".") + fmt.Fprint(out, ".") } time.Sleep(1 * time.Second) } diff --git a/generate.go b/generate.go index ecdd842..43cdf4d 100644 --- a/generate.go +++ b/generate.go @@ -6,6 +6,7 @@ import ( "fmt" "io" "io/ioutil" + "log" "strconv" "text/template" @@ -74,7 +75,7 @@ func (g *GenerateCmd) Run(ctx context.Context, out io.Writer, args []string) err if c == nil { return fmt.Errorf("not found the code for Go") } - fmt.Printf("%s\n", fmt.Sprint(c.DefaultCode)) + log.Printf("%s\n", fmt.Sprint(c.DefaultCode)) input := &Format{ Referer: q.Referer, Content: q.Content, @@ -90,11 +91,11 @@ func (g *GenerateCmd) Run(ctx context.Context, out io.Writer, args []string) err if err != nil { panic(err) } - fmt.Printf("%s", buf.String()) + log.Printf("%s", buf.String()) // TODO: どうやってファイル保存とテストしやすさを分けようか? path := buildPath(q.QuestionID, q.Slug) - fmt.Printf("save at %q\n", path) + fmt.Fprintf(out, "save at %q\n", path) if err := ioutil.WriteFile(path, buf.Bytes(), 0644); err != nil { return err } diff --git a/test.go b/test.go index 2ca7117..8bc0b78 100644 --- a/test.go +++ b/test.go @@ -51,7 +51,7 @@ func (c *TestCmd) Run(ctx context.Context, out io.Writer, args []string) error { if err != nil { return err } - fmt.Print("now sending") + fmt.Fprint(out, "now sending") for { res, err := cli.Check(ctx, q, tr) if err != nil { @@ -59,14 +59,14 @@ func (c *TestCmd) Run(ctx context.Context, out io.Writer, args []string) error { } // FIXME: pretty print if res.State == "SUCCESS" { - fmt.Printf(` + fmt.Fprintf(out,` test id: %s problem title: %s result: %s `, q.QuestionID, q.Slug, res.StatusMsg) break } else { - fmt.Print(".") + fmt.Fprint(out, ".") } time.Sleep(1 * time.Second) } From eeb424b3eb4e63f54fd2481ec7bafccb7d04aa79 Mon Sep 17 00:00:00 2001 From: budougumi0617 Date: Tue, 13 Oct 2020 14:47:42 +0900 Subject: [PATCH 7/8] :green_heart: fix tests --- cmd/leetgode/main.go | 2 +- generate_test.go | 3 ++- list_test.go | 3 ++- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/cmd/leetgode/main.go b/cmd/leetgode/main.go index 05ee101..1b320ee 100644 --- a/cmd/leetgode/main.go +++ b/cmd/leetgode/main.go @@ -32,7 +32,7 @@ func run(stdout, stderr io.Writer, args []string) error { f := flag.NewFlagSet(sub, flag.ContinueOnError) f.Usage = func() { if err := leetgode.ShowUsage(stdout); err != nil { - fmt.Fprintf(stderr, "failed show useage: %w\n", err) + fmt.Fprintf(stderr, "failed show useage: %v\n", err) } } var v bool diff --git a/generate_test.go b/generate_test.go index e520040..1d4cb25 100644 --- a/generate_test.go +++ b/generate_test.go @@ -2,6 +2,7 @@ package leetgode import ( "context" + "io/ioutil" "testing" ) @@ -17,7 +18,7 @@ func TestGenerateCmd(t *testing.T) { tt := tt t.Run(tt.name, func(t *testing.T) { cmd := &GenerateCmd{} - if err := cmd.Run(context.TODO(), tt.args); err != nil { + if err := cmd.Run(context.TODO(), ioutil.Discard, tt.args); err != nil { t.Errorf("GenerateCmd() error = %v", err) } }) diff --git a/list_test.go b/list_test.go index 4eba5a6..37fb913 100644 --- a/list_test.go +++ b/list_test.go @@ -2,12 +2,13 @@ package leetgode import ( "context" + "io/ioutil" "testing" ) func TestListCmd(t *testing.T) { cmd := &ListCmd{} - if err := cmd.Run(context.TODO(), []string{}); err != nil { + if err := cmd.Run(context.TODO(), ioutil.Discard, []string{}); err != nil { t.Errorf("ListCmd() error = %v", err) } } From 78048b705e85d63c8ac6e198488dc31d19d50b03 Mon Sep 17 00:00:00 2001 From: budougumi0617 Date: Tue, 13 Oct 2020 14:53:13 +0900 Subject: [PATCH 8/8] :gear: actions: fix actions version --- .github/workflows/test.yml | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index bd6f7d7..263fc8a 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -4,7 +4,7 @@ jobs: test: strategy: matrix: - platform: [ubuntu-latest, macos-latest, windows-latest] + platform: [ubuntu-latest] runs-on: ${{ matrix.platform }} steps: - name: Install Go @@ -14,17 +14,8 @@ jobs: go-version: '^1.15' # The Go version to download (if necessary) and use. - name: Checkout code uses: actions/checkout@v2 - - name: Use Cache(on Windows) - if: runner.os == 'Windows' - uses: actions/cache@preview - with: - path: ~/go/pkg/mod - key: ${{ runner.os }}-go-${{ hashFiles('**\go.sum') }} - restore-keys: | - ${{ runner.os }}-go- - name: Use Cache - if: runner.os != 'Windows' - uses: actions/cache@preview + uses: actions/cache@v2 with: path: ~/go/pkg/mod key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}