Skip to content

Commit

Permalink
Merge pull request #31 from hammingweight/fix-keys-flag
Browse files Browse the repository at this point in the history
The 'keys' flag applies only to 'get' commands.
  • Loading branch information
hammingweight authored Feb 6, 2025
2 parents df4acfb + 29d748d commit 801db0b
Show file tree
Hide file tree
Showing 8 changed files with 13 additions and 5 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ Flags:
-c, --config string synkctl config file location
-h, --help help for synkctl
-i, --inverter string SunSynk inverter serial number
-k, --keys string Extract specific keys from response
-v, --version version for synkctl
Use "synkctl [command] --help" for more information about a command.
Expand Down
1 change: 1 addition & 0 deletions cmd/battery_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,5 @@ var batteryGetCmd = &cobra.Command{

func init() {
batteryCmd.AddCommand(batteryGetCmd)
addKeysFlag(batteryGetCmd)
}
6 changes: 6 additions & 0 deletions cmd/cmd_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"strings"

"github.com/hammingweight/synkctl/rest"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

Expand All @@ -35,3 +36,8 @@ func displayObject(o *rest.SynkObject) error {
fmt.Println(o)
return nil
}

func addKeysFlag(cmd *cobra.Command) {
cmd.PersistentFlags().StringP("keys", "k", "", "Extract specific keys from response")
viper.BindPFlag("keys", cmd.PersistentFlags().Lookup("keys"))
}
1 change: 1 addition & 0 deletions cmd/grid_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,5 @@ var gridGetCmd = &cobra.Command{

func init() {
gridCmd.AddCommand(gridGetCmd)
addKeysFlag(gridGetCmd)
}
1 change: 1 addition & 0 deletions cmd/input_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,5 @@ var inputGetCmd = &cobra.Command{

func init() {
inputCmd.AddCommand(inputGetCmd)
addKeysFlag(inputGetCmd)
}
1 change: 1 addition & 0 deletions cmd/inverter_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ var inverterGetCmd = &cobra.Command{

func init() {
inverterCmd.AddCommand(inverterGetCmd)
addKeysFlag(inverterGetCmd)

inverterGetCmd.Flags().BoolP("short", "s", false, "Get short output (get only fields that can be updated)")
viper.BindPFlag("short", inverterGetCmd.Flags().Lookup("short"))
Expand Down
5 changes: 3 additions & 2 deletions cmd/load_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func readLoad(ctx context.Context) error {
}

// The load command displays load statistics
var getLoadCmd = &cobra.Command{
var loadGetCmd = &cobra.Command{
Use: "get",
Short: "Gets the inverter's current and cumulative load statistics",
Aliases: []string{"read"},
Expand All @@ -48,5 +48,6 @@ var getLoadCmd = &cobra.Command{
}

func init() {
loadCmd.AddCommand(getLoadCmd)
loadCmd.AddCommand(loadGetCmd)
addKeysFlag(loadGetCmd)
}
2 changes: 0 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,13 @@ func init() {
// Arguments that apply to all subcommands.
rootCmd.PersistentFlags().StringP("config", "c", configFile, "synkctl config file location")
rootCmd.PersistentFlags().StringP("inverter", "i", "", "SunSynk inverter serial number")
rootCmd.PersistentFlags().StringP("keys", "k", "", "Extract specific keys from response")

// Set up viper
replacer := strings.NewReplacer("-", "_")
viper.SetEnvKeyReplacer(replacer)
viper.SetEnvPrefix("SYNK")
viper.BindPFlag("config", rootCmd.PersistentFlags().Lookup("config"))
viper.BindPFlag("inverter", rootCmd.PersistentFlags().Lookup("inverter"))
viper.BindPFlag("keys", rootCmd.PersistentFlags().Lookup("keys"))
}

func initConfig() {
Expand Down

0 comments on commit 801db0b

Please sign in to comment.