From e293f1cadc2ba4c2a48a1b35bdf5fbd0701e2d91 Mon Sep 17 00:00:00 2001 From: Ryo Kitagawa Date: Sun, 13 Aug 2023 11:51:26 +0900 Subject: [PATCH 1/3] Fix diff command --- cmd/alp/cmd/diff.go | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/cmd/alp/cmd/diff.go b/cmd/alp/cmd/diff.go index 1932ce9..aea0eea 100644 --- a/cmd/alp/cmd/diff.go +++ b/cmd/alp/cmd/diff.go @@ -1,6 +1,7 @@ package cmd import ( + "fmt" "os" "github.com/spf13/cobra" @@ -8,14 +9,14 @@ import ( ) func NewDiffCmd(rootCmd *cobra.Command) *cobra.Command { - var diffCmd = &cobra.Command{ + diffCmd := &cobra.Command{ Use: "diff ", - Args: cobra.ExactArgs(2), + Args: cobra.RangeArgs(0, 2), Short: "Show the difference between the two profile results", Long: `Show the difference between the two profile results`, RunE: func(cmd *cobra.Command, args []string) error { sortOptions := stats.NewSortOptions() - opts, err := createOptions(rootCmd, sortOptions) + opts, err := createOptions(cmd, sortOptions) if err != nil { return err } @@ -24,11 +25,23 @@ func NewDiffCmd(rootCmd *cobra.Command) *cobra.Command { if err != nil { return err } + if from == "" { + if len(args) < 1 { + return fmt.Errorf("from is required") + } + from = args[0] + } to, err := cmd.PersistentFlags().GetString("to") if err != nil { return err } + if to == "" { + if len(args) < 2 { + return fmt.Errorf("to is required") + } + to = args[1] + } sts := stats.NewHTTPStats(true, false, false) @@ -85,8 +98,10 @@ func NewDiffCmd(rootCmd *cobra.Command) *cobra.Command { }, } - //app.Arg("from", "").Required().StringVar(&f.From) - //app.Arg("to", "").Required().StringVar(&f.To) + // app.Arg("from", "").Required().StringVar(&f.From) + // app.Arg("to", "").Required().StringVar(&f.To) + + defineOptions(diffCmd) diffCmd.PersistentFlags().StringP("from", "", "", "The comparison source file") diffCmd.PersistentFlags().StringP("to", "", "", "The comparison target file") From 0e4d32aa1cc80052cb55d4ed2982747bf1f8a76d Mon Sep 17 00:00:00 2001 From: tkuchiki Date: Mon, 14 Aug 2023 23:42:48 +0900 Subject: [PATCH 2/3] [alp diff] Remove unused code Co-authored-by: Ryo Kitagawa --- cmd/alp/cmd/diff.go | 32 ++++++++------------------------ 1 file changed, 8 insertions(+), 24 deletions(-) diff --git a/cmd/alp/cmd/diff.go b/cmd/alp/cmd/diff.go index aea0eea..ee6fd26 100644 --- a/cmd/alp/cmd/diff.go +++ b/cmd/alp/cmd/diff.go @@ -21,27 +21,17 @@ func NewDiffCmd(rootCmd *cobra.Command) *cobra.Command { return err } - from, err := cmd.PersistentFlags().GetString("from") - if err != nil { - return err - } - if from == "" { - if len(args) < 1 { - return fmt.Errorf("from is required") - } - from = args[0] + var from string + if len(args) < 1 { + return fmt.Errorf("from is required") } + from = args[0] - to, err := cmd.PersistentFlags().GetString("to") - if err != nil { - return err - } - if to == "" { - if len(args) < 2 { - return fmt.Errorf("to is required") - } - to = args[1] + var to string + if len(args) < 2 { + return fmt.Errorf("to is required") } + to = args[1] sts := stats.NewHTTPStats(true, false, false) @@ -98,13 +88,7 @@ func NewDiffCmd(rootCmd *cobra.Command) *cobra.Command { }, } - // app.Arg("from", "").Required().StringVar(&f.From) - // app.Arg("to", "").Required().StringVar(&f.To) - defineOptions(diffCmd) - diffCmd.PersistentFlags().StringP("from", "", "", "The comparison source file") - diffCmd.PersistentFlags().StringP("to", "", "", "The comparison target file") - return diffCmd } From 70c89c40385a586753b13f4130d10a9ae55d2799 Mon Sep 17 00:00:00 2001 From: Ryo Kitagawa Date: Tue, 15 Aug 2023 00:04:31 +0900 Subject: [PATCH 3/3] Use ExactArgs --- cmd/alp/cmd/diff.go | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/cmd/alp/cmd/diff.go b/cmd/alp/cmd/diff.go index ee6fd26..c45ffa7 100644 --- a/cmd/alp/cmd/diff.go +++ b/cmd/alp/cmd/diff.go @@ -1,7 +1,6 @@ package cmd import ( - "fmt" "os" "github.com/spf13/cobra" @@ -11,7 +10,7 @@ import ( func NewDiffCmd(rootCmd *cobra.Command) *cobra.Command { diffCmd := &cobra.Command{ Use: "diff ", - Args: cobra.RangeArgs(0, 2), + Args: cobra.ExactArgs(2), Short: "Show the difference between the two profile results", Long: `Show the difference between the two profile results`, RunE: func(cmd *cobra.Command, args []string) error { @@ -21,17 +20,8 @@ func NewDiffCmd(rootCmd *cobra.Command) *cobra.Command { return err } - var from string - if len(args) < 1 { - return fmt.Errorf("from is required") - } - from = args[0] - - var to string - if len(args) < 2 { - return fmt.Errorf("to is required") - } - to = args[1] + from := args[0] + to := args[1] sts := stats.NewHTTPStats(true, false, false)