Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[refactoring] Commonize doGet and doImport processing for refactoring #147

Merged
merged 2 commits into from
May 4, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 48 additions & 68 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/motemen/ghq/logger"
"github.com/urfave/cli"
"golang.org/x/sync/errgroup"
"golang.org/x/xerrors"
)

var commands = []cli.Command{
Expand Down Expand Up @@ -128,21 +129,12 @@ OPTIONS:
{{end}}`
}

func doGet(c *cli.Context) error {
var (
argURL = c.Args().Get(0)
doUpdate = c.Bool("update")
isShallow = c.Bool("shallow")
andLook = c.Bool("look")
vcsBackend = c.String("vcs")
isSilent = c.Bool("silent")
)

if argURL == "" {
cli.ShowCommandHelp(c, "get")
os.Exit(1)
}
type getter struct {
update, shallow, silent, ssh bool
vcs string
}

func (g *getter) get(argURL string) error {
// If argURL is a "./foo" or "../bar" form,
// find repository name trailing after github.com/USER/.
parts := strings.Split(argURL, string(filepath.Separator))
Expand Down Expand Up @@ -170,29 +162,49 @@ func doGet(c *cli.Context) error {
}
}

url, err := newURL(argURL)
u, err := newURL(argURL)
if err != nil {
return err
return xerrors.Errorf("Could not parse URL %q: %w", argURL, err)
}

isSSH := c.Bool("p")
if isSSH {
if g.ssh {
// Assume Git repository if `-p` is given.
if url, err = convertGitURLHTTPToSSH(url); err != nil {
return err
if u, err = convertGitURLHTTPToSSH(u); err != nil {
return xerrors.Errorf("Could not convet URL %q: %w", u, err)
}
}

remote, err := NewRemoteRepository(url)
remote, err := NewRemoteRepository(u)
if err != nil {
return err
}

if remote.IsValid() == false {
return fmt.Errorf("Not a valid repository: %s", url)
return fmt.Errorf("Not a valid repository: %s", u)
}

return getRemoteRepository(remote, g.update, g.shallow, g.vcs, g.silent)
}

func doGet(c *cli.Context) error {
var (
argURL = c.Args().Get(0)
andLook = c.Bool("look")
)
g := &getter{
update: c.Bool("update"),
shallow: c.Bool("shallow"),
ssh: c.Bool("p"),
vcs: c.String("vcs"),
silent: c.Bool("silent"),
}

if argURL == "" {
cli.ShowCommandHelp(c, "get")
os.Exit(1)
}

if err := getRemoteRepository(remote, doUpdate, isShallow, vcsBackend, isSilent); err != nil {
if err := g.get(argURL); err != nil {
return err
}
if andLook {
Expand Down Expand Up @@ -404,67 +416,35 @@ func doLook(c *cli.Context) error {
}

func doImport(c *cli.Context) error {
var (
doUpdate = c.Bool("update")
isSSH = c.Bool("p")
isShallow = c.Bool("shallow")
isSilent = c.Bool("silent")
vcsBackend = c.String("vcs")
parallel = c.Bool("parallel")
)
var 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"),
}
if parallel {
// force silent in parallel import
isSilent = true
}

processLine := func(line string) error {
url, err := newURL(line)
if err != nil {
return fmt.Errorf("Could not parse URL <%s>: %s", line, err)
}
if isSSH {
url, err = convertGitURLHTTPToSSH(url)
if err != nil {
return fmt.Errorf("Could not convert URL <%s>: %s", url, err)
}
}

remote, err := NewRemoteRepository(url)
if err != nil {
return err
}
if !remote.IsValid() {
return fmt.Errorf("Not a valid repository: %s", url)
}

if err := getRemoteRepository(remote, doUpdate, isShallow, vcsBackend, isSilent); err != nil {
return fmt.Errorf("failed to getRemoteRepository %q: %s", remote.URL(), err)
}
return nil
g.silent = true
}

var (
eg *errgroup.Group
sem chan (struct{})
)
if parallel {
eg = &errgroup.Group{}
sem = make(chan struct{}, 6)
}
eg := &errgroup.Group{}
sem := make(chan struct{}, 6)
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
line := scanner.Text()
if parallel {
eg.Go(func() error {
sem <- struct{}{}
defer func() { <-sem }()
if err := processLine(line); err != nil {
if err := g.get(line); err != nil {
logger.Log("error", err.Error())
}
return nil
})
} else {
if err := processLine(line); err != nil {
if err := g.get(line); err != nil {
logger.Log("error", err.Error())
}
}
Expand Down