From a95d054189a5a0307ce6238ff99cbcff855cc02f Mon Sep 17 00:00:00 2001 From: Arsen Musayelyan Date: Mon, 1 Mar 2021 15:01:21 -0800 Subject: [PATCH] Add comments and remove debug code --- ast.go | 12 ++++++++++++ defaults.go | 25 ++++++++++++++++++++----- scpt.go | 39 +++++++++++++++++++++++++++++++++++++-- 3 files changed, 69 insertions(+), 7 deletions(-) diff --git a/ast.go b/ast.go index 61900b1..2d035e0 100644 --- a/ast.go +++ b/ast.go @@ -15,25 +15,36 @@ type AST struct { // containing the position at which the error was encountered and the error // itself func (ast *AST) Execute() error { + // For each command in AST for _, cmd := range ast.Commands { + // If parsing variables if cmd.Vars != nil { + // For each variable for _, Var := range cmd.Vars { + // Parse value of variable val, err := ParseValue(Var.Value) if err != nil { return fmt.Errorf("%s: %s", Var.Value.Pos, err) } + // If value of variable is a function call if IsFuncCall(val) { + // Assert type of val as *FuncCall Call := val.(*FuncCall) + // Set variable value to function return value Vars[Var.Key], err = CallFunction(Call) if err != nil { return fmt.Errorf("%s: %s", Var.Value.Pos, err) } } else { + // If value is not a function call, set variable value to parsed value Vars[Var.Key] = val } } + // If parsing function calls } else if cmd.Calls != nil { + // For each function call for _, Call := range cmd.Calls { + // Attempt to call function _, err := CallFunction(Call) if err != nil { return fmt.Errorf("%s: %s", Call.Pos, err) @@ -91,6 +102,7 @@ type Bool bool // Capture parses a boolean literal encountered in the script into // a Go boolean value func (b *Bool) Capture(values []string) error { + // Convert string to boolean *b = values[0] == "true" return nil } diff --git a/defaults.go b/defaults.go index 2aba936..908cd9e 100644 --- a/defaults.go +++ b/defaults.go @@ -8,47 +8,62 @@ import ( "os/exec" ) +// Default function to display a dialog func displayDialog(args map[string]interface{}) (interface{}, error) { + // Get title title, ok := args["title"] if !ok { return nil, errors.New("title not provided") } + // Get unnamed argument as text text, ok := args[""] if !ok { return nil, errors.New("text not provided") } + // Display correct dialog based on given type switch args["type"] { case "yesno": + // Display yes or no dialog, returning bool based on user input return dlgs.Question(fmt.Sprint(title), fmt.Sprint(text), true) case "info": + // Display info dialog, returning bool based on success return dlgs.Info(fmt.Sprint(title), fmt.Sprint(text)) case "error": + // Display error dialog, returning bool based on success return dlgs.Error(fmt.Sprint(title), fmt.Sprint(text)) case "entry": + // Check if default text given defaultText, ok := args["default"] if !ok { + // Set to empty if not given defaultText = "" } + // Display entry dialog input, _, err := dlgs.Entry(fmt.Sprint(title), fmt.Sprint(text), fmt.Sprint(defaultText)) + // Return user input return input, err default: + // If type unknown, return error return nil, fmt.Errorf("unknown dialog type: %v", args["type"]) } } +// Default function to run a shell script using `sh -c` func doShellScript(args map[string]interface{}) (interface{}, error) { + // Get unnamed argument and assert its type as string script, ok := args[""].(string) + // If assertion successful if ok { + // Create new exec.Cmd containing `sh -c