shell

package
v0.0.0-...-117ffbb Latest Latest
Warning

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

Go to latest
Published: Mar 15, 2024 License: MIT Imports: 8 Imported by: 0

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.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Default = Bash

Functions

This section is empty.

Types

type BashStrategy

type BashStrategy struct{}

func Bash

func Bash() *BashStrategy

func (*BashStrategy) ArgsWrapper

func (s *BashStrategy) ArgsWrapper(args []string) []string

func (*BashStrategy) EnvWrapper

func (s *BashStrategy) EnvWrapper(env []string) []string

func (*BashStrategy) GetCommand

func (s *BashStrategy) GetCommand(ctx context.Context) *exec.Cmd

func (*BashStrategy) GetEncoding

func (s *BashStrategy) GetEncoding() encoding.Encoding

func (*BashStrategy) GetName

func (s *BashStrategy) GetName() string

type CmdStrategy

type CmdStrategy struct{}

func Cmd

func Cmd() *CmdStrategy

func (*CmdStrategy) ArgsWrapper

func (s *CmdStrategy) ArgsWrapper(args []string) []string

func (*CmdStrategy) EnvWrapper

func (s *CmdStrategy) EnvWrapper(env []string) []string

func (*CmdStrategy) GetCommand

func (s *CmdStrategy) GetCommand(ctx context.Context) *exec.Cmd

func (*CmdStrategy) GetEncoding

func (s *CmdStrategy) GetEncoding() encoding.Encoding

func (*CmdStrategy) GetName

func (s *CmdStrategy) GetName() string

type Host

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

Host is shell host structure to communicate with shell process.

func NewHost

func NewHost(ctx context.Context, strategy Strategy, options ...HostOption) *Host

NewHost creates new shell host based on passed strategy.

func (*Host) Close

func (host *Host) Close() (err error)

func (*Host) Run

func (host *Host) Run(script string) error

Run runs new shell host based on passed strategy.

func (*Host) Start

func (host *Host) Start() error

Start starts shell host.

func (*Host) Terminate

func (host *Host) Terminate() error

func (*Host) Wait

func (host *Host) Wait() error

Wait waits for the shell to exit and waits for any copying from stdout or stderr to complete. Wait automatically closes stdin pipe, and writing will be unavailable after the call.

func (*Host) Write

func (host *Host) Write(payload []byte) (int, error)

Write is io.Writer interface implementation. Write automatically starts shell process if it has not been started before.

type HostOption

type HostOption = func(str Strategy, host *Host)

func WithArgs

func WithArgs(args []string) HostOption

WithArgs setups arguments (ShellStrategy defined arguments will be included) for shell host.

func WithCwd

func WithCwd(dir string) HostOption

WithCwd setups environment variables for shell host.

func WithEncoding

func WithEncoding(encoding encoding.Encoding) HostOption

WithEncoding setups shell input/output encoding.

func WithEnv

func WithEnv(env []string) HostOption

WithEnv setups environment variables (ShellStrategy defined variables will be included) for shell host.

func WithRawArgs

func WithRawArgs(args []string) HostOption

WithRawArgs setups arguments (ShellStrategy defined arguments will not be included) for shell host.

func WithRawEnv

func WithRawEnv(env []string) HostOption

WithRawEnv setups environment variables (ShellStrategy defined variables will not be included) for shell host.

func WithStderr

func WithStderr(output io.Writer) HostOption

WithStderr setups Stderr writer for shell host.

func WithStdout

func WithStdout(output io.Writer) HostOption

WithStdout setups Stdout writer for shell host.

type PowershellStrategy

type PowershellStrategy struct{}

func PowerShell

func PowerShell() *PowershellStrategy

func (*PowershellStrategy) ArgsWrapper

func (s *PowershellStrategy) ArgsWrapper(args []string) []string

func (*PowershellStrategy) EnvWrapper

func (s *PowershellStrategy) EnvWrapper(env []string) []string

func (*PowershellStrategy) GetCommand

func (s *PowershellStrategy) GetCommand(ctx context.Context) *exec.Cmd

func (*PowershellStrategy) GetEncoding

func (s *PowershellStrategy) GetEncoding() encoding.Encoding

func (*PowershellStrategy) GetName

func (s *PowershellStrategy) GetName() string

type Strategy

type Strategy interface {
	GetName() string
	GetCommand(ctx context.Context) *exec.Cmd
	ArgsWrapper(args []string) []string
	EnvWrapper(env []string) []string
	GetEncoding() encoding.Encoding
}

ShellStrategy is interface to describe base concrete shell command.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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