simplify how we log commands

This commit is contained in:
Jesse Duffield 2022-01-05 11:57:32 +11:00
parent e524e39842
commit 05fa483f48
44 changed files with 291 additions and 233 deletions

View file

@ -22,16 +22,20 @@ type ICmdObj interface {
// 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
// logs command
Log() ICmdObj
// Marks the command object as readonly, so that when it is run, we don't log it to the user.
// We only want to log commands to the user which change state in some way.
DontLog() ICmdObj
ShouldLog() bool
}
type CmdObj struct {
cmdStr string
cmd *exec.Cmd
runner ICmdObjRunner
logCommand func(ICmdObj)
runner ICmdObjRunner
// if set to true, we don't want to log the command to the user.
dontLog bool
}
func (self *CmdObj) GetCmd() *exec.Cmd {
@ -52,12 +56,15 @@ func (self *CmdObj) GetEnvVars() []string {
return self.cmd.Env
}
func (self *CmdObj) Log() ICmdObj {
self.logCommand(self)
func (self *CmdObj) DontLog() ICmdObj {
self.dontLog = true
return self
}
func (self *CmdObj) ShouldLog() bool {
return !self.dontLog
}
func (self *CmdObj) Run() error {
return self.runner.Run(self)
}