gosh

package module
v0.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 31, 2022 License: Apache-2.0 Imports: 5 Imported by: 12 modules, 23 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

gosh

A golang library for executing bash & powershell commands easly.

Install

go get -v github.com/abdfnx/gosh

Examples

Run one command on both shell and powershell
package main

import (
  "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 ?
// `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)
package main

import (
  "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.name

  Write-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)
package main

import (
  "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)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Exec added in v0.3.7

func Exec(shell, command string) (error, string, string)

`Exec` just exectes the command

func PowershellCommand

func PowershellCommand(command string)

`PowershellCommand` executes the powershell command.

func PowershellOutput

func PowershellOutput(command string) (error, string, string)

`PowershellOutput` returns the output of powershell command, and any errors.

func PowershellOutputWithDir added in v0.3.9

func PowershellOutputWithDir(command, dir string) (error, string, string)

func Run

func Run(cmd string)

`Run` executes the same command for shell and powershell

func RunMulti

func RunMulti(unixCmd string, winCmd string)

`RunMulti` executes a command for shell and a command for powershell

func RunMultiOutput

func RunMultiOutput(unixCmd string, winCmd string) (error, string, string)

`RunMultiOutput` returns the output of the shell command and the powershell command

func RunOutput

func RunOutput(command string) (error, string, string)

`RunOutput` returns the output of the shared command for shell and powershell

func ShellCommand

func ShellCommand(command string)

`ShellCommand` executes the shell command.

func ShellOutput

func ShellOutput(command string) (error, string, string)

`ShellOutput` returns the output of shell command, and any errors.

func ShellOutputWithDir added in v0.3.9

func ShellOutputWithDir(command, dir string) (error, string, string)

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址