Skip to content

Commit 6c72e62

Browse files
committed
[#200] Rename vcs/cmd package to vcs/shell
1 parent 5125af9 commit 6c72e62

11 files changed

+88
-89
lines changed

src/vcs/git/git_command.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ package git
2525
import (
2626
"bufio"
2727
"bytes"
28-
"github.com/murex/tcr/vcs/cmd"
28+
"github.com/murex/tcr/vcs/shell"
2929
"strings"
3030
)
3131

32-
func newGitCommand() *cmd.ShellCommand {
33-
return cmd.New("git")
32+
func newGitCommand() *shell.Command {
33+
return shell.NewCommand("git")
3434
}
3535

3636
// IsGitCommandAvailable indicates if git command is available on local machine

src/vcs/p4/p4_command.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ package p4
2525
import (
2626
"bufio"
2727
"bytes"
28-
"github.com/murex/tcr/vcs/cmd"
28+
"github.com/murex/tcr/vcs/shell"
2929
"strings"
3030
)
3131

32-
func newP4Command(params ...string) *cmd.ShellCommand {
33-
return cmd.New("p4", params...)
32+
func newP4Command(params ...string) *shell.Command {
33+
return shell.NewCommand("p4", params...)
3434
}
3535

3636
// IsP4CommandAvailable indicates if p4 command is available on local machine
@@ -84,11 +84,11 @@ func runP4Command(params ...string) (output []byte, err error) {
8484
}
8585

8686
// tracePipedP4Command calls p4 command, pipes it to pipedTo command, and reports its output traces
87-
func tracePipedP4Command(pipedTo *cmd.ShellCommand, params ...string) error {
87+
func tracePipedP4Command(pipedTo *shell.Command, params ...string) error {
8888
return newP4Command().TraceAndPipe(pipedTo, params...)
8989
}
9090

9191
// runPipedP4Command calls p4 command, pipes it to pipedTo command, and reports its output traces
92-
func runPipedP4Command(pipedTo *cmd.ShellCommand, params ...string) (output []byte, err error) {
92+
func runPipedP4Command(pipedTo *shell.Command, params ...string) (output []byte, err error) {
9393
return newP4Command().RunAndPipe(pipedTo, params...)
9494
}

src/vcs/p4/p4_command_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ package p4
2424

2525
import (
2626
"github.com/murex/tcr/report"
27-
"github.com/murex/tcr/vcs/cmd"
27+
"github.com/murex/tcr/vcs/shell"
2828
"github.com/stretchr/testify/assert"
2929
"os"
3030
"strings"
@@ -67,7 +67,7 @@ func Test_trace_p4_command(t *testing.T) {
6767
func Test_run_piped_p4_command(t *testing.T) {
6868
skipWhenInGitHubActions(t)
6969
output, err := runPipedP4Command(
70-
cmd.New("grep", "Client name"),
70+
shell.NewCommand("grep", "Client name"),
7171
"info")
7272
assert.NoError(t, err)
7373
assert.Contains(t, string(output), "Client name:")
@@ -77,7 +77,7 @@ func Test_trace_piped_p4_command(t *testing.T) {
7777
skipWhenInGitHubActions(t)
7878
sniffer := report.NewSniffer()
7979
err := tracePipedP4Command(
80-
cmd.New("grep", "Client name"),
80+
shell.NewCommand("grep", "Client name"),
8181
"info")
8282
sniffer.Stop()
8383
assert.NoError(t, err)

src/vcs/p4/p4_impl.go

+10-11
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626
"bytes"
2727
"github.com/murex/tcr/report"
2828
"github.com/murex/tcr/vcs"
29-
"github.com/murex/tcr/vcs/cmd"
29+
"github.com/murex/tcr/vcs/shell"
3030
"github.com/spf13/afero"
3131
"golang.org/x/text/encoding/charmap"
3232
"golang.org/x/text/transform"
@@ -41,8 +41,8 @@ type p4Impl struct {
4141
filesystem afero.Fs
4242
runP4Function func(params ...string) (output []byte, err error)
4343
traceP4Function func(params ...string) (err error)
44-
runPipedP4Function func(cmd *cmd.ShellCommand, params ...string) (output []byte, err error)
45-
tracePipedP4Function func(cmd *cmd.ShellCommand, params ...string) (err error)
44+
runPipedP4Function func(toCmd *shell.Command, params ...string) (output []byte, err error)
45+
tracePipedP4Function func(toCmd *shell.Command, params ...string) (err error)
4646
}
4747

4848
// New initializes the p4 implementation based on the provided directory from local clone
@@ -127,7 +127,6 @@ type changeList struct {
127127
}
128128

129129
// Commit commits changes to p4 index.
130-
// TODO: p4 submit
131130
func (p *p4Impl) Commit(_ bool, messages ...string) error {
132131
cl, err := p.createChangeList(messages...)
133132
if err != nil {
@@ -152,8 +151,9 @@ func (*p4Impl) Revert() error {
152151
}
153152

154153
// Push runs a push operation.
155-
// TODO: confirm that it does nothing as already submitted to the server through commit?
156154
func (*p4Impl) Push() error {
155+
// Nothing to do in case of p4, as the "p4 submit" done in Commit()
156+
// already "pushed" the changes to the server.
157157
return nil
158158
}
159159

@@ -220,17 +220,16 @@ func (p *p4Impl) traceP4(args ...string) error {
220220

221221
// runP4 calls p4 command in a separate process and returns its output traces
222222
// The command is launched from the p4 root directory
223-
func (p *p4Impl) runP4(args ...string) (output []byte, err error) {
224-
return p.runP4Function(append([]string{"-d", p.GetRootDir()}, args...)...)
225-
}
223+
//func (p *p4Impl) runP4(args ...string) (output []byte, err error) {
224+
// return p.runP4Function(append([]string{"-d", p.GetRootDir()}, args...)...)
225+
//}
226226

227227
func (p *p4Impl) createChangeList(messages ...string) (*changeList, error) {
228228
// Command: p4 --field "Description=<message>" change -o | p4 change -i
229-
230229
out, err := p.runPipedP4Function(
231230
newP4Command("change", "-i"),
232231
"-Q", "utf8",
233-
"--field", buildDescriptionField(cmd.GetShellAttributes(), messages...),
232+
"--field", buildDescriptionField(shell.GetAttributes(), messages...),
234233
"change", "-o")
235234
if err != nil {
236235
return nil, err
@@ -240,7 +239,7 @@ func (p *p4Impl) createChangeList(messages ...string) (*changeList, error) {
240239
return &changeList{clNumber}, err
241240
}
242241

243-
func buildDescriptionField(attr cmd.ShellAttributes, messages ...string) string {
242+
func buildDescriptionField(attr shell.Attributes, messages ...string) string {
244243
var builder strings.Builder
245244
_, _ = builder.WriteString("Description=")
246245
for _, message := range messages {

src/vcs/cmd/command.go src/vcs/shell/command.go

+23-23
Original file line numberDiff line numberDiff line change
@@ -20,66 +20,66 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2020
SOFTWARE.
2121
*/
2222

23-
package cmd
23+
package shell
2424

2525
import (
2626
"github.com/codeskyblue/go-sh"
2727
"github.com/murex/tcr/report"
2828
"os/exec"
2929
)
3030

31-
// ShellCommand is a command that can be launched from a shell
32-
type ShellCommand struct {
31+
// Command is a command that can be launched from a shell
32+
type Command struct {
3333
name string
3434
params []string
3535
}
3636

37-
// New creates a new shell command instance
38-
func New(name string, params ...string) *ShellCommand {
39-
return &ShellCommand{name: name, params: params}
37+
// NewCommand creates a new shell command instance
38+
func NewCommand(name string, params ...string) *Command {
39+
return &Command{name: name, params: params}
4040
}
4141

4242
// IsInPath indicates if the command can be found in the path
43-
func (sc *ShellCommand) IsInPath() bool {
44-
_, err := exec.LookPath(sc.name)
43+
func (c *Command) IsInPath() bool {
44+
_, err := exec.LookPath(c.name)
4545
return err == nil
4646
}
4747

4848
// GetFullPath returns the full path for this command
49-
func (sc *ShellCommand) GetFullPath() string {
50-
path, _ := exec.LookPath(sc.name)
49+
func (c *Command) GetFullPath() string {
50+
path, _ := exec.LookPath(c.name)
5151
return path
5252
}
5353

5454
// Run calls the command with the provided parameters in a separate process and returns its output traces combined
55-
func (sc *ShellCommand) Run(params ...string) (output []byte, err error) {
56-
//report.PostWarning("Command: ", sc.name, " ", append(sc.params, params...))
57-
return sh.Command(sc.name, append(sc.params, params...)).CombinedOutput()
55+
func (c *Command) Run(params ...string) (output []byte, err error) {
56+
//report.PostWarning("Command: ", c.name, " ", append(c.params, params...))
57+
return sh.Command(c.name, append(c.params, params...)).CombinedOutput()
5858
}
5959

6060
// Trace calls the command with the provided parameters and reports its output traces
61-
func (sc *ShellCommand) Trace(params ...string) error {
62-
output, err := sc.Run(params...)
61+
func (c *Command) Trace(params ...string) error {
62+
output, err := c.Run(params...)
6363
if len(output) > 0 {
6464
report.PostText(string(output))
6565
}
6666
return err
6767
}
6868

6969
// RunAndPipe calls the command with the provided parameters in a separate process
70-
// and pipes its output to cmd. Returns cmd's output traces combined
71-
func (sc *ShellCommand) RunAndPipe(cmd *ShellCommand, params ...string) (output []byte, err error) {
72-
//report.PostWarning("Command: ", sc.name, " ", append(sc.params, params...), " | ", cmd.name, " ", cmd.params)
70+
// and pipes its output to cmd. Returns toCmd's output traces combined
71+
func (c *Command) RunAndPipe(toCmd *Command, params ...string) (output []byte, err error) {
72+
//report.PostWarning("Command: ", c.name, " ", append(c.params, params...), " | ", shell.name, " ", shell.params)
7373
return sh.NewSession().
74-
Command(sc.name, append(sc.params, params...)).
75-
Command(cmd.name, cmd.params).
74+
Command(c.name, append(c.params, params...)).
75+
Command(toCmd.name, toCmd.params).
7676
CombinedOutput()
7777
}
7878

7979
// TraceAndPipe calls the command with the provided parameters in a separate process
80-
// and pipes its output to cmd. Reports cmd's output traces
81-
func (sc *ShellCommand) TraceAndPipe(cmd *ShellCommand, params ...string) error {
82-
output, err := sc.RunAndPipe(cmd, params...)
80+
// and pipes its output to cmd. Reports toCmd's output traces
81+
func (c *Command) TraceAndPipe(toCmd *Command, params ...string) error {
82+
output, err := c.RunAndPipe(toCmd, params...)
8383
if len(output) > 0 {
8484
report.PostText(string(output))
8585
}

src/vcs/cmd/command_test.go src/vcs/shell/command_test.go

+27-27
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2020
SOFTWARE.
2121
*/
2222

23-
package cmd
23+
package shell
2424

2525
import (
2626
"bytes"
@@ -32,45 +32,45 @@ import (
3232
)
3333

3434
func Test_is_in_path_for_a_valid_command(t *testing.T) {
35-
assert.True(t, New("ls").IsInPath())
35+
assert.True(t, NewCommand("ls").IsInPath())
3636
}
3737

3838
func Test_is_in_path_for_an_invalid_command(t *testing.T) {
39-
assert.False(t, New("unknown-command").IsInPath())
39+
assert.False(t, NewCommand("unknown-command").IsInPath())
4040
}
4141

4242
func Test_get_full_path_for_a_valid_command(t *testing.T) {
43-
base := filepath.Base(New("ls").GetFullPath())
43+
base := filepath.Base(NewCommand("ls").GetFullPath())
4444
assert.Equal(t, strings.TrimSuffix(base, ".exe"), "ls")
4545
}
4646

4747
func Test_get_full_path_for_an_invalid_command(t *testing.T) {
48-
assert.Zero(t, New("unknown-command").GetFullPath())
48+
assert.Zero(t, NewCommand("unknown-command").GetFullPath())
4949
}
5050

5151
func Test_run_valid_command_with_initial_parameters(t *testing.T) {
52-
output, err := New("echo", "hello world!").Run()
52+
output, err := NewCommand("echo", "hello world!").Run()
5353
assert.NoError(t, err)
5454
trimmed := string(bytes.TrimRight(output, "\r\n"))
5555
assert.Equal(t, "hello world!", trimmed)
5656
}
5757

5858
func Test_run_valid_command_with_additional_parameters(t *testing.T) {
59-
output, err := New("echo").Run("hello world!")
59+
output, err := NewCommand("echo").Run("hello world!")
6060
assert.NoError(t, err)
6161
trimmed := string(bytes.TrimRight(output, "\r\n"))
6262
assert.Equal(t, "hello world!", trimmed)
6363
}
6464

6565
func Test_run_invalid_command(t *testing.T) {
66-
output, err := New("unknown-command").Run()
66+
output, err := NewCommand("unknown-command").Run()
6767
assert.Error(t, err)
6868
assert.Zero(t, output)
6969
}
7070

7171
func Test_trace_valid_command_with_initial_parameters(t *testing.T) {
7272
sniffer := report.NewSniffer()
73-
err := New("echo", "hello world!").Trace()
73+
err := NewCommand("echo", "hello world!").Trace()
7474
sniffer.Stop()
7575
assert.NoError(t, err)
7676
assert.Equal(t, 1, sniffer.GetMatchCount())
@@ -80,7 +80,7 @@ func Test_trace_valid_command_with_initial_parameters(t *testing.T) {
8080

8181
func Test_trace_valid_command_with_additional_parameters(t *testing.T) {
8282
sniffer := report.NewSniffer()
83-
err := New("echo").Trace("hello world!")
83+
err := NewCommand("echo").Trace("hello world!")
8484
sniffer.Stop()
8585
assert.NoError(t, err)
8686
assert.Equal(t, 1, sniffer.GetMatchCount())
@@ -90,47 +90,47 @@ func Test_trace_valid_command_with_additional_parameters(t *testing.T) {
9090

9191
func Test_trace_invalid_command(t *testing.T) {
9292
sniffer := report.NewSniffer()
93-
err := New("unknown-command").Trace()
93+
err := NewCommand("unknown-command").Trace()
9494
sniffer.Stop()
9595
assert.Error(t, err)
9696
assert.Equal(t, 0, sniffer.GetMatchCount())
9797
}
9898

9999
func Test_run_pipe_valid_commands_with_initial_parameters(t *testing.T) {
100-
output, err := New("echo", "hello\tworld!").RunAndPipe(
101-
New("cut", "-f", "1"))
100+
output, err := NewCommand("echo", "hello\tworld!").RunAndPipe(
101+
NewCommand("cut", "-f", "1"))
102102
assert.NoError(t, err)
103103
trimmed := string(bytes.TrimRight(output, "\r\n"))
104104
assert.Equal(t, "hello", trimmed)
105105
}
106106

107107
func Test_run_pipe_valid_commands_with_additional_parameters(t *testing.T) {
108-
output, err := New("echo").RunAndPipe(
109-
New("cut", "-f", "1"),
108+
output, err := NewCommand("echo").RunAndPipe(
109+
NewCommand("cut", "-f", "1"),
110110
"hello\tworld!")
111111
assert.NoError(t, err)
112112
trimmed := string(bytes.TrimRight(output, "\r\n"))
113113
assert.Equal(t, "hello", trimmed)
114114
}
115115

116116
func Test_run_pipe_with_first_command_invalid(t *testing.T) {
117-
output, err := New("unknown-command").RunAndPipe(
118-
New("cut", "-f", "1"))
117+
output, err := NewCommand("unknown-command").RunAndPipe(
118+
NewCommand("cut", "-f", "1"))
119119
assert.Error(t, err)
120120
assert.Zero(t, output)
121121
}
122122

123123
func Test_run_pipe_with_second_command_invalid(t *testing.T) {
124-
output, err := New("echo").RunAndPipe(
125-
New("unknown-command"))
124+
output, err := NewCommand("echo").RunAndPipe(
125+
NewCommand("unknown-command"))
126126
assert.Error(t, err)
127127
assert.Zero(t, output)
128128
}
129129

130130
func Test_trace_pipe_valid_commands_with_initial_parameters(t *testing.T) {
131131
sniffer := report.NewSniffer()
132-
err := New("echo", "hello\tworld!").TraceAndPipe(
133-
New("cut", "-f", "1"))
132+
err := NewCommand("echo", "hello\tworld!").TraceAndPipe(
133+
NewCommand("cut", "-f", "1"))
134134
sniffer.Stop()
135135
assert.NoError(t, err)
136136
assert.Equal(t, 1, sniffer.GetMatchCount())
@@ -140,8 +140,8 @@ func Test_trace_pipe_valid_commands_with_initial_parameters(t *testing.T) {
140140

141141
func Test_trace_pipe_valid_commands_with_additional_parameters(t *testing.T) {
142142
sniffer := report.NewSniffer()
143-
err := New("echo").TraceAndPipe(
144-
New("cut", "-f", "1"),
143+
err := NewCommand("echo").TraceAndPipe(
144+
NewCommand("cut", "-f", "1"),
145145
"hello\tworld!")
146146
sniffer.Stop()
147147
assert.NoError(t, err)
@@ -152,17 +152,17 @@ func Test_trace_pipe_valid_commands_with_additional_parameters(t *testing.T) {
152152

153153
func Test_trace_pipe_with_first_command_invalid(t *testing.T) {
154154
sniffer := report.NewSniffer()
155-
err := New("unknown-command").TraceAndPipe(
156-
New("cut", "-f", "1"))
155+
err := NewCommand("unknown-command").TraceAndPipe(
156+
NewCommand("cut", "-f", "1"))
157157
sniffer.Stop()
158158
assert.Error(t, err)
159159
assert.Equal(t, 0, sniffer.GetMatchCount())
160160
}
161161

162162
func Test_trace_pipe_with_second_command_invalid(t *testing.T) {
163163
sniffer := report.NewSniffer()
164-
err := New("echo").TraceAndPipe(
165-
New("unknown-command"))
164+
err := NewCommand("echo").TraceAndPipe(
165+
NewCommand("unknown-command"))
166166
sniffer.Stop()
167167
assert.Error(t, err)
168168
assert.Equal(t, 0, sniffer.GetMatchCount())

0 commit comments

Comments
 (0)