oscommands

package
v0.32.2 Latest Latest
Warning

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

Go to latest
Published: Jan 17, 2022 License: MIT Imports: 22 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 FileType added in v0.32.1

func FileType(path string) string

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

func GetLazygitPath added in v0.32.1

func GetLazygitPath() string

GetLazygitPath returns the path of the currently executed file

func GetTempDir added in v0.32.1

func GetTempDir() string

func Kill

func Kill(cmd *exec.Cmd) error

func NewGuiIO added in v0.32.1

func NewGuiIO(log *logrus.Entry, logCommandFn func(string, bool), newCmdWriterFn func() io.Writer, promptForCredentialFn func(CredentialType) string) *guiIO

func NewNullGuiIO added in v0.32.1

func NewNullGuiIO(log *logrus.Entry) *guiIO

we use this function when we want to access the functionality of our OS struct but we don't have anywhere to log things, or request input from the user.

func TestOSCommandOpenFileDarwin added in v0.32.1

func TestOSCommandOpenFileDarwin(t *testing.T)

func TestOSCommandOpenFileLinux added in v0.32.1

func TestOSCommandOpenFileLinux(t *testing.T)

TestOSCommandOpenFileLinux tests the OpenFile command on Linux

Types

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) DontLog added in v0.32.1

func (self *CmdObj) DontLog() ICmdObj

func (*CmdObj) FailOnCredentialRequest added in v0.32.1

func (self *CmdObj) FailOnCredentialRequest() ICmdObj

func (*CmdObj) GetCmd added in v0.30.1

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

func (*CmdObj) GetCredentialStrategy added in v0.32.1

func (self *CmdObj) GetCredentialStrategy() CredentialStrategy

func (*CmdObj) GetEnvVars added in v0.32.1

func (self *CmdObj) GetEnvVars() []string

func (*CmdObj) PromptOnCredentialRequest added in v0.32.1

func (self *CmdObj) PromptOnCredentialRequest() ICmdObj

func (*CmdObj) Run added in v0.32.1

func (self *CmdObj) Run() error

func (*CmdObj) RunAndProcessLines added in v0.32.1

func (self *CmdObj) RunAndProcessLines(onLine func(line string) (bool, error)) error

func (*CmdObj) RunWithOutput added in v0.32.1

func (self *CmdObj) RunWithOutput() (string, error)

func (*CmdObj) ShouldLog added in v0.32.1

func (self *CmdObj) ShouldLog() bool

func (*CmdObj) ToString added in v0.30.1

func (self *CmdObj) ToString() string

type CmdObjBuilder added in v0.32.1

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

func NewDummyCmdObjBuilder added in v0.32.1

func NewDummyCmdObjBuilder(runner ICmdObjRunner) *CmdObjBuilder

func (*CmdObjBuilder) CloneWithNewRunner added in v0.32.1

func (self *CmdObjBuilder) CloneWithNewRunner(decorate func(ICmdObjRunner) ICmdObjRunner) *CmdObjBuilder

func (*CmdObjBuilder) New added in v0.32.1

func (self *CmdObjBuilder) New(cmdStr string) ICmdObj

func (*CmdObjBuilder) NewFromArgs added in v0.32.1

func (self *CmdObjBuilder) NewFromArgs(args []string) ICmdObj

func (*CmdObjBuilder) NewShell added in v0.32.1

func (self *CmdObjBuilder) NewShell(commandStr string) ICmdObj

func (*CmdObjBuilder) Quote added in v0.32.1

func (self *CmdObjBuilder) Quote(message string) string

type CredentialStrategy added in v0.32.1

type CredentialStrategy int
const (
	// do not expect a credential request. If we end up getting one
	// we'll be in trouble because the command will hang indefinitely
	NONE CredentialStrategy = iota
	// expect a credential request and if we get one, prompt the user to enter their username/password
	PROMPT
	// in this case we will check for a credential request (i.e. the command pauses to ask for
	// username/password) and if we get one, we just submit a newline, forcing the
	// command to fail. We use this e.g. for a background `git fetch` to prevent it
	// from hanging indefinitely.
	FAIL
)

type CredentialType added in v0.32.1

type CredentialType int
const (
	Password CredentialType = iota
	Username
	Passphrase
)

type FakeCmdObjRunner added in v0.32.1

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

func NewFakeRunner added in v0.32.1

func NewFakeRunner(t *testing.T) *FakeCmdObjRunner

func (*FakeCmdObjRunner) CheckForMissingCalls added in v0.32.1

func (self *FakeCmdObjRunner) CheckForMissingCalls()

func (*FakeCmdObjRunner) Expect added in v0.32.1

func (self *FakeCmdObjRunner) Expect(expectedCmdStr string, output string, err error) *FakeCmdObjRunner

func (*FakeCmdObjRunner) ExpectArgs added in v0.32.1

func (self *FakeCmdObjRunner) ExpectArgs(expectedArgs []string, output string, err error) *FakeCmdObjRunner

func (*FakeCmdObjRunner) ExpectFunc added in v0.32.1

func (self *FakeCmdObjRunner) ExpectFunc(fn func(cmdObj ICmdObj) (string, error)) *FakeCmdObjRunner

func (*FakeCmdObjRunner) ExpectGitArgs added in v0.32.1

func (self *FakeCmdObjRunner) ExpectGitArgs(expectedArgs []string, output string, err error) *FakeCmdObjRunner

func (*FakeCmdObjRunner) Run added in v0.32.1

func (self *FakeCmdObjRunner) Run(cmdObj ICmdObj) error

func (*FakeCmdObjRunner) RunAndProcessLines added in v0.32.1

func (self *FakeCmdObjRunner) RunAndProcessLines(cmdObj ICmdObj, onLine func(line string) (bool, error)) error

func (*FakeCmdObjRunner) RunWithOutput added in v0.32.1

func (self *FakeCmdObjRunner) RunWithOutput(cmdObj ICmdObj) (string, error)

type ICmdObj added in v0.30.1

type ICmdObj interface {
	GetCmd() *exec.Cmd
	// outputs string representation of command. Note that if the command was built
	// using NewFromArgs, the output won't be quite the same as what you would type
	// into a terminal e.g. 'sh -c git commit' as opposed to 'sh -c "git commit"'
	ToString() string

	AddEnvVars(...string) ICmdObj
	GetEnvVars() []string

	// runs the command and returns an error if any
	Run() error
	// runs the command and returns the output as a string, and an error if any
	RunWithOutput() (string, error)
	// runs the command and runs a callback function on each line of the output. If the callback returns true for the boolean value, we kill the process and return.
	RunAndProcessLines(onLine func(line string) (bool, error)) error

	// Be calling DontLog(), we're saying that once we call Run(), we don't want to
	// log the command in the UI (it'll still be logged in the log file). The general rule
	// is that if a command doesn't change the git state (e.g. read commands like `git diff`)
	// then we don't want to log it. If we are changing something (e.g. `git add .`) then
	// we do. The only exception is if we're running a command in the background periodically
	// like `git fetch`, which technically does mutate stuff but isn't something we need
	// to notify the user about.
	DontLog() ICmdObj

	// This returns false if DontLog() was called
	ShouldLog() bool

	PromptOnCredentialRequest() ICmdObj
	FailOnCredentialRequest() ICmdObj

	GetCredentialStrategy() CredentialStrategy
}

A command object is a general way to represent a command to be run on the command line.

type ICmdObjBuilder added in v0.32.1

type ICmdObjBuilder interface {
	// New returns a new command object based on the string provided
	New(cmdStr string) ICmdObj
	// NewShell takes a string like `git commit` and returns an executable shell command for it e.g. `sh -c 'git commit'`
	NewShell(commandStr string) ICmdObj
	// NewFromArgs takes a slice of strings like []string{"git", "commit"} and returns a new command object. This can be useful when you don't want to worry about whitespace and quoting and stuff.
	NewFromArgs(args []string) ICmdObj
	// Quote wraps a string in quotes with any necessary escaping applied. The reason for bundling this up with the other methods in this interface is that we basically always need to make use of this when creating new command objects.
	Quote(str string) string
}

type ICmdObjRunner added in v0.32.1

type ICmdObjRunner interface {
	Run(cmdObj ICmdObj) error
	RunWithOutput(cmdObj ICmdObj) (string, error)
	RunAndProcessLines(cmdObj ICmdObj, onLine func(line string) (bool, error)) error
}

type OSCommand

type OSCommand struct {
	*common.Common
	Platform *Platform

	Cmd *CmdObjBuilder
	// 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 NewDummyOSCommandWithDeps added in v0.32.1

func NewDummyOSCommandWithDeps(deps OSCommandDeps) *OSCommand

func NewDummyOSCommandWithRunner added in v0.32.1

func NewDummyOSCommandWithRunner(runner *FakeCmdObjRunner) *OSCommand

func NewOSCommand

func NewOSCommand(common *common.Common, platform *Platform, guiIO *guiIO) *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) FileExists

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

FileExists checks whether a file exists at the specified path

func (*OSCommand) Getenv

func (c *OSCommand) Getenv(key string) string

func (*OSCommand) LogCommand added in v0.28.1

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

func (*OSCommand) OpenFile

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

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) 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

type OSCommandDeps added in v0.32.1

type OSCommandDeps struct {
	Common       *common.Common
	Platform     *Platform
	GetenvFn     func(string) string
	RemoveFileFn func(string) error
	Cmd          *CmdObjBuilder
}

type Platform

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

Platform stores the os state

func GetPlatform added in v0.32.1

func GetPlatform() *Platform

Jump to

Keyboard shortcuts

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