Skip to content

Commit

Permalink
fix: error dump creation might fail if workingDir was changed, always…
Browse files Browse the repository at this point in the history
… use project root to create it in the correct path, extended unit tests
  • Loading branch information
dreadl0ck committed Jun 28, 2023
1 parent c1666e1 commit d154eb2
Show file tree
Hide file tree
Showing 12 changed files with 212 additions and 140 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ tests/zeus/.history
debug
.vscode
tests/zeus/dumps
zeus/data.yml
zeus/data.yml
.DS_Store
.idea/
3 changes: 3 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ var (
// path for project config file
projectConfigPath string

// directory of the current project, set on startup
projectDir string

// path for project config files
zeusDir = "zeus"

Expand Down
7 changes: 5 additions & 2 deletions generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,17 @@ func handleGenerateCommand(args []string) {
outputName = zeusDir + "/generated/" + args[1]
)

// check if its a valid command chain
// check if it's a valid command chain
if chain, ok = validCommandChain(args[2:], false); !ok {
l.Println("invalid command chain")
return
}

// make sure generated dir exists
os.Mkdir(zeusDir+"/generated", 0744)
err := os.Mkdir(zeusDir+"/generated", 0744)
if err != nil && err != os.ErrExist {
l.Println("failed to create generated directory:", err)
}

// check if it contains multiple languages
for _, cmd := range chain {
Expand Down
242 changes: 121 additions & 121 deletions rice-box.go

Large diffs are not rendered by default.

23 changes: 23 additions & 0 deletions tests/zeus/commands.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ globals:
# async # bool # detach comamnd async in a screen session, attach on demand
# path # string # custom path for script file
# exec # string # supply the script directly without a file
# hidden # bool # hidden controls if the command is shown the menu
# canModifyPrompt # bool # canModifyPrompt controls if the command can modify the zeus prompt
# extends # string # extends sets the base configuration to use for this command
# workingDir # string # workingDir overwrites the working directory for this command
commands:

# multi language examples
Expand Down Expand Up @@ -96,6 +100,25 @@ commands:
description: a perl script
language: perl

go:
description: a zeus command implemented in Go
language: go
arguments:
- url:String
# zeus is usually started from the root of your project
# to find the go program to compile and execute, provide the relative path starting from your project root.
path: zeus/go/goprogram/main.go

go-custom:
description: a zeus command implemented in Go with custom workingDir
language: go
arguments:
- url:String
# set a custom location for working directory
workingDir: zeus/go/goprogram
# path to your entrypoint
path: main.go

# examples
#

Expand Down
2 changes: 1 addition & 1 deletion tests/zeus/data.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# /_____ \\___ >____//____ >
# \/ \/ \/
# Build System
# v0.9.11
# v0.9.12
#

buildNumber: 3
Expand Down
16 changes: 16 additions & 0 deletions tests/zeus/go/goprogram/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package main

import (
"fmt"
"github.com/dreadl0ck/zeus/zeusutils"
"log"
)

func main() {
urlValue := zeusutils.LoadArg("url")
if urlValue == "" {
log.Fatal("url value is empty")
}

fmt.Println("got url", urlValue)
}
30 changes: 18 additions & 12 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,22 @@ var (
// dump the currently executed script to disk
func dumpScript(script, language string, e error, stdErr string) {

stat, err := os.Stat(zeusDir + "/dumps")
pathZeusDir := filepath.Join(projectDir, zeusDir)

stat, err := os.Stat(pathZeusDir + "/dumps")
if err != nil {
err := os.Mkdir(zeusDir+"/dumps", 0700)
err := os.Mkdir(pathZeusDir+"/dumps", 0700)
if err != nil {
Log.WithError(err).Error("failed to create dumps directory")
wd, _ := os.Getwd()
Log.WithError(err).
WithField("workingDir", wd).
WithField("projectDir", projectDir).
Error("failed to create dumps directory")
return
}
} else {
if !stat.IsDir() {
Log.Error("dumpScript: " + zeusDir + "/dumps is a file")
Log.Error("dumpScript: " + pathZeusDir + "/dumps is a file")
return
}
}
Expand All @@ -84,21 +90,21 @@ func dumpScript(script, language string, e error, stdErr string) {
var (
t = lang.Comment + " Timestamp: " + time.Now().Format(timestampFormat) + "\n"
errString = lang.Comment + " Error: " + e.Error() + "\n" + lang.Comment + " StdErr: \n" + stdErrOutputComment + "\n\n"
dumpFileName = zeusDir + "/dumps/error_dump" + lang.FileExtension
dumpFileName = pathZeusDir + "/dumps/error_dump" + lang.FileExtension
)

f, err := os.OpenFile(dumpFileName, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0700)
fd, err := os.OpenFile(dumpFileName, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0700)
if err != nil {
Log.WithError(err).Error("failed to open dump file")
return
}
defer f.Close()
defer fd.Close()

f.WriteString(lang.Bang + "\n" + lang.Comment + "\n")
f.WriteString(lang.Comment + " ZEUS Error Dump\n")
f.WriteString(t)
f.WriteString(errString)
f.WriteString(script)
fd.WriteString(lang.Bang + "\n" + lang.Comment + "\n")
fd.WriteString(lang.Comment + " ZEUS Error Dump\n")
fd.WriteString(t)
fd.WriteString(errString)
fd.WriteString(script)
Log.Debug("script dumped: ", dumpFileName)
}

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.11"
var version = "0.9.12"
5 changes: 4 additions & 1 deletion wiki/docs/TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
COMMIT:
------------------------------------------------------------------------------------

## v0.8.5
## TODO

- feedback jan path completion and shell passthrough behavior
- remove go rice and use go:embed instead

- globals should start with an uppercase letter
- fix argument / chain / path completion
Expand Down
12 changes: 12 additions & 0 deletions zeus.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,18 @@ func initZeus() {
if err != nil {
log.Fatal("failed to change dir: ", err)
}

fullPath, err := os.Getwd()
if err != nil {
log.Fatal("failed to obtain full path of current working directory: ", err)
}

projectDir = fullPath
} else {
projectDir, err = os.Getwd()
if err != nil {
log.Fatal("failed to set project directory on startup: ", err)
}
}

if *flagCompletions != "" {
Expand Down
6 changes: 5 additions & 1 deletion zeus/commands.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ globals:
# path # string # custom path for script file
# exec # string # supply the script directly without a file
# language # string # set the language for the script
# hidden # bool # hidden controls if the command is shown the menu
# canModifyPrompt # bool # canModifyPrompt controls if the command can modify the zeus prompt
# extends # string # extends sets the base configuration to use for this command
# workingDir # string # workingDir overwrites the working directory for this command
commands:
# Utils
#
Expand All @@ -55,7 +59,7 @@ commands:
cp -f README.md wiki/docs
# @todo: release the tools...
#echo "[ZEUS v${version}] minifying javscript and css"
#echo "[ZEUS v${version}] minifying javascript and css"
#jsobfus -d frontend/src/js/:frontend/dist/js
#sasscompile -d frontend/src/sass:frontend/dist/css
Expand Down

0 comments on commit d154eb2

Please sign in to comment.