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