-
Notifications
You must be signed in to change notification settings - Fork 40
/
instance_id_arg.go
67 lines (59 loc) · 2.32 KB
/
instance_id_arg.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
package instance_id_arg
import (
"context"
"github.com/kurtosis-tech/kurtosis/cli/cli/command_framework/lowlevel/args"
"github.com/kurtosis-tech/kurtosis/cli/cli/command_framework/lowlevel/flags"
"github.com/kurtosis-tech/stacktrace"
)
const (
defaultIsRequired = true
defaultValueEmpty = ""
validInstanceIdLength = 32
)
// InstanceIdentifierArg pre-builds instance identifier arg which has tab-completion and validation ready out-of-the-box
func InstanceIdentifierArg(
// The arg key where this context identifier argument will be stored
argKey string,
isGreedy bool,
) *args.ArgConfig {
validate := getValidationFunc(argKey, isGreedy)
return &args.ArgConfig{
Key: argKey,
IsOptional: defaultIsRequired,
DefaultValue: defaultValueEmpty,
IsGreedy: isGreedy,
ValidationFunc: validate,
ArgCompletionProvider: args.NewManualCompletionsProvider(getCompletionsFunc()),
}
}
func getCompletionsFunc() func(ctx context.Context, flags *flags.ParsedFlags, previousArgs *args.ParsedArgs) ([]string, error) {
return func(ctx context.Context, flags *flags.ParsedFlags, previousArgs *args.ParsedArgs) ([]string, error) {
// TODO: Given the instance id and the API Key, we could potentially query the API for instance ids to
// auto complete the typing but those endpoints don't exist (yet).
return []string{}, nil
}
}
func getValidationFunc(argKey string, isGreedy bool) func(context.Context, *flags.ParsedFlags, *args.ParsedArgs) error {
return func(ctx context.Context, flags *flags.ParsedFlags, args *args.ParsedArgs) error {
var instanceIdsToValidate []string
if isGreedy {
instanceID, err := args.GetGreedyArg(argKey)
if err != nil {
return stacktrace.Propagate(err, "Expected a value for greedy arg '%v' but didn't find one", argKey)
}
instanceIdsToValidate = instanceID
} else {
instanceID, err := args.GetNonGreedyArg(argKey)
if err != nil {
return stacktrace.Propagate(err, "Expected a value for non-greedy arg '%v' but didn't find one", argKey)
}
instanceIdsToValidate = []string{instanceID}
}
for _, instanceIdToValidate := range instanceIdsToValidate {
if len(instanceIdToValidate) < validInstanceIdLength {
return stacktrace.NewError("Instance Id is not valid: %s", instanceIdsToValidate)
}
}
return nil
}
}