Skip to content

Commit

Permalink
add experimental go support
Browse files Browse the repository at this point in the history
  • Loading branch information
dreadl0ck committed Feb 10, 2021
1 parent 11222c7 commit 195bbb6
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 10 deletions.
30 changes: 21 additions & 9 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ func (c *command) AtomicRun(args []string, async bool) error {
}

// init command
cmd, script, cleanupFunc, err := c.createCommand(argBuffer)
cmd, script, cleanupFunc, err := c.createCommand(argBuffer, args)
if err != nil {
return err
}
Expand Down Expand Up @@ -450,7 +450,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) (cmd *exec.Cmd, script string, cleanupFunc func(), err error) {
func (c *command) createCommand(argBuffer string, rawArgs []string) (cmd *exec.Cmd, script string, cleanupFunc func(), err error) {

var (
shellCommand []string
Expand All @@ -474,6 +474,11 @@ func (c *command) createCommand(argBuffer string) (cmd *exec.Cmd, script string,
// add interpreter
shellCommand = append(shellCommand, lang.Interpreter)

// add extra args if set
if len(lang.Args) > 0 {
shellCommand = append(shellCommand, lang.Args...)
}

if stopOnErr && lang.FlagStopOnError != "" {
shellCommand = append(shellCommand, lang.FlagStopOnError)
}
Expand Down Expand Up @@ -520,14 +525,21 @@ func (c *command) createCommand(argBuffer string) (cmd *exec.Cmd, script string,
}
} else {

contents, err := ioutil.ReadFile(c.path)
if err != nil {
Log.Error("failed to read script")
return nil, "", nil, err
}
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, rawArgs...)
} else {

script = lang.Bang + "\n" + globalFuncs + "\n" + argBuffer + "\n" + string(contents)
shellCommand = append(shellCommand, script)
contents, err := ioutil.ReadFile(c.path)
if err != nil {
Log.Error("failed to read script")
return nil, "", nil, err
}

script = lang.Bang + "\n" + globalFuncs + "\n" + argBuffer + "\n" + string(contents)
shellCommand = append(shellCommand, script)
}
}

Log.Debug("shellCommand: ", shellCommand)
Expand Down
4 changes: 4 additions & 0 deletions commandData.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,10 @@ func (d *commandData) init(commandsFile *CommandsFile, name string) error {
useBase: d.UseBase,
}

if lang == "go" && d.Exec != "" {
return errors.New("when using Go, use of the exec field is not allowed. Please a create a file in the scripts folder instead")
}

// replace globals in outputs
for i, o := range cmd.outputs {
out, err := commandsFile.replaceGlobals(o)
Expand Down
18 changes: 18 additions & 0 deletions language.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ var (
"sh": shellLanguage(),
"zsh": zshellLanguage(),
"perl": perlLanguage(),
"go": goLanguage(),
},
}

Expand Down Expand Up @@ -67,6 +68,9 @@ func (langStore *languageStore) getLang(name string) (*Language, error) {
type Language struct {
Name string `yaml:"name"`

// extra arguments
Args []string `yaml:"args"`

// path to Interpreter
Interpreter string `yaml:"interpreter"`

Expand Down Expand Up @@ -234,3 +238,17 @@ func perlLanguage() *Language {
ErrLineNumberSymbol: "line",
}
}

func goLanguage() *Language {
return &Language{
Name: "go",
Interpreter: "go",
Args: []string{"run"},
Comment: "//",
AssignmentOperator: " = ",
VariableKeyword: "var ",
FileExtension: ".go",
CorrectErrLineNumber: true,
ErrLineNumberSymbol: "line",
}
}
2 changes: 1 addition & 1 deletion zeus/data.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# v0.8.10
#

buildNumber: 452
buildNumber: 457
deadline: ""
milestones: []
aliases: {}
Expand Down

0 comments on commit 195bbb6

Please sign in to comment.