Skip to content

Commit

Permalink
fix: fix cli params not used; crash on delete missing file
Browse files Browse the repository at this point in the history
  • Loading branch information
Neil Hemming committed Jul 20, 2021
1 parent ed2e5a1 commit ac5f4b2
Show file tree
Hide file tree
Showing 22 changed files with 213 additions and 60 deletions.
4 changes: 2 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
- checkout
- run:
name: "CI Rocket snapshot test on commit"
command: cirocket --config build/circleci.yml launch cicommit
command: cirocket launch cicommit
- run:
name: "Report card"
command: |
Expand All @@ -30,7 +30,7 @@ jobs:
command: |
echo $DOCKER_TOKEN | docker login -u $CIRCLE_PROJECT_USERNAME --password-stdin
echo $GITHUB_TOKEN | docker login ghcr.io -u $CIRCLE_PROJECT_USERNAME --password-stdin
cirocket --config build/circleci.yml launch cirelease
cirocket launch cirelease --param release=yes
workflows:
version: 2
Expand Down
55 changes: 55 additions & 0 deletions .cirocket.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
name: "release the project"
version: '1.0'

includes:
- url: https://raw.githubusercontent.com/nehemming/cirocket-config/master/build/stdbuild.yml

params:

# - name: projectName
# value: cirocket
# - name: description
# value: '<<DESCRIPTION>>'
# - name: builtBy
# value: '<<BUILDER>>'
# - name: repoUser
# value: '<<USER>>'
# - name: license
# value: 'Apache 2.0'

- name: projectName
value: '{{- or .projectName .Env.CIRCLE_PROJECT_REPONAME "cirocket"}}'
print: true

- name: description
value: 'Rocket powered task runner to assist delivering ci build missions'
- name: builtBy
value: '{{- or .builtBy .Runtime.UserName }}'
print: true
- name: repoUser
value: '{{- or .repoUser .Env.CIRCLE_PROJECT_USERNAME "nehemming"}}'
print: true
- name: license
value: 'Apache 2.0'
- name: includePackages
value: yes
- name: includeDocker
value: yes
- name: includeGHCR
value: yes
- name: includeBrews
value: yes
- name: dockerfile
value: 'Dockerfile.release'

# config for the header and footer
- name: resDir
value: build
- name: releaseHeader
optional: true
skipExpand: true
path: '{{ .resDir }}/header.tplt'
- name: releaseFooter
optional: true
skipExpand: true
path: '{{ .resDir }}/footer.tplt'
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"mode": "auto",
"program": "${workspaceRoot}",
"args": [
"launch", "--config", ".cirocket.yml"
"launch", "--config", "aaa/main.yml", "cicommit"

]
}
Expand Down
2 changes: 2 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
"cirocket",
"cliparser",
"ctxhttp",
"delims",
"dockerhub",
"initialise",
"ironment",
"logcli",
Expand Down
File renamed without changes.
12 changes: 12 additions & 0 deletions build.old/Dockerfile.release
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# docker image used by CI builds
FROM alpine:3.13
WORKDIR /opt/app
COPY cirocket .
USER 1000:1000
# executable
ENTRYPOINT [ "./cirocket" ]





File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
8 changes: 4 additions & 4 deletions internal/cmd/launch.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

func getCliParams(cmd *cobra.Command) ([]rocket.Param, error) {
valueParams, err := cmd.Flags().GetStringArray(flagParams)
valueParams, err := cmd.Flags().GetStringArray(flagParam)
if err != nil {
return nil, err
}
Expand All @@ -27,7 +27,7 @@ func parseParams(valueParams []string) ([]rocket.Param, error) {
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]}
params[i] = rocket.Param{Name: slice[0], Value: slice[1], Print: true}
}

return params, nil
Expand All @@ -51,8 +51,8 @@ func (cli *cli) runFireCmd(cmd *cobra.Command, args []string) error {
viper.AllSettings(), params, args...)
}

const flagParams = "params"
const flagParam = "param"

func (cli *cli) bindLaunchFlagsAndConfig(cmd *cobra.Command) {
cmd.Flags().StringArray(flagParams, nil, "supply parameter values to the mission, multiple params flags can be provided")
cmd.Flags().StringArray(flagParam, nil, "supply parameter values to the mission, multiple params flags can be provided")
}
22 changes: 13 additions & 9 deletions pkg/rocket/builtin/cleaner.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,17 @@ func (cleanerType) Prepare(ctx context.Context, capComm *rocket.CapComm, task ro
return nil, errors.Wrap(err, "parsing template type")
}

// Expand files
specs := make([]string, 0, len(cleanCfg.Files))
for index, f := range cleanCfg.Files {
fileSpec, err := capComm.ExpandString(ctx, "file", f)
if err != nil {
return nil, errors.Wrapf(err, "expanding file %d", index)
fn := func(execCtx context.Context) error {
// Expand files
specs := make([]string, 0, len(cleanCfg.Files))
for index, f := range cleanCfg.Files {
fileSpec, err := capComm.ExpandString(ctx, "file", f)
if err != nil {
return errors.Wrapf(err, "expanding file %d", index)
}
specs = append(specs, fileSpec)
}
specs = append(specs, fileSpec)
}

fn := func(execCtx context.Context) error {
// glob the files
files, err := globFile(specs)
if err != nil {
Expand Down Expand Up @@ -82,6 +82,10 @@ func deleteFiles(files []string, log bool) error {
return errors.Wrapf(err, "stat %s:", file)
}

if stat == nil {
continue
}

if stat.IsDir() {
err = os.RemoveAll(file)
} else {
Expand Down
9 changes: 9 additions & 0 deletions pkg/rocket/builtin/cleaner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,12 @@ func TestCleanerRun(t *testing.T) {

validateCleanerTest(t)
}

func TestDeleteMissing(t *testing.T) {
files := []string{"filenotexists.gogo"}

err := deleteFiles(files, false)
if err != nil {
t.Error("unexpected", err)
}
}
23 changes: 11 additions & 12 deletions pkg/rocket/builtin/mkdir.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,20 @@ func (mkDirType) Prepare(ctx context.Context, capComm *rocket.CapComm, task rock
return nil, errors.Wrap(err, "parsing template type")
}

// Expand directories
directories := make([]string, 0, len(mkDirCfg.Dirs))
for index, f := range mkDirCfg.Dirs {
dir, err := capComm.ExpandString(ctx, "dir", f)
if err != nil {
return nil, errors.Wrapf(err, "expanding dir %d", index)
}

directories = append(directories, dir)
}

fn := func(execCtx context.Context) error {
// Expand directories
directories := make([]string, 0, len(mkDirCfg.Dirs))
for index, f := range mkDirCfg.Dirs {
dir, err := capComm.ExpandString(ctx, "dir", f)
if err != nil {
return errors.Wrapf(err, "expanding dir %d", index)
}

directories = append(directories, dir)
}
// create
for _, dir := range directories {
err := os.MkdirAll(dir, 0o777)
err := os.MkdirAll(dir, 0777)
if err != nil {
return errors.Wrapf(err, "mkdir -p %s:", dir)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/rocket/builtin/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func TestRunGoWithError(t *testing.T) {

if err := mc.LaunchMission(context.Background(), cfgFile, mission); err == nil {
t.Error("Run go mission no error")
} else if err.Error() != "stage testing: task run go with error: process go exit code 2" {
} else if err.Error() != "stage: testing: task: run go with error: process go exit code 2" {
t.Error("Run go mission failure unknown error", err)
}
}
Expand Down
77 changes: 56 additions & 21 deletions pkg/rocket/capcomm.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io"
"net/http"
"os"
"os/user"
"path/filepath"
"runtime"
"strings"
Expand Down Expand Up @@ -63,6 +64,9 @@ type (
GOOS string
// GOARCH is the architecture the host application is running against
GOARCH string

// UserName runtime username
UserName string
}

// CapComm handles all communication between mission control and the mission stages and tasks.
Expand All @@ -86,12 +90,12 @@ type (
)

// setParamsFromConfigFile adds config file entries to the environment map.
func setParamsFromConfigFile(env map[string]string, configFile string) {
func setParamsFromConfigFile(params map[string]string, configFile string) {
dir, file := filepath.Split(configFile)
env[ConfigFileFullPath], _ = filepath.Abs(configFile)
env[ConfigFile] = file
env[ConfigBaseName] = strings.TrimSuffix(file, filepath.Ext(file))
env[ConfigDir] = strings.TrimSuffix(dir, string(filepath.Separator))
params[ConfigFileFullPath], _ = filepath.Abs(configFile)
params[ConfigFile] = file
params[ConfigBaseName] = strings.TrimSuffix(file, filepath.Ext(file))
params[ConfigDir] = strings.TrimSuffix(dir, string(filepath.Separator))
}

func initFuncMap() template.FuncMap {
Expand All @@ -114,6 +118,22 @@ func initFuncMap() template.FuncMap {
return fm
}

func getUserName() string {
if u, err := user.Current(); err == nil {
if u.Name != "" {
return u.Name
}
if u.Username != "" {
return u.Username
}
if u.Uid != "" {
return u.Uid
}
}

return "unknown"
}

// newCapCommFromEnvironment creates a new capCom from the environment.
func newCapCommFromEnvironment(configFile string, log loggee.Logger) *CapComm {
paramKvg := NewKeyValueGetter(nil)
Expand All @@ -126,8 +146,9 @@ func newCapCommFromEnvironment(configFile string, log loggee.Logger) *CapComm {
params: paramKvg,
funcMap: initFuncMap(),
runtime: Runtime{
GOOS: runtime.GOOS,
GOARCH: runtime.GOARCH,
GOOS: runtime.GOOS,
GOARCH: runtime.GOARCH,
UserName: getUserName(),
},
resources: make(providers.ResourceProviderMap),
variables: make(exportMap),
Expand Down Expand Up @@ -156,14 +177,11 @@ func (capComm *CapComm) Copy(noTrust bool) *CapComm {
additionalMissionData: capComm.additionalMissionData, // no copy, but safe as set once
funcMap: make(template.FuncMap),
params: NewKeyValueGetter(capComm.params),
runtime: Runtime{
GOOS: runtime.GOOS,
GOARCH: runtime.GOARCH,
},
resources: capComm.resources.Copy(),
variables: make(exportMap),
exportTo: capComm.variables,
log: capComm.log,
runtime: capComm.runtime,
resources: capComm.resources.Copy(),
variables: make(exportMap),
exportTo: capComm.variables,
log: capComm.log,
}

// Non trusted CapComm copies do not receive environment variables from their parent
Expand Down Expand Up @@ -719,8 +737,7 @@ func getParamFromURL(ctx context.Context, url string, optional bool) (string, er
return string(body), nil
}

// expandParam carries out template expansion of a parameter.
func (capComm *CapComm) expandParam(ctx context.Context, param Param) (string, error) {
func (capComm *CapComm) getParamValue(ctx context.Context, param Param) (string, error) {
// Read param
value := param.Value

Expand Down Expand Up @@ -751,12 +768,30 @@ func (capComm *CapComm) expandParam(ctx context.Context, param Param) (string, e
value += body
}

// Skip expanding a param
if param.SkipExpand {
return value, nil
return value, nil
}

// expandParam carries out template expansion of a parameter.
func (capComm *CapComm) expandParam(ctx context.Context, param Param) (string, error) {
value, err := capComm.getParamValue(ctx, param)
if err != nil {
return "", err
}

if !param.SkipExpand {
// Expand
value, err = capComm.ExpandString(ctx, param.Name, value)
if err != nil {
return "", err
}
}

// Print?
if param.Print {
capComm.log.WithField("value", value).Infof("param: %s", param.Name)
}

return capComm.ExpandString(ctx, param.Name, value)
return value, nil
}

// isFiltered returns true if the filter restricts
Expand Down
1 change: 1 addition & 0 deletions pkg/rocket/capcomm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1460,6 +1460,7 @@ func TestExpandParam(t *testing.T) {
{
Name: "valueTest",
Value: "1234",
Print: true,
},
{
Name: "valueTest",
Expand Down
3 changes: 3 additions & 0 deletions pkg/rocket/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ type (

// Optional if true allows the file not to exist
Optional bool `mapstructure:"optional"`

// Print if true will display the value of the parameter once expanded to the log
Print bool `mapstructure:"print"`
}

// EnvMap is a map of environment variables to their values.
Expand Down
Loading

0 comments on commit ac5f4b2

Please sign in to comment.