Repository navigation
Expand file tree
/
Copy pathcommand_builder.go
More file actions
125 lines (109 loc) · 3.05 KB
/
Copy pathcommand_builder.go
File metadata and controls
125 lines (109 loc) · 3.05 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package cmd
import (
"fmt"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"k8s.io/cli-runtime/pkg/genericclioptions"
"github.com/dodopizza/kubectl-shovel/internal/flags"
"github.com/dodopizza/kubectl-shovel/internal/globals"
"github.com/dodopizza/kubectl-shovel/internal/kubernetes"
)
// CommonOptions contains shared arguments for cli commands
type CommonOptions struct {
Container string
Image string
Pod string
Output string
OutputHostPath string
StoreOutputOnHost bool
kubeConfig *genericclioptions.ConfigFlags
}
// CommandBuilder represents logic for building and running tools
type CommandBuilder struct {
CommonOptions *CommonOptions
tool flags.DotnetTool
kube *kubernetes.Client
}
// GetFlags return FlagSet that describes generic options
func (options *CommonOptions) GetFlags() *pflag.FlagSet {
fs := pflag.NewFlagSet("common", pflag.ExitOnError)
fs.StringVarP(
&options.Container,
"container",
"c",
options.Container,
"Target container in pod. Required if pod run multiple containers",
)
fs.StringVar(
&options.Image,
"image",
options.Image,
"Image of dumper to use for job",
)
fs.StringVar(
&options.Pod,
"pod-name",
options.Pod,
"Target pod",
)
_ = cobra.MarkFlagRequired(fs, "pod-name")
fs.StringVarP(
&options.Output,
"output",
"o",
options.Output,
"Output file",
)
fs.StringVar(
&options.OutputHostPath,
"output-host-path",
options.OutputHostPath,
"Host folder, where will be stored artifact",
)
fs.BoolVarP(
&options.StoreOutputOnHost,
"store-output-on-host",
"t",
options.StoreOutputOnHost,
"Store output on node instead of downloading it locally")
options.kubeConfig = genericclioptions.NewConfigFlags(false)
options.kubeConfig.AddFlags(fs)
return fs
}
// NewCommandBuilder returns *CommandBuilder instance with specified factory flags.DotnetToolFactory
// that responsible for creation of any available flags.DotnetTool
func NewCommandBuilder(factory flags.DotnetToolFactory) *CommandBuilder {
tool := factory()
return &CommandBuilder{
CommonOptions: &CommonOptions{
Image: globals.GetDumperImage(),
Output: fmt.Sprintf("./output.%s", tool.ToolName()),
OutputHostPath: fmt.Sprintf("%s/%s", globals.PathTmpFolder, globals.PluginName),
},
tool: tool,
}
}
// Build will build and returns *cobra.Command for specific tool
func (cb *CommandBuilder) Build(short, long, example string) *cobra.Command {
cmd := &cobra.Command{
Use: fmt.Sprintf("%s [flags]", cb.tool.ToolName()),
Short: short,
Long: long,
Example: example,
RunE: func(*cobra.Command, []string) error {
return cb.launch()
},
}
cmd.Flags().AddFlagSet(cb.parse())
return cmd
}
// Tool returns tool name that contains CommandBuilder
func (cb *CommandBuilder) Tool() string {
return cb.tool.ToolName()
}
func (cb *CommandBuilder) parse() *pflag.FlagSet {
fs := pflag.NewFlagSet(cb.tool.ToolName(), pflag.ExitOnError)
fs.AddFlagSet(cb.CommonOptions.GetFlags())
fs.AddFlagSet(cb.tool.GetFlags())
return fs
}