mirror of
https://github.com/x-motemen/ghq.git
synced 2026-09-10 07:26:27 -04:00
separate list.go and list_test.go from commands*.go
This commit is contained in:
parent
9cdacb4356
commit
a716c9dd55
90
commands.go
90
commands.go
|
|
@ -266,96 +266,6 @@ func getRemoteRepository(remote RemoteRepository, doUpdate bool, isShallow bool,
|
|||
return nil
|
||||
}
|
||||
|
||||
func doList(c *cli.Context) error {
|
||||
var (
|
||||
w = c.App.Writer
|
||||
query = c.Args().First()
|
||||
exact = c.Bool("exact")
|
||||
printFullPaths = c.Bool("full-path")
|
||||
printUniquePaths = c.Bool("unique")
|
||||
)
|
||||
|
||||
var filterFn func(*LocalRepository) bool
|
||||
if query == "" {
|
||||
filterFn = func(_ *LocalRepository) bool {
|
||||
return true
|
||||
}
|
||||
} else {
|
||||
if hasSchemePattern.MatchString(query) || scpLikeURLPattern.MatchString(query) {
|
||||
if url, err := newURL(query); err == nil {
|
||||
if repo, err := LocalRepositoryFromURL(url); err == nil {
|
||||
query = repo.RelPath
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if exact {
|
||||
filterFn = func(repo *LocalRepository) bool {
|
||||
return repo.Matches(query)
|
||||
}
|
||||
} else {
|
||||
var host string
|
||||
paths := strings.Split(query, "/")
|
||||
if len(paths) > 1 && looksLikeAuthorityPattern.MatchString(paths[0]) {
|
||||
query = strings.Join(paths[1:], "/")
|
||||
host = paths[0]
|
||||
}
|
||||
filterFn = func(repo *LocalRepository) bool {
|
||||
return strings.Contains(repo.NonHostPath(), query) &&
|
||||
(host == "" || repo.PathParts[0] == host)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
repos := []*LocalRepository{}
|
||||
if err := walkLocalRepositories(func(repo *LocalRepository) {
|
||||
if !filterFn(repo) {
|
||||
return
|
||||
}
|
||||
repos = append(repos, repo)
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if printUniquePaths {
|
||||
subpathCount := map[string]int{} // Count duplicated subpaths (ex. foo/dotfiles and bar/dotfiles)
|
||||
reposCount := map[string]int{} // Check duplicated repositories among roots
|
||||
|
||||
// Primary first
|
||||
for _, repo := range repos {
|
||||
if reposCount[repo.RelPath] == 0 {
|
||||
for _, p := range repo.Subpaths() {
|
||||
subpathCount[p] = subpathCount[p] + 1
|
||||
}
|
||||
}
|
||||
|
||||
reposCount[repo.RelPath] = reposCount[repo.RelPath] + 1
|
||||
}
|
||||
|
||||
for _, repo := range repos {
|
||||
if reposCount[repo.RelPath] > 1 && repo.IsUnderPrimaryRoot() == false {
|
||||
continue
|
||||
}
|
||||
|
||||
for _, p := range repo.Subpaths() {
|
||||
if subpathCount[p] == 1 {
|
||||
fmt.Fprintln(w, p)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for _, repo := range repos {
|
||||
if printFullPaths {
|
||||
fmt.Fprintln(w, repo.FullPath)
|
||||
} else {
|
||||
fmt.Fprintln(w, repo.RelPath)
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func detectShell() string {
|
||||
shell := os.Getenv("SHELL")
|
||||
if shell != "" {
|
||||
|
|
|
|||
|
|
@ -228,50 +228,6 @@ func TestCommandGet(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestCommandList(t *testing.T) {
|
||||
_, _, err := capture(func() {
|
||||
app := cli.NewApp()
|
||||
flagSet := flagSet("list", commandList.Flags)
|
||||
c := cli.NewContext(app, flagSet, nil)
|
||||
|
||||
doList(c)
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("error should be nil, but: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCommandListUnique(t *testing.T) {
|
||||
_, _, err := capture(func() {
|
||||
app := cli.NewApp()
|
||||
flagSet := flagSet("list", commandList.Flags)
|
||||
flagSet.Parse([]string{"--unique"})
|
||||
c := cli.NewContext(app, flagSet, nil)
|
||||
|
||||
doList(c)
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("error should be nil, but: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCommandListUnknown(t *testing.T) {
|
||||
_, _, err := capture(func() {
|
||||
app := cli.NewApp()
|
||||
flagSet := flagSet("list", commandList.Flags)
|
||||
flagSet.Parse([]string{"--unknown-flag"})
|
||||
c := cli.NewContext(app, flagSet, nil)
|
||||
|
||||
doList(c)
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("error should be nil, but: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDoRoot(t *testing.T) {
|
||||
ghqrootEnv := "GHQ_ROOT"
|
||||
testCases := []struct {
|
||||
|
|
|
|||
98
list.go
Normal file
98
list.go
Normal file
|
|
@ -0,0 +1,98 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
func doList(c *cli.Context) error {
|
||||
var (
|
||||
w = c.App.Writer
|
||||
query = c.Args().First()
|
||||
exact = c.Bool("exact")
|
||||
printFullPaths = c.Bool("full-path")
|
||||
printUniquePaths = c.Bool("unique")
|
||||
)
|
||||
|
||||
var filterFn func(*LocalRepository) bool
|
||||
if query == "" {
|
||||
filterFn = func(_ *LocalRepository) bool {
|
||||
return true
|
||||
}
|
||||
} else {
|
||||
if hasSchemePattern.MatchString(query) || scpLikeURLPattern.MatchString(query) {
|
||||
if url, err := newURL(query); err == nil {
|
||||
if repo, err := LocalRepositoryFromURL(url); err == nil {
|
||||
query = repo.RelPath
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if exact {
|
||||
filterFn = func(repo *LocalRepository) bool {
|
||||
return repo.Matches(query)
|
||||
}
|
||||
} else {
|
||||
var host string
|
||||
paths := strings.Split(query, "/")
|
||||
if len(paths) > 1 && looksLikeAuthorityPattern.MatchString(paths[0]) {
|
||||
query = strings.Join(paths[1:], "/")
|
||||
host = paths[0]
|
||||
}
|
||||
filterFn = func(repo *LocalRepository) bool {
|
||||
return strings.Contains(repo.NonHostPath(), query) &&
|
||||
(host == "" || repo.PathParts[0] == host)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
repos := []*LocalRepository{}
|
||||
if err := walkLocalRepositories(func(repo *LocalRepository) {
|
||||
if !filterFn(repo) {
|
||||
return
|
||||
}
|
||||
repos = append(repos, repo)
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if printUniquePaths {
|
||||
subpathCount := map[string]int{} // Count duplicated subpaths (ex. foo/dotfiles and bar/dotfiles)
|
||||
reposCount := map[string]int{} // Check duplicated repositories among roots
|
||||
|
||||
// Primary first
|
||||
for _, repo := range repos {
|
||||
if reposCount[repo.RelPath] == 0 {
|
||||
for _, p := range repo.Subpaths() {
|
||||
subpathCount[p] = subpathCount[p] + 1
|
||||
}
|
||||
}
|
||||
|
||||
reposCount[repo.RelPath] = reposCount[repo.RelPath] + 1
|
||||
}
|
||||
|
||||
for _, repo := range repos {
|
||||
if reposCount[repo.RelPath] > 1 && repo.IsUnderPrimaryRoot() == false {
|
||||
continue
|
||||
}
|
||||
|
||||
for _, p := range repo.Subpaths() {
|
||||
if subpathCount[p] == 1 {
|
||||
fmt.Fprintln(w, p)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for _, repo := range repos {
|
||||
if printFullPaths {
|
||||
fmt.Fprintln(w, repo.FullPath)
|
||||
} else {
|
||||
fmt.Fprintln(w, repo.RelPath)
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
51
list_test.go
Normal file
51
list_test.go
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
func TestCommandList(t *testing.T) {
|
||||
_, _, err := capture(func() {
|
||||
app := cli.NewApp()
|
||||
flagSet := flagSet("list", commandList.Flags)
|
||||
c := cli.NewContext(app, flagSet, nil)
|
||||
|
||||
doList(c)
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("error should be nil, but: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCommandListUnique(t *testing.T) {
|
||||
_, _, err := capture(func() {
|
||||
app := cli.NewApp()
|
||||
flagSet := flagSet("list", commandList.Flags)
|
||||
flagSet.Parse([]string{"--unique"})
|
||||
c := cli.NewContext(app, flagSet, nil)
|
||||
|
||||
doList(c)
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("error should be nil, but: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCommandListUnknown(t *testing.T) {
|
||||
_, _, err := capture(func() {
|
||||
app := cli.NewApp()
|
||||
flagSet := flagSet("list", commandList.Flags)
|
||||
flagSet.Parse([]string{"--unknown-flag"})
|
||||
c := cli.NewContext(app, flagSet, nil)
|
||||
|
||||
doList(c)
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("error should be nil, but: %s", err)
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue