mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-05-12 21:05:48 +02:00
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.
This commit is contained in:
parent
b07c4fc001
commit
76ed204795
7 changed files with 312 additions and 56 deletions
65
pkg/commands/git_commands/git_command_builder.go
Normal file
65
pkg/commands/git_commands/git_command_builder.go
Normal file
|
@ -0,0 +1,65 @@
|
|||
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
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue