Skip to content

Commit 59f239b

Browse files
author
Fotios Lindiakos
committed
Parse args to find an explicit directory name
1 parent ed62b7b commit 59f239b

File tree

2 files changed

+96
-4
lines changed

2 files changed

+96
-4
lines changed

command/repo.go

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,14 @@ A repository can be supplied as an argument in any of the following formats:
5151
}
5252

5353
var repoCloneCmd = &cobra.Command{
54-
Use: "clone <repo>",
54+
Use: "clone <repo> [<directory>]",
5555
Args: cobra.MinimumNArgs(1),
5656
Short: "Clone a repository locally",
5757
Long: `Clone a GitHub repository locally.
5858
59-
To pass 'git clone' options, separate them with '--'.`,
59+
To pass 'git clone' options, separate them with '--'.
60+
61+
In order to clone to a specific directory, provide it before the '--' instead of after the other additional options.`,
6062
RunE: repoClone,
6163
}
6264

@@ -87,9 +89,29 @@ With no argument, the repository for the current directory is displayed.`,
8789
RunE: repoView,
8890
}
8991

92+
func parseExtraArgs(extraArgs []string) (args []string, target string) {
93+
args = extraArgs
94+
95+
if len(args) > 0 {
96+
if !strings.HasPrefix(args[0], "-") {
97+
target, args = args[0], args[1:]
98+
}
99+
}
100+
return
101+
}
102+
90103
func runClone(cloneURL string, args []string) (target string, err error) {
91-
cloneArgs := append(args, cloneURL)
92-
target = path.Base(strings.TrimSuffix(cloneURL, ".git"))
104+
cloneArgs, target := parseExtraArgs(args)
105+
106+
cloneArgs = append(cloneArgs, cloneURL)
107+
108+
// If the args contain an explicit target, pass it to clone
109+
// otherwise, parse the URL to determine where git cloned it to so we can return it
110+
if target != "" {
111+
cloneArgs = append(cloneArgs, target)
112+
} else {
113+
target = path.Base(strings.TrimSuffix(cloneURL, ".git"))
114+
}
93115

94116
cloneArgs = append([]string{"clone"}, cloneArgs...)
95117

command/repo_test.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/json"
66
"io/ioutil"
77
"os/exec"
8+
"reflect"
89
"regexp"
910
"strings"
1011
"testing"
@@ -337,6 +338,65 @@ func TestRepoFork_in_parent_survey_no(t *testing.T) {
337338
}
338339
}
339340

341+
func TestParseExtraArgs(t *testing.T) {
342+
type Wanted struct {
343+
args []string
344+
dir string
345+
}
346+
tests := []struct {
347+
name string
348+
args []string
349+
want Wanted
350+
}{
351+
{
352+
name: "args and target",
353+
args: []string{"target_directory", "-o", "upstream", "--depth", "1"},
354+
want: Wanted{
355+
args: []string{"-o", "upstream", "--depth", "1"},
356+
dir: "target_directory",
357+
},
358+
},
359+
{
360+
name: "only args",
361+
args: []string{"-o", "upstream", "--depth", "1"},
362+
want: Wanted{
363+
args: []string{"-o", "upstream", "--depth", "1"},
364+
dir: "",
365+
},
366+
},
367+
{
368+
name: "only target",
369+
args: []string{"target_directory"},
370+
want: Wanted{
371+
args: []string{},
372+
dir: "target_directory",
373+
},
374+
},
375+
{
376+
name: "no args",
377+
args: []string{},
378+
want: Wanted{
379+
args: []string{},
380+
dir: "",
381+
},
382+
},
383+
}
384+
for _, tt := range tests {
385+
t.Run(tt.name, func(t *testing.T) {
386+
args, dir := parseExtraArgs(tt.args)
387+
got := Wanted{
388+
args: args,
389+
dir: dir,
390+
}
391+
392+
if !reflect.DeepEqual(got, tt.want) {
393+
t.Errorf("got %#v want %#v", got, tt.want)
394+
}
395+
})
396+
}
397+
398+
}
399+
340400
func TestRepoClone(t *testing.T) {
341401
tests := []struct {
342402
name string
@@ -348,11 +408,21 @@ func TestRepoClone(t *testing.T) {
348408
args: "repo clone OWNER/REPO",
349409
want: "git clone https://github.com/OWNER/REPO.git",
350410
},
411+
{
412+
name: "shorthand with directory",
413+
args: "repo clone OWNER/REPO target_directory",
414+
want: "git clone https://github.com/OWNER/REPO.git target_directory",
415+
},
351416
{
352417
name: "clone arguments",
353418
args: "repo clone OWNER/REPO -- -o upstream --depth 1",
354419
want: "git clone -o upstream --depth 1 https://github.com/OWNER/REPO.git",
355420
},
421+
{
422+
name: "clone arguments with directory",
423+
args: "repo clone OWNER/REPO target_directory -- -o upstream --depth 1",
424+
want: "git clone -o upstream --depth 1 https://github.com/OWNER/REPO.git target_directory",
425+
},
356426
{
357427
name: "HTTPS URL",
358428
args: "repo clone https://github.com/OWNER/REPO",

0 commit comments

Comments
 (0)