From bd10d0d39011b35a265cbb0c26541f90f9693efe Mon Sep 17 00:00:00 2001 From: shikaan Date: Fri, 22 Dec 2023 01:41:55 +0100 Subject: [PATCH 1/3] feat: add support for script dependencies --- .vscode/launch.json | 15 ++ main.go | 19 ++- pkg/exceptions/exceptions.go | 15 +- pkg/scripts/help.go | 28 ++++ pkg/scripts/help_test.go | 27 ++++ pkg/scripts/{scripts.go => parse.go} | 114 ++++++--------- pkg/scripts/parse_test.go | 204 +++++++++++++++++++++++++++ pkg/scripts/run.go | 69 +++++++++ pkg/scripts/run_test.go | 28 ++++ pkg/scripts/scripts_test.go | 178 ----------------------- shmuxfile.sh | 5 +- test_shmuxfile.sh | 11 ++ 12 files changed, 439 insertions(+), 274 deletions(-) create mode 100644 .vscode/launch.json create mode 100644 pkg/scripts/help.go create mode 100644 pkg/scripts/help_test.go rename pkg/scripts/{scripts.go => parse.go} (54%) create mode 100644 pkg/scripts/parse_test.go create mode 100644 pkg/scripts/run.go create mode 100644 pkg/scripts/run_test.go delete mode 100644 pkg/scripts/scripts_test.go create mode 100644 test_shmuxfile.sh diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..e63a647 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,15 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Launch Package", + "type": "go", + "request": "launch", + "mode": "auto", + "program": "${fileDirname}/../main.go" + } + ] +} \ No newline at end of file diff --git a/main.go b/main.go index 6f17511..b6b9bf3 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "io" "os" "github.com/shikaan/shmux/pkg/arguments" @@ -9,12 +10,14 @@ import ( "github.com/shikaan/shmux/pkg/scripts" ) +const MAX_FILE_SIZE = 1<<20; // 1MB + func main() { shell, config, scriptName, args, err := arguments.Parse() - exceptions.HandleException(err) + exceptions.HandleException(err, 1) file, err := os.Open(config) - exceptions.HandleException(err) + exceptions.HandleException(err, 1) defer file.Close() if scriptName == arguments.HELP_SCRIPT { @@ -22,9 +25,13 @@ func main() { return } - script, err := scripts.ReadScript(scriptName, shell, file) - exceptions.HandleException(err) + limitedReader := io.LimitReader(file, MAX_FILE_SIZE) + content, err := io.ReadAll(limitedReader) + exceptions.HandleException(err, 1) + + script, err := scripts.ReadScript(scriptName, shell, content, 0) + exceptions.HandleException(err, 1) - err = scripts.RunScript(script, args) - exceptions.HandleScriptError(scriptName, err) + status, err := scripts.RunScript(script, args) + exceptions.HandleException(err, status) } diff --git a/pkg/exceptions/exceptions.go b/pkg/exceptions/exceptions.go index 2191382..d9befbb 100644 --- a/pkg/exceptions/exceptions.go +++ b/pkg/exceptions/exceptions.go @@ -3,24 +3,11 @@ package exceptions import ( "fmt" "os" - "os/exec" ) -func HandleException(e error) { +func HandleException(e error, status int) { if e != nil { os.Stderr.WriteString(fmt.Sprintf("shmux: %s\n", e.Error())) - os.Exit(1) - } -} - -func HandleScriptError(scriptName string, e error) { - if e != nil { - status := 1 - if exitError, ok := e.(*exec.ExitError); ok { - status = exitError.ExitCode() - } - - os.Stderr.WriteString(fmt.Sprintf("shmux: script \"%s\" exited with code %d\n", scriptName, status)) os.Exit(status) } } diff --git a/pkg/scripts/help.go b/pkg/scripts/help.go new file mode 100644 index 0000000..25afae3 --- /dev/null +++ b/pkg/scripts/help.go @@ -0,0 +1,28 @@ +package scripts + +import ( + "bufio" + "fmt" + "io" + "strings" +) + +func MakeHelp(file io.Reader) string { + availableScripts := []string{} + scanner := bufio.NewScanner(file) + + for scanner.Scan() { + line := scanner.Text() + isScriptLine, match, _ := readScript(line) + + if isScriptLine { + availableScripts = append(availableScripts, match) + } + } + + return fmt.Sprintf(`usage: shmux [-config ] [-shell ]