From e53e3babecba97777df9a7164bff8039c997db92 Mon Sep 17 00:00:00 2001 From: Shane C Date: Thu, 4 Jul 2024 15:29:39 -0400 Subject: [PATCH] initial commit --- .idea/.gitignore | 8 +++ .idea/modules.xml | 8 +++ .idea/shell.iml | 9 +++ .idea/vcs.xml | 6 ++ README.md | 7 ++ go.mod | 5 ++ go.sum | 2 + main.go | 177 ++++++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 222 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/modules.xml create mode 100644 .idea/shell.iml create mode 100644 .idea/vcs.xml create mode 100644 README.md create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..d7b24a1 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/shell.iml b/.idea/shell.iml new file mode 100644 index 0000000..5e764c4 --- /dev/null +++ b/.idea/shell.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..f8437f0 --- /dev/null +++ b/README.md @@ -0,0 +1,7 @@ +# Shell Module + +This module enables shell integration within Eggactyl. + +# Usage + +Please see [the wiki](https://git.shadowhosting.xyz/Eggactyl/shell/wiki). \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..39d0fc5 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module git.shadowhosting.xyz/Eggactyl/shell + +go 1.22.4 + +require golang.org/x/sys v0.22.0 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..96f003d --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI= +golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= diff --git a/main.go b/main.go new file mode 100644 index 0000000..31c1b00 --- /dev/null +++ b/main.go @@ -0,0 +1,177 @@ +package shell + +import ( + "bytes" + "errors" + "fmt" + "io/fs" + "os" + "os/exec" + "strings" + + "golang.org/x/sys/unix" +) + +var LinuxType string + +var ErrCommandDoesNotExist = errors.New("command Doesn't Exist") +var ErrSourceDoesNotExist = errors.New("source Doesn't Exist") + +func RunCommand(command string, args ...string) error { + + if !DoesCommandExist(command) { + return ErrCommandDoesNotExist + } + + var stderr bytes.Buffer + + var mainCommand string + + mainCommand = "/bin/bash" + + bashCommand := exec.Command(mainCommand, "-c", command) + bashCommand.Args = append(bashCommand.Args, args...) + bashCommand.Stderr = &stderr + + if err := bashCommand.Start(); err != nil { + return errors.New(stderr.String() + " " + err.Error()) + } + + if err := bashCommand.Wait(); err != nil { + return errors.New(stderr.String() + " " + err.Error()) + } + + return nil + +} + +func RunCommandWithSource(source string, command string, args ...string) error { + + if !DoesSourceExist(source) { + return ErrSourceDoesNotExist + } + + if !DoesCommandExistWithSource(source, command) { + return ErrCommandDoesNotExist + } + + var stderr bytes.Buffer + + var mainCommand string + + mainCommand = "/bin/bash" + + bashCommand := exec.Command(mainCommand, "-c", fmt.Sprintf("source %s; %s", source, command)) + bashCommand.Args = append(bashCommand.Args, args...) + bashCommand.Stderr = &stderr + + if err := bashCommand.Start(); err != nil { + return errors.New(stderr.String()) + } + + if err := bashCommand.Wait(); err != nil { + return errors.New(stderr.String()) + } + + return nil + +} + +func DoesCommandExist(command string) bool { + + var stderr bytes.Buffer + + var mainCommand string + + mainCommand = "/bin/bash" + + bashCommand := exec.Command(mainCommand, "-c", fmt.Sprintf("command -v %s", command)) + bashCommand.Stderr = &stderr + + if err := bashCommand.Start(); err != nil { + if strings.Contains(err.Error(), "1") { + return false + } + } + + if err := bashCommand.Wait(); err != nil { + if strings.Contains(err.Error(), "1") { + return false + } + } + + return true + +} + +func DoesSourceExist(source string) bool { + + var stderr bytes.Buffer + + var mainCommand string + + mainCommand = "/bin/bash" + + bashCommand := exec.Command(mainCommand, "-c", fmt.Sprintf("source %s", source)) + bashCommand.Stderr = &stderr + + if err := bashCommand.Start(); err != nil { + if strings.Contains(err.Error(), "1") { + return false + } + } + + if err := bashCommand.Wait(); err != nil { + if strings.Contains(err.Error(), "1") { + return false + } + } + + return true + +} + +func DoesCommandExistWithSource(source string, command string) bool { + + if sourceCheck := DoesSourceExist(source); !sourceCheck { + return false + } + + var stderr bytes.Buffer + + var mainCommand string + + mainCommand = "/bin/bash" + + bashCommand := exec.Command(mainCommand, "-c", fmt.Sprintf("source %s && command -v %s", source, command)) + bashCommand.Stderr = &stderr + + if err := bashCommand.Start(); err != nil { + if strings.Contains(err.Error(), "1") { + return false + } + } + + if err := bashCommand.Wait(); err != nil { + if strings.Contains(err.Error(), "1") { + return false + } + } + + return true + +} + +func CanExecute(script string) bool { + + if _, err := os.Stat(script); errors.Is(err, fs.ErrNotExist) { + return false + } + + if err := unix.Access(script, unix.X_OK); err != nil { + return err == unix.EACCES + } + + return true + +}