|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + |
| 7 | + helper "github.com/home-assistant/cli/client" |
| 8 | + log "github.com/sirupsen/logrus" |
| 9 | + "github.com/spf13/cobra" |
| 10 | + "github.com/spf13/pflag" |
| 11 | +) |
| 12 | + |
| 13 | +var dockerOptionsCmd = &cobra.Command{ |
| 14 | + Use: "options", |
| 15 | + Aliases: []string{"option", "opt", "opts", "op"}, |
| 16 | + Short: "Allows you to set options on the host docker backend", |
| 17 | + Long: ` |
| 18 | +This command allows you to set configuration options for on the host |
| 19 | +docker backend running on your Home Assistant system.`, |
| 20 | + Example: ` |
| 21 | + ha docker options --enable-ipv6 true`, |
| 22 | + ValidArgsFunction: cobra.NoFileCompletions, |
| 23 | + Args: cobra.NoArgs, |
| 24 | + Run: func(cmd *cobra.Command, args []string) { |
| 25 | + log.WithField("args", args).Debug("docker options") |
| 26 | + |
| 27 | + section := "docker" |
| 28 | + command := "options" |
| 29 | + |
| 30 | + options := make(map[string]any) |
| 31 | + |
| 32 | + for _, value := range []string{ |
| 33 | + "enable_ipv6", |
| 34 | + } { |
| 35 | + data, err := cmd.Flags().GetBool(value) |
| 36 | + if err == nil && cmd.Flags().Changed(value) { |
| 37 | + options[strings.ReplaceAll(value, "-", "_")] = data |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + resp, err := helper.GenericJSONPost(section, command, options) |
| 42 | + if err != nil { |
| 43 | + fmt.Println(err) |
| 44 | + ExitWithError = true |
| 45 | + } else { |
| 46 | + ExitWithError = !helper.ShowJSONResponse(resp) |
| 47 | + } |
| 48 | + }, |
| 49 | +} |
| 50 | + |
| 51 | +func init() { |
| 52 | + dockerOptionsCmd.Flags().BoolP("enable-ipv6", "", false, "Enable IPv6") |
| 53 | + dockerOptionsCmd.Flags().SetNormalizeFunc(func(set *pflag.FlagSet, name string) pflag.NormalizedName { |
| 54 | + return pflag.NormalizedName(strings.ReplaceAll(name, "_", "-")) |
| 55 | + }) |
| 56 | + |
| 57 | + dockerCmd.AddCommand(dockerOptionsCmd) |
| 58 | +} |
0 commit comments