-
-
Notifications
You must be signed in to change notification settings - Fork 184
/
cmd_get.go
187 lines (171 loc) · 3.87 KB
/
cmd_get.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package main
import (
"bufio"
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"sync"
"github.com/mattn/go-isatty"
"github.com/urfave/cli/v2"
"github.com/x-motemen/ghq/cmdutil"
"github.com/x-motemen/ghq/logger"
"golang.org/x/sync/errgroup"
)
func doGet(c *cli.Context) error {
var (
args = c.Args().Slice()
andLook = c.Bool("look")
parallel = c.Bool("parallel")
)
g := &getter{
update: c.Bool("update"),
shallow: c.Bool("shallow"),
ssh: c.Bool("p"),
vcs: c.String("vcs"),
silent: c.Bool("silent"),
branch: c.String("branch"),
recursive: !c.Bool("no-recursive"),
bare: c.Bool("bare"),
}
if parallel {
// force silent in parallel import
g.silent = true
}
var (
firstArg string // Look at the first repo only, if there are more than one
argCnt int
getInfo getInfo // For fetching and looking a single repo
scr scanner
err error
)
if len(args) > 0 {
scr = &sliceScanner{slice: args}
} else {
fd := os.Stdin.Fd()
if isatty.IsTerminal(fd) || isatty.IsCygwinTerminal(fd) {
return fmt.Errorf("no target args specified. see `ghq get -h` for more details")
}
scr = bufio.NewScanner(os.Stdin)
}
eg := &errgroup.Group{}
sem := make(chan struct{}, 6)
for scr.Scan() {
target := scr.Text()
if firstArg == "" {
firstArg = target
}
argCnt += 1
if parallel {
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)
}
return nil
})
} else {
if getInfo, err = g.get(target); err != nil {
return fmt.Errorf("failed to get %q: %w", target, err)
}
}
}
if err = scr.Err(); err != nil {
return fmt.Errorf("error occurred while reading input: %w", err)
}
if err = eg.Wait(); err != nil {
return err
}
if andLook {
if argCnt > 1 && firstArg != "" {
return look(firstArg, g.bare)
}
if argCnt == 1 && getInfo.localRepository != nil {
return lookByLocalRepository(getInfo.localRepository)
}
}
return nil
}
type sliceScanner struct {
slice []string
index int
}
func (s *sliceScanner) Scan() bool {
s.index++
return s.index <= len(s.slice)
}
func (s *sliceScanner) Text() string {
return s.slice[s.index-1]
}
func (s *sliceScanner) Err() error {
return nil
}
type scanner interface {
Scan() bool
Text() string
Err() error
}
func detectShell() string {
shell := os.Getenv("SHELL")
if shell != "" {
return shell
}
if runtime.GOOS == "windows" {
return os.Getenv("COMSPEC")
}
return "/bin/sh"
}
func look(name string, bare bool) error {
var (
reposFound []*LocalRepository
mu sync.Mutex
)
if err := walkAllLocalRepositories(func(repo *LocalRepository) {
if repo.Matches(name) {
mu.Lock()
reposFound = append(reposFound, repo)
mu.Unlock()
}
}); err != nil {
return err
}
if len(reposFound) == 0 {
if url, err := newURL(name, false, false); err == nil {
repo, err := LocalRepositoryFromURL(url, bare)
if err != nil {
return err
}
_, err = os.Stat(repo.FullPath)
// if the directory exists
if err == nil {
reposFound = append(reposFound, repo)
}
}
}
switch len(reposFound) {
case 0:
return fmt.Errorf("no repository found")
case 1:
return lookByLocalRepository(reposFound[0])
default:
b := &strings.Builder{}
b.WriteString("More than one repositories are found; Try more precise name\n")
for _, repo := range reposFound {
b.WriteString(fmt.Sprintf(" - %s\n", strings.Join(repo.PathParts, "/")))
}
return errors.New(b.String())
}
}
func lookByLocalRepository(repo *LocalRepository) error {
cmd := exec.Command(detectShell())
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Dir = repo.FullPath
cmd.Env = append(os.Environ(), "GHQ_LOOK="+filepath.ToSlash(repo.RelPath))
return cmdutil.RunCommand(cmd, true)
}