Skip to content

Commit

Permalink
fix: allow adding env vars when creating a component
Browse files Browse the repository at this point in the history
This is needed to be able to pass which SB profile to use.
  • Loading branch information
metacosm committed Oct 9, 2019
1 parent f5ebc5e commit ea3039d
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 35 deletions.
57 changes: 57 additions & 0 deletions pkg/cmdutil/env.go
Original file line number Diff line number Diff line change
@@ -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
}
14 changes: 13 additions & 1 deletion pkg/hal/cli/component/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type halkyonRuntime struct {

type createOptions struct {
*cmdutil.CreateOptions
*cmdutil.EnvOptions
runtime string
RV string
exposeP string
Expand Down Expand Up @@ -81,6 +82,7 @@ func (o *createOptions) Build() runtime.Object {
Version: o.RV,
ExposeService: o.expose,
Port: int32(o.port),
Envs: o.Envs,
},
}
}
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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{}
Expand All @@ -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")
Expand All @@ -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 <group id>.<artifact id>)")

cmdutil.SetupEnvOptions(o, cmd)

return cmd
}
45 changes: 11 additions & 34 deletions pkg/hal/cli/link/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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")
}
Expand Down Expand Up @@ -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
}
}

Expand All @@ -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{
Expand All @@ -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,
},
}
}
Expand All @@ -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
}
Expand Down

0 comments on commit ea3039d

Please sign in to comment.