diff --git a/pkg/cmdutil/env.go b/pkg/cmdutil/env.go new file mode 100644 index 0000000..1671363 --- /dev/null +++ b/pkg/cmdutil/env.go @@ -0,0 +1,57 @@ +package cmdutil + +import ( + "fmt" + "github.com/spf13/cobra" + halkyon "halkyon.io/api/v1beta1" + "halkyon.io/hal/pkg/ui" + "strings" +) + +type WithEnv interface { + SetEnvOptions(o *EnvOptions) +} + +type EnvOptions struct { + EnvPairs []string + Envs []halkyon.NameValuePair +} + +func SetupEnvOptions(o WithEnv, cmd *cobra.Command) { + env := &EnvOptions{} + o.SetEnvOptions(env) + cmd.Flags().StringSliceVarP(&env.EnvPairs, "env", "e", []string{}, "Environment variables as 'name=value' pairs") +} + +func (o *EnvOptions) Complete() error { + if len(o.EnvPairs) > 0 { + for _, pair := range o.EnvPairs { + if _, e := o.addToEnv(pair); e != nil { + return e + } + } + } else { + for { + envAsString := ui.AskOrReturnToExit("Env variable in the 'name=value' format, simply press enter when finished") + if len(envAsString) == 0 { + break + } + if _, e := o.addToEnv(envAsString); e != nil { + return e + } + } + } + return nil +} + +func (o *EnvOptions) addToEnv(pair string) (halkyon.NameValuePair, error) { + // todo: extract as generic version + split := strings.Split(pair, "=") + if len(split) != 2 { + return halkyon.NameValuePair{}, fmt.Errorf("invalid environment variable: %s, format must be 'name=value'", pair) + } + env := halkyon.NameValuePair{Name: split[0], Value: split[1]} + o.Envs = append(o.Envs, env) + ui.OutputSelection("Set env variable", fmt.Sprintf("%s=%s", env.Name, env.Value)) + return env, nil +} diff --git a/pkg/hal/cli/component/create.go b/pkg/hal/cli/component/create.go index 215421a..12de082 100644 --- a/pkg/hal/cli/component/create.go +++ b/pkg/hal/cli/component/create.go @@ -45,6 +45,7 @@ type halkyonRuntime struct { type createOptions struct { *cmdutil.CreateOptions + *cmdutil.EnvOptions runtime string RV string exposeP string @@ -81,6 +82,7 @@ func (o *createOptions) Build() runtime.Object { Version: o.RV, ExposeService: o.expose, Port: int32(o.port), + Envs: o.Envs, }, } } @@ -151,6 +153,10 @@ func (o *createOptions) Complete(name string, cmd *cobra.Command, args []string) } } + if err := o.EnvOptions.Complete(); err != nil { + return err + } + return nil } @@ -234,6 +240,10 @@ func getRuntimeNames() []string { return result } +func (o *createOptions) SetEnvOptions(env *cmdutil.EnvOptions) { + o.EnvOptions = env +} + func NewCmdCreate(fullParentName string) *cobra.Command { c := k8s.GetClient() o := &createOptions{} @@ -248,7 +258,7 @@ func NewCmdCreate(fullParentName string) *cobra.Command { cmd.Flags().StringVarP(&o.runtime, "runtime", "r", "", "Runtime to use for the component. Possible values:"+strings.Join(getRuntimeNames(), ",")) cmd.Flags().StringVarP(&o.RV, "runtimeVersion", "i", "", "Runtime version") - cmd.Flags().StringVarP(&o.exposeP, "expose", "e", "", "Whether or not to expose the microservice outside of the cluster") + cmd.Flags().StringVarP(&o.exposeP, "expose", "x", "", "Whether or not to expose the microservice outside of the cluster") cmd.Flags().IntVarP(&o.port, "port", "o", 0, "Port the microservice listens on") cmd.Flags().StringVarP(&o.scaffoldP, "scaffold", "s", "", "Use code generator to scaffold the component") cmd.Flags().StringVarP(&o.G, "groupid", "g", "", "Maven group id e.g. com.example") @@ -257,5 +267,7 @@ func NewCmdCreate(fullParentName string) *cobra.Command { cmd.Flags().StringVarP(&o.Template, "template", "t", "rest", "Template name used to select the project to be created, only supported for Spring Boot") cmd.Flags().StringVarP(&o.P, "packagename", "p", "", "Package name (defaults to .)") + cmdutil.SetupEnvOptions(o, cmd) + return cmd } diff --git a/pkg/hal/cli/link/create.go b/pkg/hal/cli/link/create.go index a1c3136..3444acc 100644 --- a/pkg/hal/cli/link/create.go +++ b/pkg/hal/cli/link/create.go @@ -4,7 +4,6 @@ import ( "fmt" "github.com/spf13/cobra" link "halkyon.io/api/link/v1beta1" - halkyon "halkyon.io/api/v1beta1" "halkyon.io/hal/pkg/cmdutil" "halkyon.io/hal/pkg/k8s" "halkyon.io/hal/pkg/ui" @@ -22,16 +21,19 @@ const ( type createOptions struct { targetName string secret string - envPairs []string - envs []halkyon.NameValuePair linkType link.LinkType *cmdutil.CreateOptions + *cmdutil.EnvOptions +} + +func (o *createOptions) SetEnvOptions(env *cmdutil.EnvOptions) { + o.EnvOptions = env } func (o *createOptions) Complete(name string, cmd *cobra.Command, args []string) error { // first check if proper parameters combination are provided useSecret := len(o.secret) > 0 - useEnv := len(o.envPairs) > 0 + useEnv := len(o.EnvPairs) > 0 if useSecret && useEnv { return fmt.Errorf("invalid parameter combination: either pass a secret name or environment variables, not both") } @@ -73,22 +75,8 @@ func (o *createOptions) Complete(name string, cmd *cobra.Command, args []string) } else { o.linkType = link.EnvLinkType ui.OutputSelection("Selected link type", o.linkType.String()) - if useEnv { - for _, pair := range o.envPairs { - if _, e := o.addToEnv(pair); e != nil { - return e - } - } - } else { - for { - envAsString := ui.AskOrReturnToExit("Env variable in the 'name=value' format, simply press enter when finished") - if len(envAsString) == 0 { - break - } - if _, e := o.addToEnv(envAsString); e != nil { - return e - } - } + if err := o.EnvOptions.Complete(); err != nil { + return err } } @@ -99,18 +87,6 @@ func (o *createOptions) Validate() error { return nil } -func (o *createOptions) addToEnv(pair string) (halkyon.NameValuePair, error) { - // todo: extract as generic version - split := strings.Split(pair, "=") - if len(split) != 2 { - return halkyon.NameValuePair{}, fmt.Errorf("invalid environment variable: %s, format must be 'name=value'", pair) - } - env := halkyon.NameValuePair{Name: split[0], Value: split[1]} - o.envs = append(o.envs, env) - ui.OutputSelection("Set env variable", fmt.Sprintf("%s=%s", env.Name, env.Value)) - return env, nil -} - func (o *createOptions) Build() runtime.Object { return &link.Link{ ObjectMeta: v1.ObjectMeta{ @@ -121,7 +97,7 @@ func (o *createOptions) Build() runtime.Object { ComponentName: o.targetName, Type: o.linkType, Ref: o.secret, - Envs: o.envs, + Envs: o.Envs, }, } } @@ -145,7 +121,8 @@ func NewCmdCreate(parent string) *cobra.Command { l.Flags().StringVarP(&o.targetName, "target", "t", "", "Name of the component or capability to link to") l.Flags().StringVarP(&o.secret, "secret", "s", "", "Secret name to reference if using Secret type") - l.Flags().StringSliceVarP(&o.envPairs, "env", "e", []string{}, "Environment variables as 'name=value' pairs") + + cmdutil.SetupEnvOptions(o, l) return l }