oscommands

package
v0.31.3 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2021 License: MIT Imports: 21 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CopyDir

func CopyDir(src string, dst string) (err error)

CopyDir recursively copies a directory tree, attempting to preserve permissions. Source directory must exist. If destination already exists we'll clobber it. Symlinks are ignored and skipped.

func CopyFile

func CopyFile(src, dst string) (err error)

CopyFile copies the contents of the file named src to the file named by dst. The file will be created if it does not already exist. If the destination file exists, all it's contents will be replaced by the contents of the source file. The file mode will be copied from the source and the copied data is synced/flushed to stable storage.

func Kill

func Kill(cmd *exec.Cmd) error

func RunCommandWithOutputLiveAux added in v0.31.1

func RunCommandWithOutputLiveAux(
	c *OSCommand,
	cmdObj ICmdObj,
	writer io.Writer,

	handleOutput func(string) string,
	startCmd func(cmd *exec.Cmd) (*cmdHandler, error),
) error

RunCommandWithOutputLiveAux runs a command and return every word that gets written in stdout Output is a function that executes by every word that gets read by bufio As return of output you need to give a string that will be written to stdin NOTE: If the return data is empty it won't write anything to stdin

func RunCommandWithOutputLiveWrapper

func RunCommandWithOutputLiveWrapper(
	c *OSCommand,
	cmdObj ICmdObj,
	writer io.Writer,
	output func(string) string,
) error

func RunLineOutputCmd

func RunLineOutputCmd(cmd *exec.Cmd, onLine func(line string) (bool, error)) error

Types

type CmdLogEntry added in v0.28.1

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

TODO: make these fields private

func NewCmdLogEntry added in v0.28.1

func NewCmdLogEntry(cmdStr string, span string, commandLine bool) CmdLogEntry

func (CmdLogEntry) GetCmdStr added in v0.28.1

func (e CmdLogEntry) GetCmdStr() string

func (CmdLogEntry) GetCommandLine added in v0.28.1

func (e CmdLogEntry) GetCommandLine() bool

func (CmdLogEntry) GetSpan added in v0.28.1

func (e CmdLogEntry) GetSpan() string

type CmdObj added in v0.30.1

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

func (*CmdObj) AddEnvVars added in v0.30.1

func (self *CmdObj) AddEnvVars(vars ...string) ICmdObj

func (*CmdObj) GetCmd added in v0.30.1

func (self *CmdObj) GetCmd() *exec.Cmd

func (*CmdObj) ToString added in v0.30.1

func (self *CmdObj) ToString() string

type ICmdObj added in v0.30.1

type ICmdObj interface {
	GetCmd() *exec.Cmd
	ToString() string
	AddEnvVars(...string) ICmdObj
}

A command object is a general way to represent a command to be run on the command line. If you want to log the command you'll use .ToString() and if you want to run it you'll use .GetCmd()

type OSCommand

type OSCommand struct {
	Log              *logrus.Entry
	Platform         *Platform
	Config           config.AppConfigurer
	Command          func(string, ...string) *exec.Cmd
	BeforeExecuteCmd func(*exec.Cmd)
	Getenv           func(string) string

	// something like 'Staging File': allows us to group cmd logs under a single title
	CmdLogSpan string
	// contains filtered or unexported fields
}

OSCommand holds all the os commands

func NewDummyOSCommand

func NewDummyOSCommand() *OSCommand

NewDummyOSCommand creates a new dummy OSCommand for testing

func NewOSCommand

func NewOSCommand(log *logrus.Entry, config config.AppConfigurer) *OSCommand

NewOSCommand os command runner

func (*OSCommand) AppendLineToFile

func (c *OSCommand) AppendLineToFile(filename, line string) error

AppendLineToFile adds a new line in file

func (*OSCommand) CopyToClipboard

func (c *OSCommand) CopyToClipboard(str string) error

func (*OSCommand) CreateFileWithContent

func (c *OSCommand) CreateFileWithContent(path string, content string) error

CreateFileWithContent creates a file with the given content

func (*OSCommand) CreateTempFile

func (c *OSCommand) CreateTempFile(filename, content string) (string, error)

CreateTempFile writes a string to a new temp file and returns the file's name

func (*OSCommand) DetectUnamePass

func (c *OSCommand) DetectUnamePass(cmdObj ICmdObj, writer io.Writer, promptUserForCredential func(string) string) error

DetectUnamePass detect a username / password / passphrase question in a command promptUserForCredential is a function that gets executed when this function detect you need to fillin a password or passphrase The promptUserForCredential argument will be "username", "password" or "passphrase" and expects the user's password/passphrase or username back

func (*OSCommand) ExecutableFromString

func (c *OSCommand) ExecutableFromString(commandStr string) *exec.Cmd

ExecutableFromString takes a string like `git status` and returns an executable command for it

func (*OSCommand) FileExists

func (c *OSCommand) FileExists(path string) (bool, error)

FileExists checks whether a file exists at the specified path

func (*OSCommand) FileType

func (c *OSCommand) FileType(path string) string

FileType tells us if the file is a file, directory or other

func (*OSCommand) GetLazygitPath

func (c *OSCommand) GetLazygitPath() string

GetLazygitPath returns the path of the currently executed file

func (*OSCommand) LogCommand added in v0.28.1

func (c *OSCommand) LogCommand(cmdStr string, commandLine bool)

func (*OSCommand) LogExecCmd added in v0.28.1

func (c *OSCommand) LogExecCmd(cmd *exec.Cmd)

func (*OSCommand) NewCmdObj added in v0.30.1

func (c *OSCommand) NewCmdObj(cmd *exec.Cmd) ICmdObj

func (*OSCommand) NewCmdObjFromArgs added in v0.30.1

func (c *OSCommand) NewCmdObjFromArgs(args []string) ICmdObj

func (*OSCommand) NewCmdObjFromStr added in v0.30.1

func (c *OSCommand) NewCmdObjFromStr(cmdStr string) ICmdObj

func (*OSCommand) OpenFile

func (c *OSCommand) OpenFile(filename string) error

OpenFile opens a file with the given

func (c *OSCommand) OpenLink(link string) error

OpenLink opens a file with the given

func (*OSCommand) PipeCommands

func (c *OSCommand) PipeCommands(commandStrings ...string) error

PipeCommands runs a heap of commands and pipes their inputs/outputs together like A | B | C

func (*OSCommand) PrepareShellSubProcess added in v0.27.1

func (c *OSCommand) PrepareShellSubProcess(command string) *exec.Cmd

PrepareShellSubProcess returns the pointer to a custom command

func (*OSCommand) PrepareSubProcess

func (c *OSCommand) PrepareSubProcess(cmdName string, commandArgs ...string) *exec.Cmd

PrepareSubProcess iniPrepareSubProcessrocess then tells the Gui to switch to it TODO: see if this needs to exist, given that ExecutableFromString does the same things

func (*OSCommand) Quote

func (c *OSCommand) Quote(message string) string

Quote wraps a message in platform-specific quotation marks

func (*OSCommand) Remove

func (c *OSCommand) Remove(filename string) error

Remove removes a file or directory at the specified path

func (*OSCommand) RemoveFile added in v0.28.1

func (c *OSCommand) RemoveFile(path string) error

func (*OSCommand) RunCommand

func (c *OSCommand) RunCommand(formatString string, formatArgs ...interface{}) error

RunCommand runs a command and just returns the error

func (*OSCommand) RunCommandWithOptions

func (c *OSCommand) RunCommandWithOptions(command string, options RunCommandOptions) error

func (*OSCommand) RunCommandWithOutput

func (c *OSCommand) RunCommandWithOutput(formatString string, formatArgs ...interface{}) (string, error)

RunCommandWithOutput wrapper around commands returning their output and error NOTE: If you don't pass any formatArgs we'll just use the command directly, however there's a bizarre compiler error/warning when you pass in a formatString with a percent sign because it thinks it's supposed to be a formatString when in that case it's not. To get around that error you'll need to define the string in a variable and pass the variable into RunCommandWithOutput.

func (*OSCommand) RunCommandWithOutputLive

func (c *OSCommand) RunCommandWithOutputLive(cmdObj ICmdObj, writer io.Writer, handleOutput func(string) string) error

Due to a lack of pty support on windows we have RunCommandWithOutputLiveWrapper being defined separate for windows and other OS's

func (*OSCommand) RunCommandWithOutputWithOptions

func (c *OSCommand) RunCommandWithOutputWithOptions(command string, options RunCommandOptions) (string, error)

func (*OSCommand) RunExecutable

func (c *OSCommand) RunExecutable(cmd *exec.Cmd) error

RunExecutable runs an executable file and returns an error if there was one

func (*OSCommand) RunExecutableWithOutput

func (c *OSCommand) RunExecutableWithOutput(cmd *exec.Cmd) (string, error)

RunExecutableWithOutput runs an executable file and returns its output

func (*OSCommand) RunPreparedCommand

func (c *OSCommand) RunPreparedCommand(cmd *exec.Cmd) error

RunPreparedCommand takes a pointer to an exec.Cmd and runs it this is useful if you need to give your command some environment variables before running it

func (*OSCommand) RunShellCommand added in v0.27.1

func (c *OSCommand) RunShellCommand(command string) error

RunShellCommand runs shell commands i.e. 'sh -c <command>'. Good for when you need access to the shell

func (*OSCommand) SetBeforeExecuteCmd

func (c *OSCommand) SetBeforeExecuteCmd(cmd func(*exec.Cmd))

func (*OSCommand) SetCommand

func (c *OSCommand) SetCommand(cmd func(string, ...string) *exec.Cmd)

SetCommand sets the command function used by the struct. To be used for testing only

func (*OSCommand) SetOnRunCommand added in v0.28.1

func (c *OSCommand) SetOnRunCommand(f func(CmdLogEntry))

func (*OSCommand) SetRemoveFile added in v0.28.1

func (c *OSCommand) SetRemoveFile(f func(string) error)

To be used for testing only

func (*OSCommand) ShellCommandFromString

func (c *OSCommand) ShellCommandFromString(commandStr string) *exec.Cmd

ShellCommandFromString takes a string like `git commit` and returns an executable shell command for it

func (*OSCommand) WithSpan added in v0.28.1

func (c *OSCommand) WithSpan(span string) *OSCommand

type Platform

type Platform struct {
	OS              string
	Shell           string
	ShellArg        string
	OpenCommand     string
	OpenLinkCommand string
}

Platform stores the os state

type RunCommandOptions

type RunCommandOptions struct {
	EnvVars []string
}

Jump to

Keyboard shortcuts

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