forked from x-motemen/ghq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd_get.go
173 lines (159 loc) · 3.43 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
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"),
}
if parallel {
// force silent in parallel import
g.silent = true
}
var (
firstArg string
scr scanner
)
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
}
if parallel {
sem <- struct{}{}
eg.Go(func() error {
defer func() { <-sem }()
if err := g.get(target); err != nil {
logger.Logf("error", "failed to get %q: %s", target, err)
}
return nil
})
} else {
if 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 && firstArg != "" {
return look(firstArg)
}
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) 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)
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:
repo := reposFound[0]
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)
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())
}
}