summaryrefslogtreecommitdiffstats
path: root/pkg/commands/git_commands/git_command_builder.go
blob: 7708c8e9ff9db5e08be76fbd09aa2c3abc7d0c3e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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
}