forked from kubevirt/kubevirt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroot.go
140 lines (126 loc) · 4.62 KB
/
root.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package virtctl
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/spf13/cobra"
"k8s.io/client-go/tools/clientcmd"
"kubevirt.io/client-go/kubecli"
"kubevirt.io/client-go/log"
"kubevirt.io/kubevirt/pkg/virtctl/adm"
"kubevirt.io/kubevirt/pkg/virtctl/configuration"
"kubevirt.io/kubevirt/pkg/virtctl/console"
"kubevirt.io/kubevirt/pkg/virtctl/create"
"kubevirt.io/kubevirt/pkg/virtctl/credentials"
"kubevirt.io/kubevirt/pkg/virtctl/expose"
"kubevirt.io/kubevirt/pkg/virtctl/guestfs"
"kubevirt.io/kubevirt/pkg/virtctl/imageupload"
"kubevirt.io/kubevirt/pkg/virtctl/memorydump"
"kubevirt.io/kubevirt/pkg/virtctl/pause"
"kubevirt.io/kubevirt/pkg/virtctl/portforward"
"kubevirt.io/kubevirt/pkg/virtctl/scp"
"kubevirt.io/kubevirt/pkg/virtctl/softreboot"
"kubevirt.io/kubevirt/pkg/virtctl/ssh"
"kubevirt.io/kubevirt/pkg/virtctl/templates"
"kubevirt.io/kubevirt/pkg/virtctl/usbredir"
"kubevirt.io/kubevirt/pkg/virtctl/version"
"kubevirt.io/kubevirt/pkg/virtctl/vm"
"kubevirt.io/kubevirt/pkg/virtctl/vmexport"
"kubevirt.io/kubevirt/pkg/virtctl/vnc"
)
var programName string
func NewVirtctlCommand() (*cobra.Command, clientcmd.ClientConfig) {
programName := GetProgramName(filepath.Base(os.Args[0]))
// used in cobra templates to display either `kubectl virt` or `virtctl`
cobra.AddTemplateFunc(
"ProgramName", func() string {
return programName
},
)
// used to enable replacement of `ProgramName` placeholder for cobra.Example, which has no template support
cobra.AddTemplateFunc(
"prepare", func(s string) string {
// order matters!
result := strings.Replace(s, "{{ProgramName}}", programName, -1)
return result
},
)
rootCmd := &cobra.Command{
Use: programName,
Short: programName + " controls virtual machine related operations on your kubernetes cluster.",
SilenceUsage: true,
SilenceErrors: true,
Run: func(cmd *cobra.Command, args []string) {
cmd.Printf(cmd.UsageString())
},
}
optionsCmd := &cobra.Command{
Use: "options",
Hidden: true,
Run: func(cmd *cobra.Command, args []string) {
cmd.Printf(cmd.UsageString())
},
}
optionsCmd.SetUsageTemplate(templates.OptionsUsageTemplate())
//TODO: Add a ClientConfigFactory which allows substituting the KubeVirt client with a mock for unit testing
clientConfig := kubecli.DefaultClientConfig(rootCmd.PersistentFlags())
AddGlogFlags(rootCmd.PersistentFlags())
rootCmd.SetUsageTemplate(templates.MainUsageTemplate())
rootCmd.SetOut(os.Stdout)
rootCmd.AddCommand(
configuration.NewListPermittedDevices(clientConfig),
console.NewCommand(clientConfig),
usbredir.NewCommand(clientConfig),
vnc.NewCommand(clientConfig),
scp.NewCommand(clientConfig),
ssh.NewCommand(clientConfig),
portforward.NewCommand(clientConfig),
vm.NewStartCommand(clientConfig),
vm.NewStopCommand(clientConfig),
vm.NewRestartCommand(clientConfig),
vm.NewMigrateCommand(clientConfig),
vm.NewMigrateCancelCommand(clientConfig),
vm.NewGuestOsInfoCommand(clientConfig),
vm.NewUserListCommand(clientConfig),
vm.NewFSListCommand(clientConfig),
vm.NewAddVolumeCommand(clientConfig),
vm.NewRemoveVolumeCommand(clientConfig),
vm.NewExpandCommand(clientConfig),
memorydump.NewMemoryDumpCommand(clientConfig),
pause.NewPauseCommand(clientConfig),
pause.NewUnpauseCommand(clientConfig),
softreboot.NewSoftRebootCommand(clientConfig),
expose.NewExposeCommand(clientConfig),
version.VersionCommand(clientConfig),
imageupload.NewImageUploadCommand(clientConfig),
guestfs.NewGuestfsShellCommand(clientConfig),
vmexport.NewVirtualMachineExportCommand(clientConfig),
create.NewCommand(clientConfig),
credentials.NewCommand(clientConfig),
adm.NewCommand(clientConfig),
optionsCmd,
)
return rootCmd, clientConfig
}
// GetProgramName returns the command name to display in help texts.
// If `virtctl` is installed via krew to be used as a kubectl plugin, it's run via a symlink, so the basename then
// is `kubectl-virt`. In this case we want to accommodate the user by adjusting the help text (usage, examples and
// the like) by displaying `kubectl virt <command>` instead of `virtctl <command>`.
// see https://github.com/kubevirt/kubevirt/issues/2356 for more details
// see also templates.go
func GetProgramName(binary string) string {
if strings.HasSuffix(binary, "-virt") {
return fmt.Sprintf("%s virt", strings.TrimSuffix(binary, "-virt"))
}
return "virtctl"
}
func Execute() {
log.InitializeLogging(programName)
cmd, clientConfig := NewVirtctlCommand()
if err := cmd.Execute(); err != nil {
version.CheckClientServerVersion(&clientConfig)
fmt.Fprintln(cmd.Root().ErrOrStderr(), strings.TrimSpace(err.Error()))
os.Exit(1)
}
}