-
Notifications
You must be signed in to change notification settings - Fork 6
/
root.go
105 lines (89 loc) · 3.53 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
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd
import (
"flag"
"fmt"
"os"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
const (
defaultClusterName = "my-cluster"
defaultNamespace = "default"
defaultInfrastructureProvider = "docker"
defaultBootstrapProvider = "kubeadm"
defaultVersion = "v1.15.3"
defaultControlPlaneCount = 1
defaultWorkerCount = 1
)
type generateOptions struct {
infraProvider string
clusterName string
clusterNamespace string
bsProvider string
k8sVersion string
machineDeployment bool
controlplaneMachineCount int
workerMachineCount int
allowEmptyEnvVar bool
}
// RootCmd returns the root command for capi-yaml-gen tool
func RootCmd() *cobra.Command {
rootCmd := &cobra.Command{
Use: "capi-yaml",
Short: "Devtool to help with YAML for CAPI and CAPI providers",
Long: "Devtool to help with YAML for CAPI and CAPI providers",
RunE: func(cmd *cobra.Command, args []string) error {
if err := cmd.Help(); err != nil {
return err
}
return nil
},
}
rootCmd.AddCommand(getGenerateCommand())
return rootCmd
}
func getGenerateCommand() *cobra.Command {
opts := generateOptions{}
cmd := &cobra.Command{
Use: "generate",
Short: "generate yaml for CAPI and its providers",
Long: "generate yaml for CAPI and its providers",
RunE: func(cmd *cobra.Command, args []string) error {
return runGenerateCommand(opts, os.Stdout)
},
}
cmd.Flags().StringVarP(&opts.clusterName, "cluster-name", "c", defaultClusterName, "Name for the cluster")
cmd.Flags().StringVarP(&opts.clusterNamespace, "namespace", "n", defaultNamespace, "Namespace where the cluster will be created")
cmd.Flags().StringVarP(&opts.infraProvider, "infrastructure-provider", "i", defaultInfrastructureProvider, "Infrastructure provider for the cluster")
cmd.Flags().StringVarP(&opts.bsProvider, "boostrap-provider", "b", defaultBootstrapProvider, "Bootstrap provider for the cluster")
cmd.Flags().StringVarP(&opts.k8sVersion, "k8s-version", "k", defaultVersion, "Version of kubernetes for the cluster")
cmd.Flags().BoolVarP(&opts.machineDeployment, "generate-machine-deployment", "d", true, "Generate a machine deployment instead of individual machines")
cmd.Flags().IntVarP(&opts.controlplaneMachineCount, "control-plane-count", "m", defaultControlPlaneCount, "Number of control plane machines in the cluster")
cmd.Flags().IntVarP(&opts.workerMachineCount, "worker-count", "w", defaultWorkerCount, "Number of worker machines in the cluster")
cmd.Flags().BoolVarP(&opts.allowEmptyEnvVar, "allow-empty-env-vars", "a", false, "Allow unset environment variables")
return cmd
}
// Execute starts the process
func Execute() {
if err := flag.CommandLine.Parse([]string{}); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
if err := RootCmd().Execute(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
}