Skip to content

Commit

Permalink
feat: refactor to support variables, cli params
Browse files Browse the repository at this point in the history
  • Loading branch information
Neil Hemming committed Jul 20, 2021
1 parent 84015d4 commit d37b6c0
Show file tree
Hide file tree
Showing 48 changed files with 3,970 additions and 989 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"ironment",
"logcli",
"nehemming",
"nolint"
"nolint",
"testdata"
]
}
719 changes: 719 additions & 0 deletions CREDITS

Large diffs are not rendered by default.

33 changes: 20 additions & 13 deletions build/stdbuild.yml
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,13 @@ stages:
- name: go list modules
type: run
command: 'go list -json -m all'
output: '{{ .artifactsDir }}/list.tmp'
output:
variable: nancy
- name: nancy
type: run
command: 'nancy sleuth --quiet'
input: '{{ .artifactsDir }}/list.tmp'
input:
variable: nancy
logStdOut: true

- name: coverage
Expand All @@ -124,7 +126,8 @@ stages:
- name: credits
type: run
command: 'gocredits'
output: 'CREDITS'
output:
path: 'CREDITS'

# release releases the project using goreleaser
- name: snapshot_release
Expand All @@ -135,20 +138,22 @@ stages:
value: '{{ .resDir }}//Dockerfile.release'
- name: releaseHeader
optional: true
skipTemplate: true
file: '{{ .resDir }}/header.tplt'
skipExpand: true
path: '{{ .resDir }}/header.tplt'
- name: releaseFooter
optional: true
skipTemplate: true
file: '{{ .resDir }}/footer.tplt'
skipExpand: true
path: '{{ .resDir }}/footer.tplt'
tasks:
- name: prepare release script
type: template
delims:
left: '[['
right: ']]'
template: '{{ .resDir }}/goreleaser.ytpl'
output: '{{ .goreleaserCfg }}'
template:
path: '{{ .resDir }}/goreleaser.ytpl'
output:
path: '{{ .goreleaserCfg }}'

- name: go releaser
type: run
Expand All @@ -169,11 +174,11 @@ stages:
value: '{{ .resDir }}/Dockerfile.release'
- name: releaseHeader
optional: true
skipTemplate: true
skipExpand: true
file: '{{ .resDir }}/header.tplt'
- name: releaseFooter
optional: true
skipTemplate: true
skipExpand: true
file: '{{ .resDir }}/footer.tplt'

tasks:
Expand All @@ -182,8 +187,10 @@ stages:
delims:
left: '[['
right: ']]'
template: '{{ .resDir }}/goreleaser.ytpl'
output: '{{ .goreleaserCfg }}'
template:
path: '{{ .resDir }}/goreleaser.ytpl'
output:
path: '{{ .goreleaserCfg }}'

- name: go releaser
type: run
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.16

require (
github.com/apex/log v1.9.0
github.com/hashicorp/go-multierror v1.1.1
github.com/mitchellh/mapstructure v1.4.1
github.com/pkg/errors v0.8.1
github.com/spf13/cobra v1.2.1
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,14 @@ github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORR
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
github.com/hashicorp/consul/api v1.1.0/go.mod h1:VmuI/Lkw1nC05EYQWNKwWGbkg+FbDBtguAZLlVdkD9Q=
github.com/hashicorp/consul/sdk v0.1.1/go.mod h1:VKf9jXwCTEY1QZP2MOLRhb5i/I/ssyNV1vwHyQBF0x8=
github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/go-cleanhttp v0.5.1/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
github.com/hashicorp/go-immutable-radix v1.0.0/go.mod h1:0y9vanUI8NX6FsYoO3zeMjhV/C5i9g4Q3DwcSNZ4P60=
github.com/hashicorp/go-msgpack v0.5.3/go.mod h1:ahLV/dePpqEmjfWmKiqvPkv/twdG7iPBM1vqhUKIvfM=
github.com/hashicorp/go-multierror v1.0.0/go.mod h1:dHtQlpGsu+cZNNAkkCN/P3hoUDHhCYQXV3UM06sGGrk=
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
github.com/hashicorp/go-rootcerts v1.0.0/go.mod h1:K6zTfqpRlCUIjkwsN4Z+hiSfzSTQa6eBIzfwKfwNnHU=
github.com/hashicorp/go-sockaddr v1.0.0/go.mod h1:7Xibr9yA9JjQq1JpNB2Vw7kxv8xerXegt+ozgdvDeDU=
github.com/hashicorp/go-syslog v1.0.0/go.mod h1:qPfqrKkXGihmCqbJM2mZgkZGvKG1dFdvsLplgctolz4=
Expand Down
79 changes: 57 additions & 22 deletions internal/cmd/initcoonfig.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ name: "init sample mission"
version: '1.0'

# sequences can be used to specify what stages are executed. If they are not included all stages are run in file order.
# when oone or more sequences are defined the stages are run in the order of the requested sequence(s).
# when one or more sequences are defined the stages are run in the order of the requested sequence(s).
# in the cas below the command would be: cirocket launch run
# sequences:
# run:
Expand All @@ -19,13 +19,17 @@ version: '1.0'
# params are configuration settings. The are accessible in all settings that support template expansion
# params can be specified as part of the mission, stage or task. Params are inherited and can be overbidden
# all params must have aa name, the key by which they are accessible name: fred is accessible as {{ .fred }} in templates.
# param values can be specified using the value tag or read from a file. Both are subject to template expansion.
# param values can be specified using the value tag or read from a file or web url. Both are subject to template expansion.
# If both value and file are specified the final value is the concatenation of the value property and file contents.
# skipExpand stops template expansion occurring. If optional is true no error will be raised if the file or url is not found
params:
- name: welcome
value: hello world
#- name: secret
# file: somefile.txt
# path: some_file.txt
# url: "https//..."
# skipExpand: false
# optional: false

# env contains environment variables that are defined for use in template expansion or passed to any sub processes executed
# env variables are subject to param expansion, so can for example be passed secrets from params.
Expand Down Expand Up @@ -85,35 +89,60 @@ stages:
command: go
args:
- version
#glob: true
#logStdOut: true
# glob: false
# logStdOut: false

# sub process output is either sent to a file, the log or to the host applications stdout.
# output allows a file to be specified, appendOutput controls if the file should be appended to or truncated
# redirection uses the input, output and error sub keys
# output allows a file or variable to be specified, append controls if the file should be appended to or truncated
# logStdOut sends log output, if not going to a file to the log, otherwise its send to stdout.
# output: filename
# appendOutput: true
# output:
# path: filename
# variable: exported_variable
# append: false
# logStdOut: true

# a input file can be specified in place of stdin
# input: input

# error output normally goes to the log
# it can be directed to a file, merged with the output file or direct to stderr
# error: error_file
# appendError: true
# directStdErr: true
# a input file can be specified in place of stdin, sources include variables, files, urls or inline
# all arguments support template expansion. This can be disabled with the skipExpand arg. The timeout setting
# allows a request timeout limit to be specified. If blank default is 30 seconds.
# input:
# inline: test
# path: file
# url: url
# variable: exported_variable

# error output normally goes to the log, this can be modified using the error sub key
# it can be directed to a file, merged with the output file or direct to the host processes stderr.
# error:
# path: filename
# variable: exported_variable
# append: false
# directStdErr: false

# template tasks are used to run a go template
- name: template_example
type: template

# template input is defined by the template sub key and its output by the output key
# inline templates are not expanded prior to template processing
# template:
# inline: test
# path: file
# url: url
# variable: exported_variable

# output:
# path: filename
# variable: exported_variable
# append: false

# either a template or inline property must be specified.
# template is the name of the go template file
# inline is an inline expansion
#template: file
inline: |
Say {{.welcome}}!
template:
inline: |
Say {{.welcome}}!
# template output is either sent to a file or to the host applications stdout.
# output: filename
Expand All @@ -128,12 +157,18 @@ stages:
# - 'file-1'


# fetch pulls files from a request urls
# fetch pulls data from files, inline statements and urls into exported variables or local files.
- name: fetch_task
type: fetch
# resources:
# - url: 'https://raw.githubusercontent.com/nehemming/cirocket/master/README.md'
# output: testdata/readme.tmp

# - source:
# inline: test
# path: file
# url: url
# variable: exported_variable
# output:
# path: 'testdata/readme.tmp'
# variable: exported_variable
# append: false

# end of file
40 changes: 39 additions & 1 deletion internal/cmd/launch.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,58 @@
package cmd

import (
"fmt"
"strings"

"github.com/nehemming/cirocket/pkg/rocket"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

func getCliParams(cmd *cobra.Command) ([]rocket.Param, error) {
valueParams, err := cmd.Flags().GetStringArray(flagParams)
if err != nil {
return nil, err
}
return parseParams(valueParams)
}

func parseParams(valueParams []string) ([]rocket.Param, error) {
params := make([]rocket.Param, len(valueParams))

for i, nv := range valueParams {
slice := strings.SplitN(nv, "=", 2)

if len(slice) != 2 {
return nil, fmt.Errorf("param[%d] %s is not formed as name=value", i, nv)
}

params[i] = rocket.Param{Name: slice[0], Value: slice[1]}
}

return params, nil
}

func (cli *cli) runFireCmd(cmd *cobra.Command, args []string) error {
// Check that the init process found a config file
if cli.initError != nil {
return cli.initError
}

// Handle params
params, err := getCliParams(cmd)
if err != nil {
return err
}

// Attempt to launch mission
return rocket.Default().LaunchMission(cli.ctx, viper.ConfigFileUsed(), viper.AllSettings(), args...)
return rocket.Default().
LaunchMissionWithParams(cli.ctx, viper.ConfigFileUsed(),
viper.AllSettings(), params, args...)
}

const flagParams = "params"

func (cli *cli) bindLaunchFlagsAndConfig(cmd *cobra.Command) {
cmd.Flags().StringArray(flagParams, nil, "supply parameter values to the mission, multiple params flags can be provided")
}
53 changes: 53 additions & 0 deletions internal/cmd/launch_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package cmd

import "testing"

func TestParseParamsEmpty(t *testing.T) {
var list []string

r, err := parseParams(list)
if err != nil || len(r) > 0 {
t.Error("unexpected", err, len(r))
}
}

func TestParseParamsSingle(t *testing.T) {
list := []string{"abc=123"}

r, err := parseParams(list)
if err != nil || len(r) != 1 {
t.Error("unexpected", err, len(r))
return
}

if r[0].Name != "abc" {
t.Error("unexpected name", r[0].Name)
}
if r[0].Value != "123" {
t.Error("unexpected value", r[0].Name, r[0].Value)
}
}

func TestParseParamsMultiple(t *testing.T) {
list := []string{"abc=123", "def=456,7=8"}

r, err := parseParams(list)
if err != nil || len(r) != 2 {
t.Error("unexpected", err, len(r))
return
}

if r[0].Name != "abc" {
t.Error("unexpected name", r[0].Name)
}
if r[0].Value != "123" {
t.Error("unexpected value", r[0].Name, r[0].Value)
}

if r[1].Name != "def" {
t.Error("unexpected name", r[1].Name)
}
if r[1].Value != "456,7=8" {
t.Error("unexpected value", r[1].Name, r[0].Value)
}
}
5 changes: 5 additions & 0 deletions pkg/loggee/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ func SetLogger(logger Logger) {
mustHaveLogger()
}

func Default() Logger {
mustHaveLogger()
return defaultLog
}

// mustHaveLogger checks that a logger has been set. If there is no logger this method will panic.
func mustHaveLogger() {
if defaultLog == nil {
Expand Down
Loading

0 comments on commit d37b6c0

Please sign in to comment.