cmdsync

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: May 7, 2021 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package cmdsync has logic for synchronizing multiple shell comands. Commands can depend on the completion or readiness of other commands where readiness can be determined by the output matching some regular expression.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Group

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

Group manages scheduling concurrent ShellCmds

func NewGroup

func NewGroup(commands ...*ShellCmd) *Group

NewGroup makes a new Group it can be optionally initialized with commands or they can be added later via AddCommands

func (*Group) AddCommands

func (g *Group) AddCommands(commands ...*ShellCmd) error

AddCommands will add ShellCmds to the commands slice It will return an error if called after Group.Run()

func (*Group) Run

func (g *Group) Run() error

Run will run all of the group's ShellCmds and block until they have all finished running, or an interrupt/kill signal is received

It checks for each ShellCmd's prerequisites (ShellCmds it depends-on being in a ready state) before starting the ShellCmd

The returned error is the first error returned from the Group's ShellCmds, if any

func (*Group) RunContext added in v0.4.0

func (g *Group) RunContext(ctx context.Context) error

RunContext is the same as Run but can also be cancelled via ctx

func (*Group) SendInterrupts

func (g *Group) SendInterrupts()

SendInterrupts relays an interrupt signal to all underlying commands

type ShellCmd added in v0.4.0

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

ShellCmd is a wrapper around exec.Cmd that eases syncing to other ShellCmd's via Group.

Its implementation calls the shell directly (through zsh/bash)

ShellCmd can indicate that the underlying process has reached a "ready state" by

  1. Its stdout/stderr outputs matching a given regexp.
  2. Its underlying process completing/exiting with a non-zero code.

An interrupt signal can be sent to the underlying process via Interrupt().

func NewShellCmd added in v0.4.0

func NewShellCmd(shell, command string, options ...ShellCmdOption) (*ShellCmd, error)

NewShellCmd defaults to using zsh. bash and sh are also supported

func (*ShellCmd) Interrupt added in v0.4.0

func (s *ShellCmd) Interrupt() error

Interrupt will send an interrupt signal to the process

func (*ShellCmd) IsReady added in v0.4.0

func (s *ShellCmd) IsReady() bool

IsReady is a simple getter for the ready state of a monitored command

func (*ShellCmd) Run added in v0.4.0

func (s *ShellCmd) Run() error

Run the underlying command. This function blocks until the command exits

func (*ShellCmd) RunContext added in v0.4.0

func (s *ShellCmd) RunContext(ctx context.Context) error

RunContext is the same as Run but cancels if the ctx cancels

func (*ShellCmd) Write added in v0.4.0

func (s *ShellCmd) Write(in []byte) (int, error)

Write implements io.Writer, so that ShellCmd itself can be used for exec.ShellCmd.Stdout and Stderr Write "intercepts" writes to Stdout/Stderr to check if the outputs match a regexp and determines if a command has reached its "ready state" the ready state is used by Orchestrator to coordinate dependent commands

type ShellCmdOption added in v0.4.0

type ShellCmdOption func(*ShellCmd) error

func CmdDir

func CmdDir(dir string) ShellCmdOption

CmdDir is a functional option that modifies the Dir property of the underlying exec.ShellCmd which is the directory to execute the Command from

func Color added in v0.4.0

func Color(c color.Color) ShellCmdOption

Color is a functional option that sets the ansiColor for the outputs

func DependsOn

func DependsOn(cmdNames []string) ShellCmdOption

DependsOn is a functional option that sets a slice of dependencies for this command. The dependencies are names of commands that need to have completed or reached a ready state prior to this command starting.

Note that there is no validation that the cmdNames are valid/match other ShellCmd's configs (because it would cause a circular dependency). Some, but not all possible config errors are checked at runtime.

func Environment

func Environment(envMap map[string]string) ShellCmdOption

Environment is a functional option that adds export commands to the start of a command. This is a bit of a hacky workaround to maintain exec.ShellCmd's default environment, while being able to set additional variables

func Name added in v0.4.0

func Name(name string) ShellCmdOption

Name is a functional option that sets a monitored command's name, which is used to prefix each line written to Stdout

func ReadyPattern

func ReadyPattern(pattern string) ShellCmdOption

ReadyPattern is a functional option that takes in a pattern string that must compile into a valid regexp and sets it to monitored command's readyPattern field

func SilenceOutput

func SilenceOutput() ShellCmdOption

SilenceOutput sets the command's Stdout and Stderr to nil so no output will be seen in the terminal

Jump to

Keyboard shortcuts

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