Skip to content

Commit

Permalink
added support to use resolve home directory, globals and arguments in…
Browse files Browse the repository at this point in the history
… workingDir
  • Loading branch information
dreadl0ck committed Feb 18, 2021
1 parent 0d767fe commit 96c62b6
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 13 deletions.
22 changes: 15 additions & 7 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,18 +132,26 @@ func (c *command) Run(args []string, async bool) error {
return c.AsyncRun(args)
}

// handle args
argBuffer, argValues, err := c.parseArguments(args)
if err != nil {
return err
}

if c.workingDir != "" {
Log.Debug("moving into workingDir", c.workingDir)
err := os.Chdir(c.workingDir)

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

// handle args
argBuffer, argValues, err := c.parseArguments(args)
if err != nil {
return err
Log.Debug("moving into workingDir: ", finalWorkDir)

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

// handle dependencies
Expand Down
27 changes: 26 additions & 1 deletion commandData.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ package main
import (
"errors"
"io/ioutil"
"log"
"os/user"
"strconv"
"strings"

Expand Down Expand Up @@ -283,7 +285,30 @@ func (d *commandData) init(commandsFile *CommandsFile, name string) error {
}

if d.WorkingDir != "" {
cmd.workingDir = d.WorkingDir

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

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

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

// 3) update workingDir
cmd.workingDir = workDir
}

if lang == "go" && d.Exec != "" {
Expand Down
12 changes: 9 additions & 3 deletions commandsFile.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@ func parseCommandsFile(path string, flush bool) (*CommandsFile, error) {
if flush {
// flush command map
cmdMap.flush()
g = nil
g = &globals{
Vars: make(map[string]string, 0),
}
}

if len(commandsFile.Globals) > 0 {
Expand Down Expand Up @@ -286,14 +288,18 @@ func watchCommandsFile(path, eventID string) {
if err != nil {
// flush command map
cmdMap.flush()
g = nil
g = &globals{
Vars: make(map[string]string, 0),
}
Log.WithError(err).Error("failed to parse commandsFile")
}
} else {
if err != nil {
// flush command map
cmdMap.flush()
g = nil
g = &globals{
Vars: make(map[string]string, 0),
}
// shell is currently busy. store the error to present it to the user once the shell is free again.
lastCommandsFileError = err
} else {
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.5"
var version = "0.9.6"
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.5
version: 0.9.6

# all commands
# available fields:
Expand Down

0 comments on commit 96c62b6

Please sign in to comment.