initial commit

This commit is contained in:
Shane C 2024-07-04 15:29:39 -04:00
commit e53e3babec
Signed by: shane
GPG key ID: E46B5FEA35B22FF9
8 changed files with 222 additions and 0 deletions

8
.idea/.gitignore vendored Normal file
View file

@ -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

8
.idea/modules.xml Normal file
View file

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/shell.iml" filepath="$PROJECT_DIR$/.idea/shell.iml" />
</modules>
</component>
</project>

9
.idea/shell.iml Normal file
View file

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="Go" enabled="true" />
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

6
.idea/vcs.xml Normal file
View file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

7
README.md Normal file
View file

@ -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).

5
go.mod Normal file
View file

@ -0,0 +1,5 @@
module git.shadowhosting.xyz/Eggactyl/shell
go 1.22.4
require golang.org/x/sys v0.22.0

2
go.sum Normal file
View file

@ -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=

177
main.go Normal file
View file

@ -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
}