Skip to content

Commit

Permalink
Add configure flags command for configuring CLI behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
Piccirello authored and nmanoogian committed Feb 13, 2024
1 parent f4b59de commit d2603bb
Show file tree
Hide file tree
Showing 5 changed files with 256 additions and 0 deletions.
151 changes: 151 additions & 0 deletions pkg/cmd/configure_flags.go
@@ -0,0 +1,151 @@
/*
Copyright © 2023 Doppler <support@doppler.com>
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)
}
43 changes: 43 additions & 0 deletions pkg/configuration/flags.go
@@ -0,0 +1,43 @@
/*
Copyright © 2023 Doppler <support@doppler.com>
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)
}
1 change: 1 addition & 0 deletions pkg/models/config.go
Expand Up @@ -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
Expand Down
25 changes: 25 additions & 0 deletions pkg/models/flags.go
@@ -0,0 +1,25 @@
/*
Copyright © 2023 Doppler <support@doppler.com>
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
}
36 changes: 36 additions & 0 deletions pkg/printer/config.go
Expand Up @@ -18,6 +18,7 @@ package printer
import (
"fmt"
"sort"
"strconv"
"strings"

"github.com/DopplerHQ/cli/pkg/configuration"
Expand Down Expand Up @@ -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())
}

0 comments on commit d2603bb

Please sign in to comment.