Skip to content

Commit

Permalink
Merge pull request #15 from hammingweight/use-force-arg-instead-of-sw…
Browse files Browse the repository at this point in the history
…itch

Use cobra to check the arguments.
  • Loading branch information
hammingweight authored Jan 14, 2025
2 parents 24057d4 + 3a3787a commit ab9db15
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 8 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,10 @@ Edit the JSON file with your new settings and use the `apply` subcommand

```
$ vi settings.json
$ synkctl inverter apply -f settings.json --force`
$ synkctl inverter apply -f settings.json force`
```

Note that the `--force` argument must be supplied to acknowledge that you are doing something potentially dangerous: There is no validation of the settings.
Note that the `force` argument must be supplied to acknowledge that you are doing something potentially dangerous: There is no validation of the settings.

## The **synkctl** REST Client
A CLI can be useful but for more complex scenarios, it's better to have a program that monitors and adjust settings by making API calls. For example:
Expand Down
10 changes: 4 additions & 6 deletions cmd/inverter_apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
)

func confirmApply() bool {
fmt.Println("You did not specify \"--force=true\". Do you really want to proceed?")
fmt.Println("You did not pass the argument \"force\". Do you really want to proceed?")
fmt.Println("[yes/NO])")
var resp string
fmt.Scan(&resp)
Expand Down Expand Up @@ -61,9 +61,9 @@ 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),
Args: cobra.MatchAll(cobra.MaximumNArgs(1), cobra.OnlyValidArgs),
RunE: func(cmd *cobra.Command, args []string) error {
if !forceFlag {
if len(args) != 1 {
if !confirmApply() {
fmt.Println("aborting")
os.Exit(1)
Expand All @@ -80,13 +80,11 @@ var inverterApplyCmd = &cobra.Command{
}
return applyInverterSettings(cmd.Context(), in)
},
ValidArgs: []string{"force"},
}

var forceFlag bool

func init() {
inverterCmd.AddCommand(inverterApplyCmd)
inverterApplyCmd.Flags().BoolVar(&forceFlag, "force", false, "Acknowledge the risks")
inverterApplyCmd.Flags().StringP("file", "f", "", "JSON file with inverter settings")
viper.BindPFlag("file", inverterApplyCmd.Flags().Lookup("file"))
}

0 comments on commit ab9db15

Please sign in to comment.