mirror of
https://github.com/x-motemen/ghq.git
synced 2026-09-10 07:26:27 -04:00
feat(get): add --print flag to output local repository path
This commit is contained in:
parent
531d4f1a5f
commit
e7882d7169
20
cmd_get.go
20
cmd_get.go
|
|
@ -21,10 +21,11 @@ import (
|
|||
|
||||
func doGet(c *cli.Context) error {
|
||||
var (
|
||||
args = c.Args().Slice()
|
||||
andLook = c.Bool("look")
|
||||
parallel = c.Bool("parallel")
|
||||
silent = c.Bool("silent")
|
||||
args = c.Args().Slice()
|
||||
andLook = c.Bool("look")
|
||||
parallel = c.Bool("parallel")
|
||||
silent = c.Bool("silent")
|
||||
printPath = c.Bool("print")
|
||||
)
|
||||
g := &getter{
|
||||
update: c.Bool("update"),
|
||||
|
|
@ -74,8 +75,12 @@ func doGet(c *cli.Context) error {
|
|||
sem <- struct{}{}
|
||||
eg.Go(func() error {
|
||||
defer func() { <-sem }()
|
||||
if getInfo, err = g.get(target); err != nil {
|
||||
logger.Logf("error", "failed to get %q: %s", target, err)
|
||||
info, getErr := g.get(target)
|
||||
getInfo, err = info, getErr
|
||||
if getErr != nil {
|
||||
logger.Logf("error", "failed to get %q: %s", target, getErr)
|
||||
} else if printPath && info.localRepository != nil {
|
||||
fmt.Println(info.localRepository.FullPath)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
|
@ -83,6 +88,9 @@ func doGet(c *cli.Context) error {
|
|||
if getInfo, err = g.get(target); err != nil {
|
||||
return fmt.Errorf("failed to get %q: %w", target, err)
|
||||
}
|
||||
if printPath && getInfo.localRepository != nil {
|
||||
fmt.Println(getInfo.localRepository.FullPath)
|
||||
}
|
||||
}
|
||||
}
|
||||
if err = scr.Err(); err != nil {
|
||||
|
|
|
|||
|
|
@ -303,6 +303,73 @@ func TestCommandGet(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestCommandGet_print(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
args []string
|
||||
inputRepos []string
|
||||
}{{
|
||||
name: "single repo",
|
||||
args: []string{"", "get", "--print", "motemen/ghq-test-repo"},
|
||||
inputRepos: nil,
|
||||
}, {
|
||||
name: "bulk from stdin",
|
||||
args: []string{"", "get", "--print"},
|
||||
inputRepos: []string{"github.com/x-motemen/ghq", "github.com/motemen/gore"},
|
||||
}, {
|
||||
name: "bulk parallel",
|
||||
args: []string{"", "get", "--print", "--parallel"},
|
||||
inputRepos: []string{"github.com/x-motemen/ghq", "github.com/motemen/gore"},
|
||||
}}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
withFakeGitBackend(t, func(t *testing.T, tmpRoot string, _ *_cloneArgs, _ *_updateArgs) {
|
||||
// pre-create dirs for bulk cases
|
||||
for _, r := range tc.inputRepos {
|
||||
os.MkdirAll(filepath.Join(tmpRoot, r, ".git"), 0755)
|
||||
}
|
||||
|
||||
var out string
|
||||
var err error
|
||||
if len(tc.inputRepos) > 0 {
|
||||
out, _, err = captureWithInput(tc.inputRepos, func() {
|
||||
newApp().Run(tc.args)
|
||||
})
|
||||
} else {
|
||||
out, _, err = capture(func() {
|
||||
newApp().Run(tc.args)
|
||||
})
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatalf("capture error: %s", err)
|
||||
}
|
||||
|
||||
lines := strings.Split(strings.TrimRight(out, "\n"), "\n")
|
||||
if len(tc.inputRepos) == 0 {
|
||||
// single repo: output should contain the local path
|
||||
if len(lines) != 1 || lines[0] == "" {
|
||||
t.Errorf("expected one path in output, got: %q", out)
|
||||
}
|
||||
if !filepath.IsAbs(lines[0]) {
|
||||
t.Errorf("expected absolute path, got: %q", lines[0])
|
||||
}
|
||||
} else {
|
||||
// bulk: one path per repo
|
||||
if len(lines) != len(tc.inputRepos) {
|
||||
t.Errorf("expected %d paths, got %d: %q", len(tc.inputRepos), len(lines), out)
|
||||
}
|
||||
for _, line := range lines {
|
||||
if !filepath.IsAbs(line) {
|
||||
t.Errorf("expected absolute path, got: %q", line)
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestLook(t *testing.T) {
|
||||
withFakeGitBackend(t, func(t *testing.T, tmproot string, _ *_cloneArgs, _ *_updateArgs) {
|
||||
os.MkdirAll(filepath.Join(tmproot, "github.com", "motemen", "ghq", ".git"), 0755)
|
||||
|
|
|
|||
|
|
@ -40,6 +40,7 @@ var commandGet = &cli.Command{
|
|||
Usage: "Specify `branch` name. This flag implies --single-branch on Git"},
|
||||
&cli.BoolFlag{Name: "parallel", Aliases: []string{"P"}, Usage: "Import parallelly"},
|
||||
&cli.BoolFlag{Name: "bare", Usage: "Do a bare clone"},
|
||||
&cli.BoolFlag{Name: "print", Usage: "Print local repository path after get"},
|
||||
&cli.StringFlag{
|
||||
Name: "partial",
|
||||
Usage: "Do a partial clone. Can specify either \"blobless\" or \"treeless\"",
|
||||
|
|
@ -107,7 +108,7 @@ type commandDoc struct {
|
|||
}
|
||||
|
||||
var commandDocs = map[string]commandDoc{
|
||||
"get": {"", "[-u] [-p] [--shallow] [--vcs <vcs>] [--look] [--silent] [--branch <branch>] [--no-recursive] [--bare] [--partial blobless|treeless] <repository URL>|<project>|<user>/<project>|<host>/<user>/<project>"},
|
||||
"get": {"", "[-u] [-p] [--shallow] [--vcs <vcs>] [--look] [--silent] [--branch <branch>] [--no-recursive] [--bare] [--partial blobless|treeless] [--print] <repository URL>|<project>|<user>/<project>|<host>/<user>/<project>"},
|
||||
"list": {"", "[-p] [-e] [<query>]"},
|
||||
"create": {"", "<project>|<user>/<project>|<host>/<user>/<project>"},
|
||||
"rm": {"", "<project>|<user>/<project>|<host>/<user>/<project>"},
|
||||
|
|
|
|||
Loading…
Reference in a new issue