Skip to content

Commit

Permalink
Merge branch 'main' of github.com:shikaan/shmux
Browse files Browse the repository at this point in the history
  • Loading branch information
shikaan committed Aug 18, 2023
2 parents b54eede + 9e73b8b commit c31532e
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 25 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/shikaan/shmux

go 1.19
go 1.21
6 changes: 2 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ func main() {
script, err := scripts.ReadScript(scriptName, shell, file)
exceptions.HandleException(err)

output, err := scripts.RunScript(script, args)
exceptions.HandleException(err)

fmt.Print(output)
err = scripts.RunScript(script, args)
exceptions.HandleScriptError(scriptName, err)
}
13 changes: 9 additions & 4 deletions pkg/exceptions/exceptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package exceptions
import (
"fmt"
"os"
"os/exec"
)

func HandleException(e error) {
Expand All @@ -12,10 +13,14 @@ func HandleException(e error) {
}
}

func HandleScriptError(scriptName string, e error, output string) {
func HandleScriptError(scriptName string, e error) {
if e != nil {
os.Stderr.WriteString(fmt.Sprintf("shmux: script \"%s\" returned non-zero status code.\n", scriptName))
os.Stderr.WriteString(fmt.Sprintf("Error: %s\n", e.Error()))
os.Exit(1)
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)
}
}
31 changes: 15 additions & 16 deletions pkg/scripts/scripts.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ import (
"path/filepath"
"regexp"
"strings"

"github.com/shikaan/shmux/pkg/exceptions"
"time"
)

const TEMP_SCRIPT_FILE = "shmux"
var TEMP_SCRIPT_FILE = ""
func init() {
TEMP_SCRIPT_FILE = fmt.Sprintf("shmux_%d", time.Now().UnixNano())
}

// A script is a slice of lines representing each a LOC
type Script struct {
Expand All @@ -23,28 +25,25 @@ type Script struct {
Options []string
}

// Executes a script in a given shell
// Creates an exec.Cmd that executes a script in a given shell
// Arguments are positional arguments ($1, $2...) which can be used in the script as replacement
func RunScript(script *Script, arguments []string) (string, error) {
path := getTempScriptPath()
os.RemoveAll(path)
func RunScript(script *Script, arguments []string) (err error) {
path := GetTempScriptPath()

file, err := os.Create(path)
if err != nil {
return "", err
return err
}
defer file.Close()
defer os.RemoveAll(path)

fileContent := strings.Join(script.Lines, "\n")
file.WriteString(replaceArguments(fileContent, arguments, script.Name))

out, err := exec.Command(script.Interpreter, append(script.Options, path)...).Output()
if err != nil {
exceptions.HandleScriptError(script.Name, err, string(out))
return "", err
}

return string(out), nil
cmd := exec.Command(script.Interpreter, append(script.Options, path)...)
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
return cmd.Run()
}

// Parses the provided file, retuning the Script whose name is the provided one
Expand Down Expand Up @@ -147,7 +146,7 @@ func replaceArguments(content string, arguments []string, scriptName string) str
}

// Returns the path of the temporary script we use for execution
func getTempScriptPath() string {
func GetTempScriptPath() string {
return filepath.Join(os.TempDir(), TEMP_SCRIPT_FILE)
}

Expand Down

0 comments on commit c31532e

Please sign in to comment.