-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #436 from DopplerHQ/tom-flags
Add `configure flags` command for configuring CLI behavior
- Loading branch information
Showing
16 changed files
with
504 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,7 +25,7 @@ jobs: | |
- uses: actions/checkout@v2 | ||
- name: Salus Scan | ||
id: salus_scan | ||
uses: federacy/[email protected].4 | ||
uses: federacy/[email protected].5 | ||
env: | ||
SALUS_CONFIGURATION: "file://salus-config.yaml" | ||
with: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
/* | ||
Copyright © 2023 Doppler <[email protected]> | ||
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.