package
0.0.0-20240430122006-14dca07f557d
Repository: https://github.com/evg4b/fisherman.git
Documentation: pkg.go.dev

# README

fisherman/pkg/shell

A package providing a lightweight cross-platform wrapper over the system shell.

Features:

  • Expandable (implements shell.ShellStrategy interface to integrate)
  • Shell host implements io.Writer interface to simple put commands into stdin.
  • The commands are executed after entering a new line. This can be used to execute commands over time using a single terminal process.

Available shell strategies

  • shell.Cmd() - Strategy to use cmd.exe as shell host. Can be used only on windows. It is also the default shell for windows.
  • shell.Bash() - Strategy to use bash as shell host. Available on windows, linux and darwin. It is the default shell for linux and darwin. Note: for windows bash available only in package MSYS2.
  • shell.PowerShell() - Strategy to use powershell as shell host. Can be used on windows, linux and darwin.

Options

  • shell.WithCwd(dir) - specifies the working directory of the shell. If it is the empty ot not set, host runs the shell in the calling process's current directory.
  • shell.WithStdout(output) - specify the process's standard output.
  • shell.WithStderr(output) - specify the process's standard error.
  • shell.WithEnv(env) - specifies the environment of the shell process (including the shell strategy provided variables).
  • shell.WithRawEnv(env) - specifies the environment of the shell process (without the shell strategy provided variables).
  • shell.WithArgs(args) - holds command line arguments (including the shell strategy provided args)
  • shell.WithRawArgs(args) - holds command line arguments (without the shell strategy provided args)
  • shell.WithEncoding(encoding) - overwrite encoding for shell. (To setup utf-8 encoding pass encoding.Nop)

Examples

Basic

ctx := context.Background()
host := shell.NewHost(ctx, shell.Cmd())

if err := host.Run("echo 'this is demo shell' >> log.txt"); err != nil {
  panic(err)
}

Advanced script input

ctx := context.Background()
host := shell.NewHost(ctx, shell.Cmd())

_, err = fmt.Fprintln(host, "echo 'this is demo shell' >> log.txt")
if err != nil {
  panic(err)
}


_, err = fmt.Fprintln(host, "ping -n 10 google.com")
if err != nil {
  panic(err)
}

if err := host.Wait(); err != nil {
  panic(err)
}

Collect shell output

buffer := bytes.NewBufferString("")

ctx := context.Background()
host := shell.NewHost(ctx, shell.Cmd(), shell.WithStdout(buffer))
if err := host.Run("ping -n 10 google.com"); err != nil {
  panic(err)
}

fmt.Print(buffer.String())

Custom encoding

buffer := bytes.NewBuffer([]byte{})

host := shell.NewHost(
  ctx,
  shell.Cmd(),
  shell.WithStdout(buffer),
  shell.WithEncoding(charmap.Windows1251),
)

fmt.Fprintln(host, "chcp 1251 > nul")
fmt.Fprintln(host, "echo проверка русского текста")

if err := host.Wait(); err != nil {
  panic(err)
}

fmt.Print(buffer.String())

Helpers

Also, the package contains helpers functions in fisherman/pkg/shell/helpers. These functions can be useful when working with shell host.

MergeEnv

  • helpers.MergeEnv - Merges a slice of environment variables (for example os.Environ()) with a map of custom variables. In case when variable already defined in slice, it will replaced from map.

Known issues

There is a problem with rendering utf8 output on windows. This is related to the problem with UTF-8 support in Windows. ReadConsoleA and ReadFile just silently fail with the code page set to 65001. Read more here.

# Packages

No description provided by the author

# Functions

No description provided by the author
No description provided by the author
NewHost creates new shell host based on passed strategy.
No description provided by the author
WithArgs setups arguments (ShellStrategy defined arguments will be included) for shell host.
WithCwd setups environment variables for shell host.
WithEncoding setups shell input/output encoding.
WithEnv setups environment variables (ShellStrategy defined variables will be included) for shell host.
WithRawArgs setups arguments (ShellStrategy defined arguments will not be included) for shell host.
WithRawEnv setups environment variables (ShellStrategy defined variables will not be included) for shell host.
WithStderr setups Stderr writer for shell host.
WithStdout setups Stdout writer for shell host.

# Variables

No description provided by the author

# Structs

No description provided by the author
No description provided by the author
Host is shell host structure to communicate with shell process.
No description provided by the author

# Interfaces

ShellStrategy is interface to describe base concrete shell command.

# Type aliases

No description provided by the author