Skip to content

Commit

Permalink
Merge pull request #14 from hammingweight/use-cobra-exact-args
Browse files Browse the repository at this point in the history
Use cobra to check that there are no command line arguments.
  • Loading branch information
hammingweight authored Jan 14, 2025
2 parents cd979d1 + e9d8e49 commit 24057d4
Show file tree
Hide file tree
Showing 10 changed files with 11 additions and 30 deletions.
4 changes: 1 addition & 3 deletions cmd/battery_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,8 @@ var batteryGetCmd = &cobra.Command{
Use: "get",
Short: "Reads battery statistics",
Aliases: []string{"read"},
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 0 {
return fmt.Errorf("%w '%v'", ErrUnexpectedArguments, args)
}
return readBattery(cmd.Context())
},
}
Expand Down
4 changes: 1 addition & 3 deletions cmd/configuration_generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,8 @@ func generate() error {
var generateCmd = &cobra.Command{
Use: "generate",
Short: "Creates a configuration file",
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 0 {
return fmt.Errorf("%w '%v'", ErrUnexpectedArguments, args)
}
return generate()
},
}
Expand Down
8 changes: 3 additions & 5 deletions cmd/configuration_verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ import (
)

// Invoke the SunSynk OAUTH endpoint and check that the request succeeds
func verify(ctx context.Context, args []string) error {
if len(args) != 0 {
return fmt.Errorf("unexpected argument '%s'", args[0])
}
func verify(ctx context.Context) error {
config, err := configuration.ReadConfigurationFromFile(viper.GetString("config"))
if err != nil {
return fmt.Errorf("%w: %w", ErrCantAuthenticateUser, err)
Expand All @@ -53,8 +50,9 @@ and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
return verify(cmd.Context(), args)
return verify(cmd.Context())
},
}

Expand Down
1 change: 0 additions & 1 deletion cmd/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,4 @@ var (
ErrCantReadLoadStatistics = errors.New("can't read load statistics")
ErrCantUpdateInverterSettings = errors.New("can't update inverter settings")
ErrNoInverterSerialNumber = errors.New("no inverter serial number (--inverter) supplied")
ErrUnexpectedArguments = errors.New("unexpected argument(s)")
)
4 changes: 1 addition & 3 deletions cmd/grid_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,8 @@ var gridGetCmd = &cobra.Command{
Use: "get",
Short: "Gets the state of the grid connection",
Aliases: []string{"read"},
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 0 {
return fmt.Errorf("%w '%v'", ErrUnexpectedArguments, args)
}
return readGrid(cmd.Context())
},
}
Expand Down
4 changes: 1 addition & 3 deletions cmd/input_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,8 @@ var inputGetCmd = &cobra.Command{
Use: "get",
Aliases: []string{"read"},
Short: "Gets the state of the inverter's inputs",
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 0 {
return fmt.Errorf("%w '%v'", ErrUnexpectedArguments, args)
}
return readInputState(cmd.Context())
},
}
Expand Down
4 changes: 1 addition & 3 deletions cmd/inverter_apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,8 @@ func applyInverterSettings(ctx context.Context, in *os.File) error {
var inverterApplyCmd = &cobra.Command{
Use: "apply",
Short: "Applies the inverter settings from a file or stdin",
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 0 {
return fmt.Errorf("%w '%v'", ErrUnexpectedArguments, args)
}
if !forceFlag {
if !confirmApply() {
fmt.Println("aborting")
Expand Down
4 changes: 1 addition & 3 deletions cmd/inverter_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,8 @@ var inverterGetCmd = &cobra.Command{
Use: "get",
Short: "Reads the inverter settings",
Aliases: []string{"read"},
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 0 {
return fmt.Errorf("%w '%v'", ErrUnexpectedArguments, args)
}
return readInverterSettings(cmd.Context())
},
}
Expand Down
4 changes: 1 addition & 3 deletions cmd/inverter_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,8 @@ func updateInverterSettings(ctx context.Context) error {
var updateCmd = &cobra.Command{
Use: "update",
Short: "Basic options to update the inverter settings",
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 0 {
return fmt.Errorf("%w '%v'", ErrUnexpectedArguments, args)
}
return updateInverterSettings(cmd.Context())
},
}
Expand Down
4 changes: 1 addition & 3 deletions cmd/load_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,8 @@ var getLoadCmd = &cobra.Command{
Use: "get",
Short: "Gets the inverter's current and cumulative load statistics",
Aliases: []string{"read"},
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 0 {
return fmt.Errorf("%w '%v'", ErrUnexpectedArguments, args)
}
return readLoad(cmd.Context())
},
}
Expand Down

0 comments on commit 24057d4

Please sign in to comment.