From ef7c4f26f7b19b9471794d4a7b136836f1036cef Mon Sep 17 00:00:00 2001 From: Shane C Date: Thu, 4 Jul 2024 21:57:46 -0400 Subject: [PATCH] added 'command' shell command --- cmd/command.go | 28 ++++++++++++++++++++++++++++ tes.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 cmd/command.go create mode 100644 tes.go diff --git a/cmd/command.go b/cmd/command.go new file mode 100644 index 0000000..24c2920 --- /dev/null +++ b/cmd/command.go @@ -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 + +} diff --git a/tes.go b/tes.go new file mode 100644 index 0000000..7f0fcf4 --- /dev/null +++ b/tes.go @@ -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//", + //Shell: "/bin/bash", + }) + + if err != nil { + log.Fatal(err) + } + + if err := cmd.Run(); err != nil { + log.Fatal(err) + } + +}