Skip to content

Commit

Permalink
Add ability to set environment variables via CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
jianyuan committed Feb 4, 2019
1 parent 569430f commit 18f06f6
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 10 deletions.
2 changes: 1 addition & 1 deletion cmd_env.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var commandEnv = cli.Command{
var err error

proc := NewProcess("env")
proc.AppendConfigSource(configSourceFromContext(c))
proc.AppendConfigSource(configSourcesFromContext(c)...)

err = proc.Start()
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ var commandRun = cli.Command{
}

proc := NewProcess(c.Args().First(), c.Args().Tail()...)
proc.AppendConfigSource(configSourceFromContext(c))
proc.AppendConfigSource(configSourcesFromContext(c)...)

err = proc.Start()
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd_start.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ var commandStart = cli.Command{
}

proc := NewProcess(command[0], command[1:]...)
proc.AppendConfigSource(configSourceFromContext(c))
proc.AppendConfigSource(configSourcesFromContext(c)...)

err = proc.Start()
if err != nil {
Expand Down
12 changes: 8 additions & 4 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,16 @@ type ConfigSource interface {

const metadataConfigSourceKey = "configSource"

func setConfigSource(c *cli.Context, src ConfigSource) {
c.App.Metadata[metadataConfigSourceKey] = src
func appendConfigSource(c *cli.Context, src ConfigSource) {
m, ok := c.App.Metadata[metadataConfigSourceKey].([]ConfigSource)
if !ok {
m = []ConfigSource{}
}
c.App.Metadata[metadataConfigSourceKey] = append(m, src)
}

func configSourceFromContext(c *cli.Context) ConfigSource {
if src, ok := c.App.Metadata[metadataConfigSourceKey].(ConfigSource); ok {
func configSourcesFromContext(c *cli.Context) []ConfigSource {
if src, ok := c.App.Metadata[metadataConfigSourceKey].([]ConfigSource); ok {
return src
}
return nil
Expand Down
19 changes: 19 additions & 0 deletions config_stringslice.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package main

import "context"

type StringSliceConfigSource struct {
Config []string
}

func NewStringSliceConfigSource(config []string) *StringSliceConfigSource {
src := &StringSliceConfigSource{
Config: make([]string, len(config)),
}
copy(src.Config, config)
return src
}

func (src *StringSliceConfigSource) List(ctx context.Context) ([]string, error) {
return src.Config, nil
}
12 changes: 11 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,13 @@ func main() {
Name: "config-table",
EnvVar: "SIDEKICK_CONFIG_TABLE",
},
cli.StringSliceFlag{
Name: "env, e",
Usage: "set environment variables",
},
}
app.Before = func(c *cli.Context) error {
// TODO: support multiple config sources
var configSource ConfigSource
switch c.String("config-source") {
case "dynamodb":
Expand All @@ -32,7 +37,12 @@ func main() {
return cli.NewExitError("couldn't find that config source type", 2)
}

setConfigSource(c, configSource)
appendConfigSource(c, configSource)

envs := c.StringSlice("env")
if len(envs) > 0 {
appendConfigSource(c, NewStringSliceConfigSource(envs))
}

return nil
}
Expand Down
4 changes: 2 additions & 2 deletions process.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ func NewProcess(name string, arg ...string) *Process {
return p
}

func (p *Process) AppendConfigSource(src ConfigSource) {
p.configSources = append(p.configSources, src)
func (p *Process) AppendConfigSource(sources ...ConfigSource) {
p.configSources = append(p.configSources, sources...)
}

func (p *Process) resetAndInstallEnv() error {
Expand Down

0 comments on commit 18f06f6

Please sign in to comment.