-
Notifications
You must be signed in to change notification settings - Fork 5
/
command_builder.go
106 lines (90 loc) · 2.34 KB
/
command_builder.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package cmd
import (
"fmt"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/dodopizza/kubectl-shovel/internal/flags"
)
// CommonOptions represents container info
type CommonOptions struct {
ContainerID string
ContainerRuntime string
ContainerName string
PodName string
PodNamespace string
StoreOutputOnHost bool
}
// CommandBuilder represents logic for building and running tools
type CommandBuilder struct {
CommonOptions *CommonOptions
tool flags.DotnetTool
}
// GetFlags return FlagSet that describes options for container selection
func (options *CommonOptions) GetFlags() *pflag.FlagSet {
fs := pflag.NewFlagSet("common", pflag.ExitOnError)
fs.StringVar(
&options.ContainerID,
"container-id",
options.ContainerID,
"Container ID to run tool for",
)
_ = cobra.MarkFlagRequired(fs, "container-id")
fs.StringVar(
&options.ContainerRuntime,
"container-runtime",
options.ContainerRuntime,
"Container Runtime to run tool for",
)
_ = cobra.MarkFlagRequired(fs, "container-runtime")
fs.StringVar(
&options.ContainerName,
"container-name",
options.ContainerName,
"Container name to run tool for",
)
fs.StringVar(
&options.PodName,
"pod-name",
options.PodName,
"Pod name to run tool for",
)
fs.StringVar(
&options.PodNamespace,
"pod-namespace",
options.PodNamespace,
"Pod namespace to run tool for",
)
fs.BoolVar(
&options.StoreOutputOnHost,
"store-output-on-host",
options.StoreOutputOnHost,
"Flag, indicating that output should be stored on host /tmp folder",
)
return fs
}
// NewCommandBuilder returns options with specified tool name
func NewCommandBuilder(options *CommonOptions, factory flags.DotnetToolFactory) *CommandBuilder {
return &CommandBuilder{
tool: factory(),
CommonOptions: options,
}
}
// Build will build command *cobra.Command from options
func (cb *CommandBuilder) Build() *cobra.Command {
cmd := &cobra.Command{
Use: fmt.Sprintf("%s [flags]", cb.tool.ToolName()),
Args: cobra.NoArgs,
Short: "",
Long: "",
RunE: func(cmd *cobra.Command, args []string) error {
return cb.launch()
},
}
cmd.PersistentFlags().AddFlagSet(cb.flags())
return cmd
}
func (cb *CommandBuilder) flags() *pflag.FlagSet {
fs := pflag.NewFlagSet(cb.tool.ToolName(), pflag.ExitOnError)
fs.AddFlagSet(cb.tool.GetFlags())
return fs
}