lazygit/pkg/commands/git_commands/git_command_builder.go
Jesse Duffield 76ed204795 Add convenience builder for git commands
Adds a convenience builder for git commands. Applies it in a couple of places that benefit from it.

I've got a larger PR that does a more sweeping change but I want some reviews on that before I go ahead with it.
2023-05-20 16:12:34 +10:00

65 lines
1.3 KiB
Go

package git_commands
import "fmt"
// convenience struct for building git commands. Especially useful when
// including conditional args
type GitCommandBuilder struct {
command string
}
func NewGitCmd(command string) *GitCommandBuilder {
return &GitCommandBuilder{command: command}
}
func (self *GitCommandBuilder) Arg(flag string) *GitCommandBuilder {
if flag == "" {
return self
}
self.command += " " + flag
return self
}
func (self *GitCommandBuilder) ArgIf(include bool, flag string) *GitCommandBuilder {
if include {
return self.Arg(flag)
}
return self
}
func (self *GitCommandBuilder) ArgIfElse(isTrue bool, onTrue string, onFalse string) *GitCommandBuilder {
if isTrue {
return self.Arg(onTrue)
} else {
return self.Arg(onFalse)
}
}
func (self *GitCommandBuilder) Args(args []string) *GitCommandBuilder {
for _, arg := range args {
self.Arg(arg)
}
return self
}
func (self *GitCommandBuilder) Config(value string) *GitCommandBuilder {
// config settings come before the command
self.command = fmt.Sprintf("-c %s %s", value, self.command)
return self
}
func (self *GitCommandBuilder) RepoPath(value string) *GitCommandBuilder {
// repo path comes before the command
self.command = fmt.Sprintf("-C %s %s", value, self.command)
return self
}
func (self *GitCommandBuilder) ToString() string {
return "git " + self.command
}