bash

package module
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Dec 13, 2025 License: MIT Imports: 13 Imported by: 3

README

Go Bash

icon

Run linux bash commands in Go.

Installation

go get github.com/tkdeng/gobash

Usage

import (
  "github.com/tkdeng/gobash"
)

func main(){
  // run bash command
  out, err := bash.Run([]string{`echo`, `test`}, "/pwd", []string{"ENV=var"})

  // run from default directory and no environment vars
  out, err := bash.Run([]string{`echo`, `test`}, "", nil)

  // run raw `bash -c` command
  bash.RunRaw(`echo "test"`, "", nil)

  // run command as specified user
  bash.RunUser(`echo "root test"`, "sudo", "", nil)
  bash.RunUser(`echo "user test"`, "user", "", nil)

  // pipe multiple commands `echo "test" | tee -a "./test.txt"`
  bash.Pipe(".", []string{"echo", "test"}, []string{"tee", "-a", "./test.txt"})
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func If added in v0.1.3

func If(logic string, dir string, env []string) bool

If will run a bash if [[ logic ]] method, and return true or false to the output for go to process

note: uses RunRaw method

func InputSelect

func InputSelect(msg string, opts ...string) int

InputSelect prompts the user to select from a list of options and returns the selected index

func InputText

func InputText(msg string) string

InputText prompts the user for text input and returns the entered string

func InputYN

func InputYN(msg string, def ...bool) bool

InputYN prompts the user for a yes/no input and returns true or false

func Pipe

func Pipe(dir string, args ...[]string)

Pipe allows you to pipe multiple bash commands

[example (bash)]

echo "test" | tee -a "./test.txt"

[example (go)]

bash.Pipe(".", []string{"echo", "test"}, []string{"tee", "-a", "./test.txt"})

@dir: a directory to run the command in (set to an empty string to disable)

func PipeMultiDir

func PipeMultiDir(args ...[]string)

PipeMultiDir allows you to pipe multiple bash commands with a different directory for each of them

note: the first arg is the directory

[example]

bash.PipeMultiDir([]string{"/dir1", "cat", "test.txt"}, []string{"./dir2", "tee", "-a", "./test.txt"})

func PipeMultiDirEnv

func PipeMultiDirEnv(env []string, args ...[]string)

PipeMultiDirEnv is just like the 'PipeMultiDir' method, but it also allows you to add custom envirronment vars

note: the first arg is the directory

[example]

bash.PipeMultiDirEnv([]string{`MyEnvVar=CustomValue`}, []string{"/dir1", "cat", "test.txt"}, []string{"./dir2", "tee", "-a", "./test.txt"})

func Run

func Run(args []string, dir string, env []string, liveOutput ...bool) (output []byte, err error)

Run will run a bash command based on the given args

note: stdin is piped to the os logs

@dir: a directory to run the command in (set to an empty string to disable)
@env: an optional list of environment variables (set to nil to disable)

[optional]

@liveOutput[0]: set to true to pipe stdout and stderr to the os
@liveOutput[1]: set to false to only pipe stdout to the os, and keep stderr hidden

func RunRaw

func RunRaw(cmdStr string, dir string, env []string, liveOutput ...bool) (output []byte, err error)

RunRaw will run an unescaped (unquoted) bash command as a different user

this method uses `bash -c` to get around the auto quotes added by golang

note: user input is Not recommended for this method

note: stdin is piped to the os logs

@cmdStr: the command to run
@dir: a directory to run the command in (set to an empty string to disable)
@env: an optional list of environment variables (set to nil to disable)

[optional]

@liveOutput[0]: set to true to pipe stdout and stderr to the os
@liveOutput[1]: set to false to only pipe stdout to the os, and keep stderr hidden

func RunUser

func RunUser(cmdStr string, user string, dir string, env []string, liveOutput ...bool) (output []byte, err error)

RunUser will run an unescaped (unquoted) bash command as a specified user

this method uses `runuser -l [user] -c`

note: user input is Not recommended for this method

note: stdin is piped to the os logs

@cmdStr: the command to run
@user: the username to run the command as
@dir: a directory to run the command in (set to an empty string to disable)
@env: an optional list of environment variables (set to nil to disable)

[optional]

@liveOutput[0]: set to true to pipe stdout and stderr to the os
@liveOutput[1]: set to false to only pipe stdout to the os, and keep stderr hidden

func RunUserFile

func RunUserFile(file string, args []string, user string, dir string, env []string, liveOutput ...bool) (output []byte, err error)

RunUserFile will run a bash file as a specified user

this method uses `pkexec --user [user]` to simulate a user in a normal desktop environment

note: user input is Not recommended for this method

note: stdin is piped to the os logs

@file: the file to run
@user: the username to run the command as
@dir: a directory to run the command in (set to an empty string to disable)
@env: an optional list of environment variables (set to nil to disable)

[optional]

@liveOutput[0]: set to true to pipe stdout and stderr to the os
@liveOutput[1]: set to false to only pipe stdout to the os, and keep stderr hidden

Types

type ProgressBar

type ProgressBar struct {
	// contains filtered or unexported fields
}

func NewProgressBar

func NewProgressBar(msg string) *ProgressBar

NewProgressBar creates a new terminal progress bar

func (*ProgressBar) AddSize added in v0.1.2

func (pb *ProgressBar) AddSize(size int)

AddSize adds to the total size of the progress bar for the Step method

func (*ProgressBar) Msg

func (pb *ProgressBar) Msg(msg string)

Msg changes the message for the progress bar

func (*ProgressBar) SetLevel

func (pb *ProgressBar) SetLevel(level float64)

SetLevel sets the current percentage level of the progress bar

func (*ProgressBar) SetSize

func (pb *ProgressBar) SetSize(size int)

SetSize sets the total size of the progress bar for the Step method

func (*ProgressBar) Step

func (pb *ProgressBar) Step(size ...int)

Step increments the progress bar by size (default 1)

func (*ProgressBar) Stop

func (pb *ProgressBar) Stop()

Stop stops the progress bar and cleans up the terminal

type SystemSleepLock added in v0.1.5

type SystemSleepLock struct {
	// contains filtered or unexported fields
}

func SleepLock added in v0.1.5

func SleepLock() *SystemSleepLock

SleepLock prevents the system from sleeping by using systemd-inhibit

func (*SystemSleepLock) Release added in v0.1.5

func (lock *SystemSleepLock) Release()

Release releases the sleep lock

Jump to

Keyboard shortcuts

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