зеркало из https://github.com/mislav/hub.git
[cmd] Improve argument display in `--noop` and debug modes
This commit is contained in:
Родитель
bc70e565e5
Коммит
3df552668f
14
cmd/cmd.go
14
cmd/cmd.go
|
@ -21,7 +21,17 @@ type Cmd struct {
|
|||
}
|
||||
|
||||
func (cmd Cmd) String() string {
|
||||
return fmt.Sprintf("%s %s", cmd.Name, strings.Join(cmd.Args, " "))
|
||||
args := make([]string, len(cmd.Args))
|
||||
for i, a := range cmd.Args {
|
||||
if strings.ContainsRune(a, '"') {
|
||||
args[i] = fmt.Sprintf(`'%s'`, a)
|
||||
} else if a == "" || strings.ContainsRune(a, '\'') || strings.ContainsRune(a, ' ') {
|
||||
args[i] = fmt.Sprintf(`"%s"`, a)
|
||||
} else {
|
||||
args[i] = a
|
||||
}
|
||||
}
|
||||
return fmt.Sprintf("%s %s", cmd.Name, strings.Join(args, " "))
|
||||
}
|
||||
|
||||
// WithArg returns the current argument
|
||||
|
@ -138,7 +148,7 @@ func NewWithArray(cmd []string) *Cmd {
|
|||
|
||||
func verboseLog(cmd *Cmd) {
|
||||
if os.Getenv("HUB_VERBOSE") != "" {
|
||||
msg := fmt.Sprintf("$ %s %s", cmd.Name, strings.Join(cmd.Args, " "))
|
||||
msg := fmt.Sprintf("$ %s", cmd.String())
|
||||
if ui.IsTerminal(os.Stderr) {
|
||||
msg = fmt.Sprintf("\033[35m%s\033[0m", msg)
|
||||
}
|
||||
|
|
|
@ -18,3 +18,11 @@ func TestWithArg(t *testing.T) {
|
|||
assert.Equal(t, "git", execCmd.Name)
|
||||
assert.Equal(t, 4, len(execCmd.Args))
|
||||
}
|
||||
|
||||
func Test_String(t *testing.T) {
|
||||
c := Cmd{
|
||||
Name: "echo",
|
||||
Args: []string{"hi", "hello world", "don't", `"fake news"`},
|
||||
}
|
||||
assert.Equal(t, `echo hi "hello world" "don't" '"fake news"'`, c.String())
|
||||
}
|
||||
|
|
|
@ -57,13 +57,13 @@ func TestInitInAnotherDir(t *testing.T) {
|
|||
|
||||
commands := args.Commands()
|
||||
assert.Equal(t, 2, len(commands))
|
||||
assert.Equal(t, "git init --template mytpl --shared=umask my project", commands[0].String())
|
||||
assert.Equal(t, "git init --template mytpl --shared=umask \"my project\"", commands[0].String())
|
||||
|
||||
currentDir, err := os.Getwd()
|
||||
assert.Equal(t, nil, err)
|
||||
|
||||
expected := fmt.Sprintf(
|
||||
"git --git-dir %s remote add origin git@github.com:jingweno/%s.git",
|
||||
"git --git-dir \"%s\" remote add origin git@github.com:jingweno/%s.git",
|
||||
filepath.Join(currentDir, "my project", ".git"),
|
||||
"my-project",
|
||||
)
|
||||
|
|
Загрузка…
Ссылка в новой задаче