Skip to content

Commit 1e6b6ca

Browse files
committed
Rename process package to command
1 parent d40f797 commit 1e6b6ca

6 files changed

Lines changed: 99 additions & 99 deletions

File tree

ansible/ansible.go

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@ import (
66
"os/exec"
77
"strings"
88

9+
"github.com/GearBoxLab/core/command"
910
"github.com/GearBoxLab/core/printer"
10-
"github.com/GearBoxLab/core/process"
1111

1212
"github.com/symfony-cli/terminal"
1313
)
1414

1515
type Ansible struct {
16-
processFactory process.Factory
16+
commandFactory command.Factory
1717
}
1818

19-
func New(processFactory process.Factory) *Ansible {
19+
func New(commandFactory command.Factory) *Ansible {
2020
return &Ansible{
21-
processFactory: processFactory,
21+
commandFactory: commandFactory,
2222
}
2323
}
2424

@@ -32,33 +32,33 @@ func (ansible *Ansible) Install(osName, sudoPassword string) (err error) {
3232
if false == installed {
3333
switch osName {
3434
case "oracle-linux":
35-
processes := []*process.Process{
36-
ansible.processFactory.NewSudoProcess(sudoPassword, "dnf", "check-update", "-y"),
37-
ansible.processFactory.NewSudoProcess(sudoPassword, "dnf", "upgrade", "-y"),
38-
ansible.processFactory.NewSudoProcess(sudoPassword, "dnf", "install", "-y", "epel-release"),
39-
ansible.processFactory.NewSudoProcess(sudoPassword, "dnf", "install", "-y", "ansible"),
35+
commands := []*command.Command{
36+
ansible.commandFactory.NewSudoCommand(sudoPassword, "dnf", "check-update", "-y"),
37+
ansible.commandFactory.NewSudoCommand(sudoPassword, "dnf", "upgrade", "-y"),
38+
ansible.commandFactory.NewSudoCommand(sudoPassword, "dnf", "install", "-y", "epel-release"),
39+
ansible.commandFactory.NewSudoCommand(sudoPassword, "dnf", "install", "-y", "ansible"),
4040
}
4141

42-
printer.Printf("\n<comment>$ %s</comment>\n", processes[0].String())
43-
if err = processes[0].Run(); err != nil {
42+
printer.Printf("\n<comment>$ %s</comment>\n", commands[0].String())
43+
if err = commands[0].Run(); err != nil {
4444
var exitError *exec.ExitError
4545
if errors.As(err, &exitError) && exitError.ExitCode() == 100 {
46-
printer.Printf("\n<comment>$ %s</comment>\n", processes[1].String())
47-
if err = processes[1].Run(); err != nil {
46+
printer.Printf("\n<comment>$ %s</comment>\n", commands[1].String())
47+
if err = commands[1].Run(); err != nil {
4848
return err
4949
}
5050
} else {
5151
return err
5252
}
5353
}
5454

55-
printer.Printf("\n<comment>$ %s</comment>\n", processes[2].String())
56-
if err = processes[2].Run(); nil != err {
55+
printer.Printf("\n<comment>$ %s</comment>\n", commands[2].String())
56+
if err = commands[2].Run(); nil != err {
5757
return err
5858
}
5959

60-
printer.Printf("\n<comment>$ %s</comment>\n", processes[3].String())
61-
if err = processes[3].Run(); nil != err {
60+
printer.Printf("\n<comment>$ %s</comment>\n", commands[3].String())
61+
if err = commands[3].Run(); nil != err {
6262
return err
6363
}
6464
default:
@@ -70,25 +70,25 @@ func (ansible *Ansible) Install(osName, sudoPassword string) (err error) {
7070
}
7171

7272
func (ansible *Ansible) RunAnsiblePlaybook(playbookFilePath, variableFilePath, sudoPassword string, args ...string) (err error) {
73-
proc := ansible.processFactory.NewProcess(
73+
cmd := ansible.commandFactory.NewCommand(
7474
"HISTSIZE=0",
7575
"ansible-playbook",
7676
playbookFilePath,
7777
"--extra-vars", "@"+variableFilePath,
7878
"--extra-vars", "ansible_become_password="+sudoPassword,
7979
)
80-
proc.SetSecretArguments(6)
80+
cmd.SetSecretArguments(6)
8181

8282
if terminal.IsVerbose() {
83-
proc.AddArguments("-" + strings.Repeat("v", terminal.GetLogLevel()-1))
83+
cmd.AddArguments("-" + strings.Repeat("v", terminal.GetLogLevel()-1))
8484
}
8585

8686
if len(args) > 0 {
87-
proc.AddArguments(args...)
87+
cmd.AddArguments(args...)
8888
}
8989

90-
printer.Printf("\n<comment>$ %s</comment>\n", proc.String())
91-
if err = proc.Run(); nil != err {
90+
printer.Printf("\n<comment>$ %s</comment>\n", cmd.String())
91+
if err = cmd.Run(); nil != err {
9292
return err
9393
}
9494

@@ -99,13 +99,13 @@ func (ansible *Ansible) isInstalled() (installed bool, err error) {
9999
var path string
100100
var realPath string
101101

102-
if path, err = ansible.processFactory.NewProcess("which", "ansible").Output(); nil != err && "exit status 1" != err.Error() {
102+
if path, err = ansible.commandFactory.NewCommand("which", "ansible").Output(); nil != err && "exit status 1" != err.Error() {
103103
return false, err
104104
}
105105
path = strings.TrimSpace(path)
106106

107107
if "" != path {
108-
if realPath, err = ansible.processFactory.NewProcess("ls", path).Output(); nil != err {
108+
if realPath, err = ansible.commandFactory.NewCommand("ls", path).Output(); nil != err {
109109
return false, err
110110
}
111111

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package process
1+
package command
22

33
import (
44
"fmt"
Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package process
1+
package command
22

33
import (
44
"bytes"
@@ -7,13 +7,13 @@ import (
77
"strings"
88
)
99

10-
type Process struct {
10+
type Command struct {
1111
preArguments []*argument
1212
arguments []*argument
1313
}
1414

15-
func New(arguments ...string) *Process {
16-
p := &Process{
15+
func New(arguments ...string) *Command {
16+
p := &Command{
1717
preArguments: []*argument{},
1818
arguments: []*argument{},
1919
}
@@ -22,23 +22,23 @@ func New(arguments ...string) *Process {
2222
return p
2323
}
2424

25-
func NewSudoProcess(sudoPassword string, arguments ...string) *Process {
26-
process := New(arguments...).AddPreArguments("HISTSIZE=0", "echo", sudoPassword, "|", "sudo", "-S")
27-
process.SetSecretPreArguments(2)
25+
func NewSudoCommand(sudoPassword string, arguments ...string) *Command {
26+
cmd := New(arguments...).AddPreArguments("HISTSIZE=0", "echo", sudoPassword, "|", "sudo", "-S")
27+
cmd.SetSecretPreArguments(2)
2828

29-
return process
29+
return cmd
3030
}
3131

32-
func NewWslProcess(distribution string, arguments ...string) *Process {
32+
func NewWslCommand(distribution string, arguments ...string) *Command {
3333
return New(arguments...).AddPreArguments("wsl", "-d", distribution)
3434
}
3535

36-
func NewWslSudoProcess(distribution string, arguments ...string) *Process {
36+
func NewWslSudoCommand(distribution string, arguments ...string) *Command {
3737
return New(arguments...).AddPreArguments("wsl", "-d", distribution, "-u", "root")
3838
}
3939

40-
func (p *Process) Run() (err error) {
41-
cmd := p.newCommand()
40+
func (p *Command) Run() (err error) {
41+
cmd := p.newExecCmd()
4242
cmd.Stdout = os.Stdout
4343
cmd.Stdin = os.Stdin
4444
cmd.Stderr = os.Stderr
@@ -50,10 +50,10 @@ func (p *Process) Run() (err error) {
5050
return nil
5151
}
5252

53-
func (p *Process) Output() (out string, err error) {
53+
func (p *Command) Output() (out string, err error) {
5454
var result []byte
5555

56-
if result, err = p.newCommand().CombinedOutput(); err != nil {
56+
if result, err = p.newExecCmd().CombinedOutput(); err != nil {
5757
return "", err
5858
}
5959

@@ -63,31 +63,31 @@ func (p *Process) Output() (out string, err error) {
6363
return string(result), err
6464
}
6565

66-
func (p *Process) String() string {
66+
func (p *Command) String() string {
6767
return p.toString(true)
6868
}
6969

70-
func (p *Process) StringWithSecret() string {
70+
func (p *Command) StringWithSecret() string {
7171
return p.toString(false)
7272
}
7373

74-
func (p *Process) AddPreArguments(arguments ...string) *Process {
74+
func (p *Command) AddPreArguments(arguments ...string) *Command {
7575
for _, arg := range arguments {
7676
p.preArguments = append(p.preArguments, &argument{Value: arg, IsSecret: false})
7777
}
7878

7979
return p
8080
}
8181

82-
func (p *Process) AddArguments(arguments ...string) *Process {
82+
func (p *Command) AddArguments(arguments ...string) *Command {
8383
for _, arg := range arguments {
8484
p.arguments = append(p.arguments, &argument{Value: arg, IsSecret: false})
8585
}
8686

8787
return p
8888
}
8989

90-
func (p *Process) SetSecretPreArguments(indexes ...int) *Process {
90+
func (p *Command) SetSecretPreArguments(indexes ...int) *Command {
9191
lastIndex := len(p.preArguments) - 1
9292

9393
for _, index := range indexes {
@@ -99,7 +99,7 @@ func (p *Process) SetSecretPreArguments(indexes ...int) *Process {
9999
return p
100100
}
101101

102-
func (p *Process) SetSecretArguments(indexes ...int) *Process {
102+
func (p *Command) SetSecretArguments(indexes ...int) *Command {
103103
lastIndex := len(p.arguments) - 1
104104

105105
for _, index := range indexes {
@@ -111,7 +111,7 @@ func (p *Process) SetSecretArguments(indexes ...int) *Process {
111111
return p
112112
}
113113

114-
func (p *Process) SetNormalPreArguments(indexes ...int) *Process {
114+
func (p *Command) SetNormalPreArguments(indexes ...int) *Command {
115115
lastIndex := len(p.preArguments) - 1
116116

117117
for _, index := range indexes {
@@ -123,7 +123,7 @@ func (p *Process) SetNormalPreArguments(indexes ...int) *Process {
123123
return p
124124
}
125125

126-
func (p *Process) SetNormalArguments(indexes ...int) *Process {
126+
func (p *Command) SetNormalArguments(indexes ...int) *Command {
127127
lastIndex := len(p.arguments) - 1
128128

129129
for _, index := range indexes {
@@ -135,7 +135,7 @@ func (p *Process) SetNormalArguments(indexes ...int) *Process {
135135
return p
136136
}
137137

138-
func (p *Process) newCommand() *exec.Cmd {
138+
func (p *Command) newExecCmd() *exec.Cmd {
139139
args := make([]string, len(p.preArguments)+len(p.arguments))
140140
index := 0
141141

@@ -152,7 +152,7 @@ func (p *Process) newCommand() *exec.Cmd {
152152
return exec.Command(args[0], args[1:]...)
153153
}
154154

155-
func (p *Process) toString(hideSecret bool) string {
155+
func (p *Command) toString(hideSecret bool) string {
156156
buffer := strings.Builder{}
157157
preArgumentLastIndex := len(p.preArguments) - 1
158158
argumentLastIndex := len(p.arguments) - 1

command/factory.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package command
2+
3+
type Factory interface {
4+
NewCommand(arguments ...string) *Command
5+
NewSudoCommand(sudoPassword string, arguments ...string) *Command
6+
}
7+
8+
type DefaultFactory struct {
9+
}
10+
11+
func NewFactory() *DefaultFactory {
12+
return &DefaultFactory{}
13+
}
14+
15+
func (f *DefaultFactory) NewCommand(arguments ...string) *Command {
16+
return New(arguments...)
17+
}
18+
19+
func (f *DefaultFactory) NewSudoCommand(sudoPassword string, arguments ...string) *Command {
20+
return NewSudoCommand(sudoPassword, arguments...)
21+
}
22+
23+
type WslFactory struct {
24+
distribution string
25+
}
26+
27+
func NewWslFactory(distribution string) *WslFactory {
28+
return &WslFactory{distribution: distribution}
29+
}
30+
31+
func (w *WslFactory) NewCommand(arguments ...string) *Command {
32+
return NewWslCommand(w.distribution, arguments...)
33+
}
34+
35+
func (w *WslFactory) NewSudoCommand(sudoPassword string, arguments ...string) *Command {
36+
return NewWslSudoCommand(w.distribution, arguments...)
37+
}

process/factory.go

Lines changed: 0 additions & 37 deletions
This file was deleted.

0 commit comments

Comments
 (0)