Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/abdfnx/gosh
⌨ A golang library for executing bash & powershell commands easly.
https://github.com/abdfnx/gosh
bash cli go golang library linux macos powershell shell tool windows
Last synced: 2 months ago
JSON representation
⌨ A golang library for executing bash & powershell commands easly.
- Host: GitHub
- URL: https://github.com/abdfnx/gosh
- Owner: abdfnx
- License: apache-2.0
- Created: 2021-03-05T09:48:05.000Z (almost 4 years ago)
- Default Branch: trunk
- Last Pushed: 2022-11-01T14:55:02.000Z (about 2 years ago)
- Last Synced: 2024-10-01T15:44:50.788Z (3 months ago)
- Topics: bash, cli, go, golang, library, linux, macos, powershell, shell, tool, windows
- Language: Go
- Homepage: https://abdfnx.github.io/gosh
- Size: 26.4 KB
- Stars: 42
- Watchers: 2
- Forks: 7
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# gosh
> A golang library for executing bash & powershell commands easly.
## Install
```bash
go get -v github.com/abdfnx/gosh
```## Examples
### Run one command on both shell and powershell
```go
package mainimport (
"fmt"
"log""github.com/abdfnx/gosh"
)// run a command
gosh.Run("git status")// run a command with output
err, out, errout := gosh.RunOutput("echo 𝜋")if err != nil {
log.Printf("error: %v\n", err)
fmt.Print(errout)
}fmt.Print(out)
```### How `gosh.Run("COMMAND")` works ?
```go
// `Run` executes the same command for shell and powershell
func Run(cmd string) {
err, out, errout := ShellOutput("")if runtime.GOOS == "windows" {
err, out, errout = PowershellOutput(cmd)
} else {
err, out, errout = ShellOutput(cmd)
}if err != nil {
log.Printf("error: %v\n", err)
fmt.Print(errout)
}fmt.Print(out)
}
```### Run Powershell Command(s)
```go
package mainimport (
"fmt"
"log""github.com/abdfnx/gosh"
)// run a command
gosh.PowershellCommand(`Write-Host "hello from powershell"`)// run a script
gosh.PowershellCommand(`
$git_username = git config user.nameWrite-Host $git_username
`)// run a command with output
err, out, errout := gosh.PowershellOutput(`[System.Environment]::SetEnvironmentVariable("Path", $Env:Path + ";$APP_PATH\bin", [System.EnvironmentVariableTarget]::User)`)if err != nil {
log.Printf("error: %v\n", err)
fmt.Print(errout)
}fmt.Print(out)
```### Run Bash/Shell Command(s)
```go
package mainimport (
"fmt"
"log""github.com/abdfnx/gosh"
)// run a command
gosh.ShellCommand(`echo "shell or bash?"`)// run a script
gosh.ShellCommand(`
mood="👨💻"if [ $mood != "😪" ]; then
echo "still coding"
fi
`)// run a command with output
err, out, errout := gosh.ShellOutput(`curl --silent "https://get-latest.onrender.com/docker/compose"`)if err != nil {
log.Printf("error: %v\n", err)
fmt.Print(errout)
}fmt.Print(out)
```