added 'command' shell command

This commit is contained in:
Shane C 2024-07-04 21:57:46 -04:00
parent 2cc3517f37
commit ef7c4f26f7
Signed by: shane
GPG key ID: E46B5FEA35B22FF9
2 changed files with 58 additions and 0 deletions

28
cmd/command.go Normal file
View file

@ -0,0 +1,28 @@
package cmd
import (
"errors"
"fmt"
"os/exec"
"strings"
)
func Command(cmd string) (string, error) {
command := exec.Command("command", "-v", cmd)
outputBytes, err := command.Output()
if err != nil {
var exitErr *exec.ExitError
if errors.As(err, &exitErr) {
if exitErr.ExitCode() == 1 {
return "", ErrNotFound
} else {
return "", fmt.Errorf("command error: %w", err)
}
}
}
return strings.Trim(string(outputBytes), "\n"), nil
}

30
tes.go Normal file
View file

@ -0,0 +1,30 @@
package main
import (
"errors"
"git.shadowhosting.xyz/Eggactyl/shell/linux"
"log"
)
func main() {
cmd, err := linux.NewCommand(linux.CommandOptions{
Command: "./package",
//Sources: []string{"./nvm.sh"},
Args: []string{"install"},
CustomErrors: map[int8]error{
1: errors.New("failed to download package"),
},
//Cwd: "/home/<user>/<dir>",
//Shell: "/bin/bash",
})
if err != nil {
log.Fatal(err)
}
if err := cmd.Run(); err != nil {
log.Fatal(err)
}
}