Skip to content

Commit

Permalink
added support to use zeus globals and args in command path fields, ad…
Browse files Browse the repository at this point in the history
…ded support to use env vars in globals
  • Loading branch information
dreadl0ck committed Feb 18, 2021
1 parent 96c62b6 commit 7e2439b
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 31 deletions.
47 changes: 45 additions & 2 deletions arguments.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package main
import (
"bytes"
"errors"
"os"
"reflect"
"strconv"
"strings"
Expand Down Expand Up @@ -289,7 +290,7 @@ func replaceArgs(input string, args map[string]string) (string, error) {
if val, ok := args[n]; ok {
input = strings.ReplaceAll(input, "${"+n+"}", val)
} else {
return "", errors.New("variable in output name is not provided via globals or arguments: ${" + n + "}")
return "", errors.New(input + ": variable is not provided via globals or arguments: ${" + n + "}")
}
}

Expand Down Expand Up @@ -332,10 +333,52 @@ func (c *CommandsFile) replaceGlobals(input string) string {
}

for _, n := range names {
if val, ok := c.Globals[n]; ok {
if val, ok := g.Vars[n]; ok {
input = strings.ReplaceAll(input, "${"+n+"}", val)
}
}

return input
}

func resolveEnvironment(input string) string {
var (
dollar, startOfIdent bool
name string
names []string
)

// replace variables used in ${} notation with global values
for _, char := range input {
if char == '$' {
dollar = true
continue
}
if dollar {
if char == '{' {
startOfIdent = true
continue
}
}
if char == '}' {
// collect string
n := name
names = append(names, n)

// reset state values
name = ""
dollar = false
startOfIdent = false
}
if startOfIdent {
name += string(char)
continue
}
}

for _, n := range names {
input = strings.ReplaceAll(input, "${"+n+"}", os.Getenv(n))
}

return input
}
52 changes: 32 additions & 20 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,22 +138,6 @@ func (c *command) Run(args []string, async bool) error {
return err
}

if c.workingDir != "" {

// handle args in workingDir
finalWorkDir, err := replaceArgs(c.workingDir, argValues)
if err != nil {
return err
}

Log.Debug("moving into workingDir: ", finalWorkDir)

err = os.Chdir(finalWorkDir)
if err != nil {
return err
}
}

// handle dependencies
err = c.execDependencies(argValues)
if err != nil {
Expand Down Expand Up @@ -208,6 +192,22 @@ func (c *command) AtomicRun(argBuffer string, argValues map[string]string, rawAr
}
}

if c.workingDir != "" {

// handle args in workingDir
finalWorkDir, err := replaceArgs(c.workingDir, argValues)
if err != nil {
return err
}

Log.Debug("moving into workingDir: ", finalWorkDir)

err = os.Chdir(finalWorkDir)
if err != nil {
return err
}
}

cLog.WithFields(logrus.Fields{
"prefix": "exec",
"args": rawArgs,
Expand All @@ -218,7 +218,7 @@ func (c *command) AtomicRun(argBuffer string, argValues map[string]string, rawAr
s.Unlock()

// init command
cmd, script, cleanupFunc, err := c.createCommand(argBuffer, rawArgs)
cmd, script, cleanupFunc, err := c.createCommand(argValues, argBuffer, rawArgs)
if err != nil {
return err
}
Expand Down Expand Up @@ -509,7 +509,7 @@ func (c *command) getLanguage() (*Language, error) {

// create an exec.Cmd instance ready for execution
// for the given argument buffer
func (c *command) createCommand(argBuffer string, rawArgs []string) (cmd *exec.Cmd, script string, cleanupFunc func(), err error) {
func (c *command) createCommand(argValues map[string]string, argBuffer string, rawArgs []string) (cmd *exec.Cmd, script string, cleanupFunc func(), err error) {

var (
shellCommand []string
Expand Down Expand Up @@ -584,13 +584,25 @@ func (c *command) createCommand(argBuffer string, rawArgs []string) (cmd *exec.C
}
} else {

var path = c.path
if c.path != "" {

// handle args in path
p, err := replaceArgs(c.path, argValues)
if err != nil {
return nil, "", nil, err
}

path = p
}

if lang.Name == "go" {
// make an exception for golang: invoke the source file directly and pass raw args on the commandline
shellCommand = append(shellCommand, c.path)
shellCommand = append(shellCommand, path)
shellCommand = append(shellCommand, rawArgs...)
} else {

contents, err := ioutil.ReadFile(c.path)
contents, err := ioutil.ReadFile(path)
if err != nil {
Log.Error("failed to read script")
return nil, "", nil, err
Expand Down
27 changes: 26 additions & 1 deletion commandData.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,6 @@ func (d *commandData) init(commandsFile *CommandsFile, name string) error {

// create command
cmd := &command{
path: d.Path,
name: name,
args: args,
description: d.Description,
Expand Down Expand Up @@ -284,6 +283,32 @@ func (d *commandData) init(commandsFile *CommandsFile, name string) error {
extends: d.Extends,
}

if d.Path != "" {
// 1) expand home dir
var p string
for _, v := range []string{"~", "${HOME}", "$HOME"} {
if strings.Contains(d.Path, v) {
u, err := user.Current()
if err != nil {
log.Fatal(err)
}

// replace home dir variable with path
p = strings.Replace(d.Path, v, u.HomeDir, 1)
break
}
}
if p == "" {
p = d.Path
}

// 2) expand ZEUS globals
p = commandsFile.replaceGlobals(p)

// 3) update workingDir
cmd.path = p
}

if d.WorkingDir != "" {

// 1) expand home dir
Expand Down
6 changes: 6 additions & 0 deletions commandsFile.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,12 @@ func parseCommandsFile(path string, flush bool) (*CommandsFile, error) {
Vars: commandsFile.Globals,
}
}

// resolve environment vars used in the globals
// must be in format ${VAR}
for k, v := range g.Vars {
g.Vars[k] = resolveEnvironment(v)
}
}

// initialize commands
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ require (
github.com/smartystreets/assertions v1.0.1 // indirect
github.com/smartystreets/goconvey v1.6.4
github.com/x-cray/logrus-prefixed-formatter v0.5.2
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad // indirect
golang.org/x/sys v0.0.0-20210218085108-9555bcde0c6a // indirect
golang.org/x/crypto v0.0.0-20210218145215-b8e89b74b9df // indirect
golang.org/x/sys v0.0.0-20210218145245-beda7e5e158e // indirect
golang.org/x/term v0.0.0-20201210144234-2321bbc49cbf // indirect
gopkg.in/yaml.v2 v2.4.0
)
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ github.com/valyala/fasttemplate v1.0.1/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPU
github.com/x-cray/logrus-prefixed-formatter v0.5.2 h1:00txxvfBM9muc0jiLIEAkAcIMJzfthRT6usrui8uGmg=
github.com/x-cray/logrus-prefixed-formatter v0.5.2/go.mod h1:2duySbKsL6M18s5GU7VPsoEPHyzalCE06qoARUCeBBE=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad h1:DN0cp81fZ3njFcrLCytUHRSUkqBjfTo4Tx9RJTWs0EY=
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
golang.org/x/crypto v0.0.0-20210218145215-b8e89b74b9df h1:y7QZzfUiTwWam+xBn29Ulb8CBwVN5UdzmMDavl9Whlw=
golang.org/x/crypto v0.0.0-20210218145215-b8e89b74b9df/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd h1:nTDtHvHSdCn1m6ITfMRqtOd/9+7a3s8RBNOZ3eYZzJA=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
Expand All @@ -111,8 +111,8 @@ golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210218085108-9555bcde0c6a h1:+Kiu2GijIw0WaCBk1i7AcqqRx8Xg3HIYaheQazXOu8w=
golang.org/x/sys v0.0.0-20210218085108-9555bcde0c6a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210218145245-beda7e5e158e h1:f5mksnk+hgXHnImpZoWj64ja99j9zV7YUgrVG95uFE4=
golang.org/x/sys v0.0.0-20210218145245-beda7e5e158e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221 h1:/ZHdbVpdR/jk3g30/d4yUL0JU9kksj8+F/bnQUVLGDM=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
golang.org/x/term v0.0.0-20201210144234-2321bbc49cbf h1:MZ2shdL+ZM/XzY3ZGOnh4Nlpnxz5GSOhOmtHo3iPU6M=
Expand Down
2 changes: 1 addition & 1 deletion version.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ package main

// zeus version
// generated with the gen-version command
var version = "0.9.6"
var version = "0.9.7"
2 changes: 1 addition & 1 deletion zeus/commands.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ language: bash
globals:
binaryName: zeus
buildDir: bin
version: 0.9.6
version: 0.9.7

# all commands
# available fields:
Expand Down

0 comments on commit 7e2439b

Please sign in to comment.