Skip to content

Commit

Permalink
Add --dofile parameter for specifying a Dofile outside the current di…
Browse files Browse the repository at this point in the history
…rectory

Signed-off-by: Alexander Kluth <me@dittusch.dev>
  • Loading branch information
Alexander Kluth committed Aug 21, 2019
1 parent a0e7c44 commit 4f55fcb
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ import (
"os"
"os/exec"
"strings"
"path/filepath"
)

var args struct {
TaskName[] string `arg:"positional"`
Dofile string
}

type Dofile struct {
Expand All @@ -41,7 +43,7 @@ func parseCommand(command string) []string {
return parts
}

func executeTask(doFile Dofile, taskName string) {
func executeTask(doFile Dofile, dirPrefix string, taskName string) {
if _, found := doFile.Tasks[taskName]; found {
fmt.Println(Bold("-> Executing task\t"), Bold(Magenta(taskName)))

Expand All @@ -53,6 +55,7 @@ func executeTask(doFile Dofile, taskName string) {
tokens = remove(tokens, 0)

cmd := exec.Command(cmdName, tokens...)
cmd.Dir = dirPrefix

//if _, err := os.Stat(cmdName); os.IsNotExist(err) {
// fmt.Println()
Expand Down Expand Up @@ -80,7 +83,7 @@ func executeTask(doFile Dofile, taskName string) {
for _, task := range doFile.Tasks[taskName].Tasks {
fmt.Println(Bold(Magenta("-> Executing subtask\t")), Bold(task))

executeTask(doFile, task)
executeTask(doFile, dirPrefix, task)
}
} else {
fmt.Println(Bold(Red("Could not find task")), Bold(Yellow(taskName)), Bold(Red("aborting!")))
Expand All @@ -91,7 +94,15 @@ func executeTask(doFile Dofile, taskName string) {
func main() {
arg.MustParse(&args)

fileContents, err := ioutil.ReadFile("./Dofile")
var fileName = "./Dofile"
var dirPrefix = "./"

if args.Dofile != "" {
fileName = args.Dofile
dirPrefix = filepath.Dir(fileName)
}

fileContents, err := ioutil.ReadFile(fileName)
if err != nil {
log.Fatal(err)
}
Expand All @@ -105,7 +116,7 @@ func main() {
fmt.Println()

for _, taskName := range args.TaskName {
executeTask(doFile, taskName)
executeTask(doFile, dirPrefix, taskName)
}

fmt.Println()
Expand Down

0 comments on commit 4f55fcb

Please sign in to comment.