From d2603bb6cc7f837e591c35dee48bf36f11d1caef Mon Sep 17 00:00:00 2001 From: Thomas Piccirello Date: Mon, 9 Oct 2023 22:13:57 -0700 Subject: [PATCH] Add `configure flags` command for configuring CLI behavior --- pkg/cmd/configure_flags.go | 151 +++++++++++++++++++++++++++++++++++++ pkg/configuration/flags.go | 43 +++++++++++ pkg/models/config.go | 1 + pkg/models/flags.go | 25 ++++++ pkg/printer/config.go | 36 +++++++++ 5 files changed, 256 insertions(+) create mode 100644 pkg/cmd/configure_flags.go create mode 100644 pkg/configuration/flags.go create mode 100644 pkg/models/flags.go diff --git a/pkg/cmd/configure_flags.go b/pkg/cmd/configure_flags.go new file mode 100644 index 00000000..ebafb6a1 --- /dev/null +++ b/pkg/cmd/configure_flags.go @@ -0,0 +1,151 @@ +/* +Copyright © 2023 Doppler + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +package cmd + +import ( + "errors" + "fmt" + + "github.com/DopplerHQ/cli/pkg/configuration" + "github.com/DopplerHQ/cli/pkg/models" + "github.com/DopplerHQ/cli/pkg/printer" + "github.com/DopplerHQ/cli/pkg/utils" + "github.com/spf13/cobra" +) + +var configureFlagsCmd = &cobra.Command{ + Use: "flags", + Short: "View current flags", + Args: cobra.NoArgs, + Run: func(cmd *cobra.Command, args []string) { + values := map[string]bool{} + flags := models.GetFlags() + for _, flag := range flags { + value := configuration.GetFlag(flag) + values[flag] = value + } + + printer.Flags(values, utils.OutputJSON) + }, +} + +var configureFlagsGetCmd = &cobra.Command{ + Use: "get [flag]", + Short: "Get the value of a flag", + ValidArgsFunction: FlagsValidArgs, + Args: cobra.ExactArgs(1), + Run: func(cmd *cobra.Command, args []string) { + plain := utils.GetBoolFlag(cmd, "plain") + + flag := args[0] + if !configuration.IsValidFlag(flag) { + utils.HandleError(errors.New("invalid flag " + flag)) + } + + enabled := configuration.GetFlag(flag) + + printer.Flag(flag, enabled, utils.OutputJSON, plain, false) + }, +} + +var configureFlagsEnableCmd = &cobra.Command{ + Use: "enable [flag]", + Short: "Enable a flag", + ValidArgsFunction: FlagsValidArgs, + Args: cobra.ExactArgs(1), + Run: func(cmd *cobra.Command, args []string) { + flag := args[0] + if !configuration.IsValidFlag(flag) { + utils.HandleError(errors.New("invalid flag " + flag)) + } + + const value = true + configuration.SetFlag(flag, value) + + if !utils.Silent { + printer.Flag(flag, value, utils.OutputJSON, false, false) + } + }, +} + +var configureFlagsDisableCmd = &cobra.Command{ + Use: "disable [flag]", + Short: "Disable a flag", + ValidArgsFunction: FlagsValidArgs, + Args: cobra.ExactArgs(1), + Run: func(cmd *cobra.Command, args []string) { + flag := args[0] + if !configuration.IsValidFlag(flag) { + utils.HandleError(errors.New("invalid flag " + flag)) + } + + const value = false + configuration.SetFlag(flag, value) + + if !utils.Silent { + printer.Flag(flag, value, utils.OutputJSON, false, false) + } + }, +} + +var configureFlagsResetCmd = &cobra.Command{ + Use: "reset [flag]", + Short: "Reset a flag to its default", + Args: cobra.ExactArgs(1), + Run: func(cmd *cobra.Command, args []string) { + + flag := args[0] + if !configuration.IsValidFlag(flag) { + utils.HandleError(errors.New("invalid flag " + flag)) + } + + yes := utils.GetBoolFlag(cmd, "yes") + defaultValue := configuration.GetFlagDefault(flag) + + if !yes { + utils.PrintWarning(fmt.Sprintf("This will reset the %s flag to %t", flag, defaultValue)) + if !utils.ConfirmationPrompt("Continue?", false) { + utils.Log("Aborting") + return + } + } + + configuration.SetFlag(flag, defaultValue) + printer.Flag(flag, defaultValue, utils.OutputJSON, false, false) + }, +} + +func FlagsValidArgs(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + persistentValidArgsFunction(cmd) + + return models.GetFlags(), cobra.ShellCompDirectiveNoFileComp +} + +func init() { + configureCmd.AddCommand(configureFlagsCmd) + + configureFlagsGetCmd.Flags().Bool("plain", false, "print value without formatting") + configureFlagsCmd.AddCommand(configureFlagsGetCmd) + + configureFlagsCmd.AddCommand(configureFlagsEnableCmd) + + configureFlagsCmd.AddCommand(configureFlagsDisableCmd) + + configureFlagsResetCmd.Flags().BoolP("yes", "y", false, "proceed without confirmation") + configureFlagsCmd.AddCommand(configureFlagsResetCmd) + + rootCmd.AddCommand(configureFlagsCmd) +} diff --git a/pkg/configuration/flags.go b/pkg/configuration/flags.go new file mode 100644 index 00000000..0015e682 --- /dev/null +++ b/pkg/configuration/flags.go @@ -0,0 +1,43 @@ +/* +Copyright © 2023 Doppler + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +package configuration + +import ( + "slices" + + "github.com/DopplerHQ/cli/pkg/models" +) + +func GetFlag(flag string) bool { + return false +} + +func SetFlag(flag string, enable bool) { + writeConfig(configContents) +} + +func GetFlagDefault(flag string) bool { + return false +} + +func IsValidFlag(flag string) bool { + flags := models.GetFlags() + return slices.Contains(flags, flag) +} + +func IsAnalyticsEnabled() bool { + return GetFlag(models.FlagAnalytics) +} diff --git a/pkg/models/config.go b/pkg/models/config.go index c7d21b28..735ae594 100644 --- a/pkg/models/config.go +++ b/pkg/models/config.go @@ -25,6 +25,7 @@ type ConfigFile struct { VersionCheck VersionCheck `yaml:"version-check"` Analytics AnalyticsOptions `yaml:"analytics"` TUI TUIOptions `yaml:"tui"` + Flags Flags `yaml:"flags,omitempty"` } // FileScopedOptions config options diff --git a/pkg/models/flags.go b/pkg/models/flags.go new file mode 100644 index 00000000..b26b8e2e --- /dev/null +++ b/pkg/models/flags.go @@ -0,0 +1,25 @@ +/* +Copyright © 2023 Doppler + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +package models + +type Flags struct { +} + +var flags = []string{} + +func GetFlags() []string { + return flags +} diff --git a/pkg/printer/config.go b/pkg/printer/config.go index e940afca..bd8b35b2 100644 --- a/pkg/printer/config.go +++ b/pkg/printer/config.go @@ -18,6 +18,7 @@ package printer import ( "fmt" "sort" + "strconv" "strings" "github.com/DopplerHQ/cli/pkg/configuration" @@ -181,3 +182,38 @@ func ConfigOptionNames(options []string, jsonFlag bool) { } Table([]string{"name"}, rows, TableOptions()) } + +func Flags(flags map[string]bool, jsonFlag bool) { + if jsonFlag { + JSON(flags) + return + } + + var rows [][]string + for flag, value := range flags { + rows = append(rows, []string{flag, strconv.FormatBool(value)}) + } + Table([]string{"flag", "value"}, rows, TableOptions()) +} + +func Flag(flag string, value bool, jsonFlag bool, plain bool, copy bool) { + if plain || copy { + if copy { + if err := utils.CopyToClipboard(strconv.FormatBool(value)); err != nil { + utils.HandleError(err, "Unable to copy to clipboard") + } + } + + if plain { + fmt.Println(strconv.FormatBool(value)) + return + } + } + + if jsonFlag { + JSON(map[string]bool{flag: value}) + return + } + + Table([]string{"flag", "value"}, [][]string{{flag, strconv.FormatBool(value)}}, TableOptions()) +}