gitlab.com/sparetimecoders/build-tools@v0.1.0/pkg/kubecmd/kubecmd.go (about) 1 package kubecmd 2 3 import ( 4 "flag" 5 "fmt" 6 "gitlab.com/sparetimecoders/build-tools/pkg/config" 7 "io" 8 ) 9 10 func Kubecmd(dir string, out io.Writer, args ...string) *string { 11 var context, namespace string 12 const ( 13 contextUsage = "override the context for default environment deployment target" 14 namespaceUsage = "override the namespace for default environment deployment target" 15 ) 16 set := flag.NewFlagSet("deploy", flag.ExitOnError) 17 set.Usage = func() { 18 _, _ = fmt.Fprintf(out, "Usage: deploy [options] <environment>\n\nFor example `deploy --context test-cluster --namespace test prod` would deploy to namsepace `test` in the `test-cluster` but assuming to use the `prod` configuration files (if present)\n\nOptions:\n") 19 set.PrintDefaults() 20 } 21 set.StringVar(&context, "context", "", contextUsage) 22 set.StringVar(&context, "c", "", contextUsage+" (shorthand)") 23 set.StringVar(&namespace, "namespace", "", namespaceUsage) 24 set.StringVar(&namespace, "n", "", namespaceUsage+" (shorthand)") 25 26 _ = set.Parse(args) 27 28 if set.NArg() < 1 { 29 set.Usage() 30 } else { 31 environment := set.Args()[0] 32 if cfg, err := config.Load(dir, out); err != nil { 33 _, _ = fmt.Fprintln(out, err.Error()) 34 } else { 35 if env, err := cfg.CurrentEnvironment(environment); err != nil { 36 _, _ = fmt.Fprintln(out, err.Error()) 37 } else { 38 if context != "" { 39 env.Context = context 40 } 41 if namespace != "" { 42 env.Namespace = namespace 43 } 44 45 cmd := fmt.Sprintf("kubectl --context %s --namespace %s", env.Context, env.Namespace) 46 return &cmd 47 } 48 } 49 } 50 51 return nil 52 }