From 875c7bebd1cf024dac4e3737d614c133bf9d74ae Mon Sep 17 00:00:00 2001 From: Andy Maloney Date: Wed, 9 Aug 2023 16:32:26 -0400 Subject: [PATCH] When debug is on, output the commands used to run frameworks (#370) --- amod/amod.go | 3 +++ util/executil/executil.go | 23 +++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/amod/amod.go b/amod/amod.go index 07ad6fc..1680245 100644 --- a/amod/amod.go +++ b/amod/amod.go @@ -16,6 +16,7 @@ import ( "github.com/asmaloney/gactar/actr/modules" "github.com/asmaloney/gactar/actr/param" + "github.com/asmaloney/gactar/util/executil" "github.com/asmaloney/gactar/util/issues" "github.com/asmaloney/gactar/util/keyvalue" ) @@ -39,6 +40,8 @@ func (e ErrParseChunk) Error() string { // SetDebug turns debugging on and off. This will output the tokens as they are generated. func SetDebug(debug bool) { debugging = debug + + executil.SetDebug(debug) } // OutputEBNF outputs the extended Backus–Naur form (EBNF) of the amod grammar to stdout. diff --git a/util/executil/executil.go b/util/executil/executil.go index 00aef83..5136eb1 100644 --- a/util/executil/executil.go +++ b/util/executil/executil.go @@ -6,6 +6,10 @@ import ( "os/exec" ) +var ( + debugging bool = false +) + type ErrExecuteCommand struct { Output string } @@ -16,8 +20,22 @@ func (e ErrExecuteCommand) Error() string { func ExecCommand(name string, arg ...string) (output string, err error) { cmd := exec.Command(name, arg...) + + if debugging { + fmt.Printf("Executing: %s\n", cmd.String()) + } + outputBytes, err := cmd.CombinedOutput() output = string(outputBytes) + + if debugging { + if err != nil { + fmt.Printf("Exec FAIL: %s\n%s\n", cmd.String(), output) + } else { + fmt.Printf("Exec SUCCESS: %s\n", cmd.String()) + } + } + if err != nil { err = &ErrExecuteCommand{Output: output} return @@ -25,3 +43,8 @@ func ExecCommand(name string, arg ...string) (output string, err error) { return } + +// SetDebug turns debugging on and off. This will output the command before executing it. +func SetDebug(debug bool) { + debugging = debug +}