Skip to content

Commit

Permalink
feat: do set key and format=text to set value for single config key
Browse files Browse the repository at this point in the history
  • Loading branch information
mumoshu committed Feb 27, 2020
1 parent a728aec commit 8ac9c53
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 4 deletions.
77 changes: 77 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,83 @@ In addition to everything available via the [native HCL syntax](https://github.c

`config "NAME" {}` is a layered configuration named `NAME`

Let's assume `defaults.yaml` has the following content:

```yaml
api:
endpoint: api.example.com
replicas: 2
```
And a Variant command that contains a job named `thing` that has a `config` block named `conf`:

```hcl
job "thing" {
option "env" {
type = string
}
config "myconf" {
source file {
path = "defaults.yaml"
}
source job {
name = "values"
args = {
env = opt.env
}
}
source job {
name = "value"
args = {
}
key = "key1"
format = "text"
}
}
exec {
command = "echo"
args = list("apiendpoint=${conf.myconf.api.endpoint}, replicas=${conf.myconf.replicas}, env=${conf.myconf.env}, key1=${conf.myconf.key1}")
}
}
job "values" {
option "env" {
type = string
}
exec {
command = "echo"
args = list("env: ${opt.env}")
}
}
job "value" {
exec {
command = "echo"
args = list("val1")
}
}
```

The first source loads a file at `defaults.yaml` and merges the keys and values in it into `conf`.

Similarly, the second source loads keys and values from a job named `values` and merges them into `conf`.

The third sets the value for the key `key1` where the value is the output of a job named `value`.

The configuration `myconf` can be referenced by `${conf.myconf.KEY}` as shown in the `exec` block within the `thing` job above.

Let's run `variant run thing --env prod` and the output should be:

```console
apiendpoint=api.example.com, replicas=2, env=prod, key1=val1
```

#### run

`run` runs a job with args. `run` is available within `job` and `test`.
Expand Down
23 changes: 23 additions & 0 deletions examples/config/config.variant
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@ job "getfoo" {
}
}

job "getbar" {
option "opt1" {
type = string
}
exec {
command = "echo"
args = [
"${opt.opt1}"
]
}
}

job "config view" {
// conf.foo
config "foo" {
Expand Down Expand Up @@ -48,6 +60,17 @@ job "config view" {
}
format = "yaml"
}

source job {
name = "getbar"
args = {
app = opt.app
env = opt.env
opt1 = "OPT1"
}
key = "bar"
format = "text"
}
}

exec {
Expand Down
4 changes: 2 additions & 2 deletions examples/config/config_test.variant
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ test "app deploy" {
exitstatus = 0
err = ""
out = trimspace(<<EOS
{"app":"app1","env":"prod","getfoo":"app1,prod,PARAM1","v":"app1prod"}
{"app":"app1","bar":"OPT1","env":"prod","getfoo":"app1,prod,PARAM1","v":"app1prod"}
EOS
)
}
Expand All @@ -17,7 +17,7 @@ EOS
exitstatus = 0
err = ""
out = trimspace(<<EOS
{"app":"app2","env":"prod","getfoo":"app2,prod,PARAM1","v":"app2"}
{"app":"app2","bar":"OPT1","env":"prod","getfoo":"app2,prod,PARAM1","v":"app2"}
EOS
)
}
Expand Down
39 changes: 37 additions & 2 deletions pkg/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@ import (
"github.com/zclconf/go-cty/cty/gocty"
)

const NoRunMessage = "nothing to run"
const (
NoRunMessage = "nothing to run"

FormatYAML = "yaml"

FormatText = "text"
)

type hcl2Loader struct {
Parser *hcl2parse.Parser
Expand Down Expand Up @@ -107,6 +113,7 @@ type Source struct {
type SourceFile struct {
Path string `hcl:"path,attr"`
Default *string `hcl:"default,attr"`
Key *string `hcl:"key,attr"`
}

type Step struct {
Expand Down Expand Up @@ -150,6 +157,7 @@ type SourceJob struct {
//Arguments map[string]hcl2.Expression `hcl:"args,attr"`
Args hcl2.Expression `hcl:"args,attr"`
Format *string `hcl:"format,attr"`
Key *string `hcl:"key,attr"`
}

type OptionSpec struct {
Expand Down Expand Up @@ -1293,7 +1301,7 @@ func (app *App) getConfigs(confCtx *hcl2.EvalContext, cc *HCL2Config, j JobSpec,

var format string

const FormatYAML = "yaml"
var key string

switch sourceSpec.Type {
case "file":
Expand All @@ -1314,6 +1322,10 @@ func (app *App) getConfigs(confCtx *hcl2.EvalContext, cc *HCL2Config, j JobSpec,
}

format = FormatYAML

if source.Key != nil {
key = *source.Key
}
case "job":
var source SourceJob
if err := gohcl2.DecodeBody(sourceSpec.Body, confCtx, &source); err != nil {
Expand Down Expand Up @@ -1349,6 +1361,10 @@ func (app *App) getConfigs(confCtx *hcl2.EvalContext, cc *HCL2Config, j JobSpec,
} else {
format = FormatYAML
}

if source.Key != nil {
key = *source.Key
}
default:
return cty.DynamicVal, fmt.Errorf("config source %q is not implemented. It must be either \"file\" or \"job\", so that it looks like `source file {` or `source file {`", sourceSpec.Type)
}
Expand All @@ -1360,6 +1376,25 @@ func (app *App) getConfigs(confCtx *hcl2.EvalContext, cc *HCL2Config, j JobSpec,
if err := yaml.Unmarshal(yamlData, &m); err != nil {
return cty.NilVal, err
}
case FormatText:
if key == "" {
return cty.NilVal, fmt.Errorf("`key` must be specified for `text`-formatted source at %d", sourceIdx)
}

keys := strings.Split(key, ".")
lastKeyIndex := len(keys) - 1
intermediateKeys := keys[0:lastKeyIndex]
lastKey := keys[lastKeyIndex]

cur := m

for _, k := range intermediateKeys {
if _, ok := cur[k]; !ok {
cur[k] = map[string]interface{}{}
}
}

cur[lastKey] = string(yamlData)
default:
return cty.NilVal, fmt.Errorf("format %q is not implemented yet. It must be \"yaml\" or omitted", format)
}
Expand Down

0 comments on commit 8ac9c53

Please sign in to comment.