github.com/turbot/steampipe@v1.7.0-rc.0.0.20240517123944-7cef272d4458/cmd/variable.go (about) 1 package cmd 2 3 import ( 4 "fmt" 5 6 "github.com/spf13/cobra" 7 "github.com/spf13/viper" 8 "github.com/turbot/go-kit/helpers" 9 "github.com/turbot/steampipe/pkg/cmdconfig" 10 "github.com/turbot/steampipe/pkg/constants" 11 "github.com/turbot/steampipe/pkg/display" 12 "github.com/turbot/steampipe/pkg/error_helpers" 13 "github.com/turbot/steampipe/pkg/workspace" 14 ) 15 16 // Variable management commands 17 func variableCmd() *cobra.Command { 18 var cmd = &cobra.Command{ 19 Use: "variable [command]", 20 Args: cobra.NoArgs, 21 Short: "Steampipe variable management", 22 Long: `Steampipe variable management.`, 23 } 24 25 cmd.AddCommand(variableListCmd()) 26 cmd.Flags().BoolP(constants.ArgHelp, "h", false, "Help for variable") 27 28 cmdconfig. 29 OnCmd(cmd). 30 AddModLocationFlag() 31 32 return cmd 33 } 34 35 // List variables 36 func variableListCmd() *cobra.Command { 37 var cmd = &cobra.Command{ 38 Use: "list", 39 Args: cobra.NoArgs, 40 Run: runVariableListCmd, 41 Short: "List currently installed variables", 42 Long: `List currently installed variables. 43 44 List all Steampipe variables installed for this user. 45 46 Example: 47 48 # List installed variables 49 steampipe variable list 50 51 `, 52 } 53 54 cmdconfig. 55 OnCmd(cmd). 56 AddBoolFlag("outdated", false, "Check each variable in the list for updates"). 57 AddBoolFlag(constants.ArgHelp, false, "Help for variable list", cmdconfig.FlagOptions.WithShortHand("h")). 58 AddModLocationFlag(). 59 AddStringFlag(constants.ArgOutput, constants.OutputFormatTable, "Select a console output format: table or json") 60 61 return cmd 62 } 63 64 func runVariableListCmd(cmd *cobra.Command, _ []string) { 65 ctx := cmd.Context() 66 defer func() { 67 if r := recover(); r != nil { 68 error_helpers.ShowError(ctx, helpers.ToError(r)) 69 exitCode = constants.ExitCodeUnknownErrorPanic 70 } 71 }() 72 73 // validate output arg 74 output := viper.GetString(constants.ArgOutput) 75 if !helpers.StringSliceContains([]string{constants.OutputFormatTable, constants.OutputFormatJSON}, output) { 76 error_helpers.ShowError(ctx, fmt.Errorf("output flag must be either 'json' or 'table'")) 77 return 78 } 79 80 workspacePath := viper.GetString(constants.ArgModLocation) 81 82 vars, errorsAndWarnings := workspace.LoadVariables(ctx, workspacePath) 83 // load the workspace 84 error_helpers.FailOnErrorWithMessage(errorsAndWarnings.Error, "failed to load workspace") 85 86 if viper.GetString(constants.ArgOutput) == constants.OutputFormatJSON { 87 display.ShowVarsListJson(vars) 88 } else { 89 90 display.ShowVarsListTable(vars) 91 } 92 }