-
-
Notifications
You must be signed in to change notification settings - Fork 117
Add --feature-flag option to supervisor options command #649
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,9 @@ | ||
| package cmd | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "log/slog" | ||
| "strconv" | ||
| "strings" | ||
|
|
||
| helper "github.com/home-assistant/cli/client" | ||
|
|
@@ -16,7 +18,8 @@ var supervisorOptionsCmd = &cobra.Command{ | |
| This command allows you to set configuration options for on the Home Assistant | ||
| Supervisor running on your Home Assistant system.`, | ||
| Example: ` | ||
| ha supervisor options --channel beta`, | ||
| ha supervisor options --channel beta | ||
| ha supervisor options --feature-flag supervisor_v2_api=true`, | ||
| ValidArgsFunction: cobra.NoFileCompletions, | ||
| Args: cobra.NoArgs, | ||
| Run: func(cmd *cobra.Command, args []string) { | ||
|
|
@@ -52,6 +55,34 @@ Supervisor running on your Home Assistant system.`, | |
| } | ||
| } | ||
|
|
||
| featureFlags, err := cmd.Flags().GetStringArray("feature-flag") | ||
| if err == nil && len(featureFlags) > 0 { | ||
| flagMap := make(map[string]bool) | ||
| for _, entry := range featureFlags { | ||
| parts := strings.SplitN(entry, "=", 2) | ||
| name := strings.TrimSpace(parts[0]) | ||
| if name == "" { | ||
| helper.PrintError(fmt.Errorf("invalid feature flag %q: name must not be empty", entry)) | ||
| ExitWithError = true | ||
| return | ||
| } | ||
|
|
||
| var val bool | ||
| if len(parts) == 1 { | ||
| val = true | ||
| } else { | ||
| val, err = strconv.ParseBool(parts[1]) | ||
| if err != nil { | ||
| helper.PrintError(fmt.Errorf("invalid value for feature flag %q: %q, expected true or false", name, parts[1])) | ||
| ExitWithError = true | ||
| return | ||
| } | ||
| } | ||
| flagMap[name] = val | ||
| } | ||
| options["feature_flags"] = flagMap | ||
| } | ||
|
|
||
| resp, err := helper.GenericJSONPost(section, command, options) | ||
| if err != nil { | ||
| helper.PrintError(err) | ||
|
|
@@ -72,6 +103,7 @@ func init() { | |
| supervisorOptionsCmd.Flags().BoolP("debug-block", "", false, "Enable debug mode with blocking startup") | ||
| supervisorOptionsCmd.Flags().BoolP("diagnostics", "", false, "Enable diagnostics mode") | ||
| supervisorOptionsCmd.Flags().BoolP("auto-update", "", true, "Enable/disable supervisor auto update") | ||
| supervisorOptionsCmd.Flags().StringArrayP("feature-flag", "", []string{}, "Set a development feature flag (name=true|false). Use multiple times for multiple flags.") | ||
|
|
||
| supervisorOptionsCmd.Flags().Lookup("debug").NoOptDefVal = "false" | ||
| supervisorOptionsCmd.Flags().Lookup("debug-block").NoOptDefVal = "false" | ||
|
|
@@ -92,6 +124,31 @@ func init() { | |
| supervisorOptionsCmd.RegisterFlagCompletionFunc("debug", boolCompletions) | ||
| supervisorOptionsCmd.RegisterFlagCompletionFunc("debug-block", boolCompletions) | ||
| supervisorOptionsCmd.RegisterFlagCompletionFunc("diagnostics", boolCompletions) | ||
| supervisorOptionsCmd.RegisterFlagCompletionFunc("feature-flag", featureFlagCompletions) | ||
|
|
||
| supervisorCmd.AddCommand(supervisorOptionsCmd) | ||
| } | ||
|
|
||
| func featureFlagCompletions(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { | ||
| resp, err := helper.GenericJSONGet("supervisor", "info") | ||
| if err != nil || !resp.IsSuccess() { | ||
| return nil, cobra.ShellCompDirectiveNoFileComp | ||
| } | ||
| data := resp.Result().(*helper.Response) | ||
| if data.Result != "ok" || data.Data["feature_flags"] == nil { | ||
| return nil, cobra.ShellCompDirectiveNoFileComp | ||
| } | ||
| flags, ok := data.Data["feature_flags"].(map[string]any) | ||
| if !ok { | ||
| return nil, cobra.ShellCompDirectiveNoFileComp | ||
| } | ||
| var ret []string | ||
| for name, val := range flags { | ||
| current, ok := val.(bool) | ||
| if !ok { | ||
| continue | ||
| } | ||
| ret = append(ret, fmt.Sprintf("%s=%v\tcurrently %v", name, !current, current)) | ||
|
Comment on lines
+147
to
+151
|
||
| } | ||
| return ret, cobra.ShellCompDirectiveNoFileComp | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The help text for
--feature-flagsays(name=true|false), but the implementation also accepts a bare name and defaults it totrue. Update the flag description to reflect the supported syntax so users aren’t misled.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems kinda obvious to me, or can be explored if needs to be. No extra documentation needed.