github.com/turbot/steampipe@v1.7.0-rc.0.0.20240517123944-7cef272d4458/pkg/interactive/metaquery/handlers.go (about) 1 package metaquery 2 3 import ( 4 "context" 5 "fmt" 6 "strings" 7 8 typeHelpers "github.com/turbot/go-kit/types" 9 "github.com/turbot/steampipe/pkg/cmdconfig" 10 "github.com/turbot/steampipe/pkg/constants" 11 "golang.org/x/exp/maps" 12 ) 13 14 type handler func(ctx context.Context, input *HandlerInput) error 15 16 // Handle handles a metaquery execution from the interactive client 17 func Handle(ctx context.Context, input *HandlerInput) error { 18 cmd, _ := getCmdAndArgs(input.Query) 19 metaQueryObj, found := metaQueryDefinitions[cmd] 20 if !found { 21 return fmt.Errorf("not sure how to handle '%s'", cmd) 22 } 23 handlerFunction := metaQueryObj.handler 24 return handlerFunction(ctx, input) 25 } 26 27 // .header 28 // set the ArgHeader viper key with the boolean value evaluated from arg[0] 29 func setHeader(_ context.Context, input *HandlerInput) error { 30 cmdconfig.Viper().Set(constants.ArgHeader, typeHelpers.StringToBool(input.args()[0])) 31 return nil 32 } 33 34 // .multi 35 // set the ArgMulti viper key with the boolean value evaluated from arg[0] 36 func setMultiLine(_ context.Context, input *HandlerInput) error { 37 cmdconfig.Viper().Set(constants.ArgMultiLine, typeHelpers.StringToBool(input.args()[0])) 38 return nil 39 } 40 41 // .timing 42 // set the ArgHeader viper key with the boolean value evaluated from arg[0] 43 func setTiming(ctx context.Context, input *HandlerInput) error { 44 if len(input.args()) == 0 { 45 showTimingFlag() 46 return nil 47 } 48 49 cmdconfig.Viper().Set(constants.ArgTiming, input.args()[0]) 50 return nil 51 } 52 53 func showTimingFlag() { 54 timing := cmdconfig.Viper().GetString(constants.ArgTiming) 55 56 fmt.Printf(`Timing is %s. Available options are: %s`, 57 constants.Bold(timing), 58 constants.Bold(strings.Join(maps.Keys(constants.QueryTimingValueLookup), ", "))) 59 // add an empty line here so that the rendering buffer can start from the next line 60 fmt.Println() 61 62 return 63 } 64 65 // .separator and .output 66 // set the value of `viperKey` in `viper` with the value from `args[0]` 67 func setViperConfigFromArg(viperKey string) handler { 68 return func(_ context.Context, input *HandlerInput) error { 69 cmdconfig.Viper().Set(viperKey, input.args()[0]) 70 return nil 71 } 72 } 73 74 // .exit 75 func doExit(_ context.Context, input *HandlerInput) error { 76 input.ClosePrompt() 77 return nil 78 } 79 80 // .clear 81 func clearScreen(_ context.Context, input *HandlerInput) error { 82 input.Prompt.ClearScreen() 83 return nil 84 } 85 86 // .autocomplete 87 func setAutoComplete(_ context.Context, input *HandlerInput) error { 88 cmdconfig.Viper().Set(constants.ArgAutoComplete, typeHelpers.StringToBool(input.args()[0])) 89 return nil 90 }