I am writing a bash task runner in Go which has a simple concept:
- it reads a
Taskfile
, which is a a bash script containing task definitions (simple bash function declarations) - it adds dynamically additional stuff
- Executes a command based on passed arguments
Here is a simplified example:
package main
import (
"fmt"
"os/exec"
)
func main() {
//simplified for a dynamically built script
taskFileContent := "#!/bin/bash\n\ntask:foo (){\n echo \"test\"\n}\n"
// simplified for passed arguments
task := "\ntask:foo"
bash, _ := exec.LookPath("bash")
cmd := exec.Command(bash, "-c", "\"$(cat << EOF\n"+taskFileContent+task+"\nEOF\n)\"")
fmt.Println(cmd.String())
out, _ := cmd.CombinedOutput()
fmt.Println(string(out))
}
My problem now is, that this does not work if it gets executed via Go and I get this error
task:foo: No such file or directory
But it does work if I just execute the generated script directly in the shell like this:
$ /opt/opt/homebrew/bin/bash -c "$(cat << EOF
#!/bin/bash
task:foo (){
echo "test"
}
task:foo
EOF
)"
test <-- printed out from the `task:foo` above
What am I doing wrong here ?