diff --git a/cmdutil/run.go b/cmdutil/run.go index 96539ce4..73935131 100644 --- a/cmdutil/run.go +++ b/cmdutil/run.go @@ -10,6 +10,7 @@ import ( "github.com/motemen/ghq/logger" ) +// Run the command func Run(command string, args ...string) error { cmd := exec.Command(command, args...) cmd.Stdout = os.Stdout @@ -18,6 +19,7 @@ func Run(command string, args ...string) error { return RunCommand(cmd, false) } +// RunSilently runs the command silently func RunSilently(command string, args ...string) error { cmd := exec.Command(command, args...) cmd.Stdout = ioutil.Discard @@ -26,6 +28,7 @@ func RunSilently(command string, args ...string) error { return RunCommand(cmd, true) } +// RunInDir runs the command in the specified directory func RunInDir(dir, command string, args ...string) error { cmd := exec.Command(command, args...) cmd.Stdout = os.Stdout @@ -35,6 +38,7 @@ func RunInDir(dir, command string, args ...string) error { return RunCommand(cmd, false) } +// RunInDirSilently run the command in the specified directory silently func RunInDirSilently(dir, command string, args ...string) error { cmd := exec.Command(command, args...) cmd.Stdout = ioutil.Discard @@ -44,12 +48,15 @@ func RunInDirSilently(dir, command string, args ...string) error { return RunCommand(cmd, true) } +// RunFunc for the type command execution type RunFunc func(*exec.Cmd) error +// CommandRunner is for running the command var CommandRunner = func(cmd *exec.Cmd) error { return cmd.Run() } +// RunCommand run the command func RunCommand(cmd *exec.Cmd, silent bool) error { if !silent { logger.Log(cmd.Args[0], strings.Join(cmd.Args[1:], " ")) @@ -65,11 +72,13 @@ func RunCommand(cmd *exec.Cmd, silent bool) error { return nil } +// RunError is the error type for cmdutil type RunError struct { Command *exec.Cmd ExecError error } +// Error to implement error interface func (e *RunError) Error() string { return fmt.Sprintf("%s: %s", e.Command.Path, e.ExecError) } diff --git a/commands.go b/commands.go index dc1041de..d8ab69ee 100644 --- a/commands.go +++ b/commands.go @@ -14,7 +14,7 @@ import ( "golang.org/x/sync/errgroup" ) -var Commands = []cli.Command{ +var commands = []cli.Command{ commandGet, commandList, commandLook, @@ -104,7 +104,7 @@ var commandDocs = map[string]commandDoc{ // Makes template conditionals to generate per-command documents. func mkCommandsTemplate(genTemplate func(commandDoc) string) string { template := "{{if false}}" - for _, command := range append(Commands) { + for _, command := range append(commands) { template = template + fmt.Sprintf("{{else if (eq .Name %q)}}%s", command.Name, genTemplate(commandDocs[command.Name])) } return template + "{{end}}" @@ -170,7 +170,7 @@ func doGet(c *cli.Context) error { } } - url, err := NewURL(argURL) + url, err := newURL(argURL) if err != nil { return err } @@ -178,7 +178,7 @@ func doGet(c *cli.Context) error { isSSH := c.Bool("p") if isSSH { // Assume Git repository if `-p` is given. - if url, err = ConvertGitURLHTTPToSSH(url); err != nil { + if url, err = convertGitURLHTTPToSSH(url); err != nil { return err } } @@ -264,8 +264,8 @@ func doList(c *cli.Context) error { return true } } else { - if hasSchemePattern.MatchString(query) || scpLikeUrlPattern.MatchString(query) { - if url, err := NewURL(query); err == nil { + if hasSchemePattern.MatchString(query) || scpLikeURLPattern.MatchString(query) { + if url, err := newURL(query); err == nil { if repo, err := LocalRepositoryFromURL(url); err == nil { query = repo.RelPath } @@ -357,7 +357,7 @@ func doLook(c *cli.Context) error { } if len(reposFound) == 0 { - if url, err := NewURL(name); err == nil { + if url, err := newURL(name); err == nil { repo, err := LocalRepositoryFromURL(url) if err != nil { return err @@ -415,12 +415,12 @@ func doImport(c *cli.Context) error { } processLine := func(line string) error { - url, err := NewURL(line) + url, err := newURL(line) if err != nil { return fmt.Errorf("Could not parse URL <%s>: %s", line, err) } if isSSH { - url, err = ConvertGitURLHTTPToSSH(url) + url, err = convertGitURLHTTPToSSH(url) if err != nil { return fmt.Errorf("Could not convert URL <%s>: %s", url, err) } diff --git a/git.go b/git.go index 20d1a1b7..57ae58e0 100644 --- a/git.go +++ b/git.go @@ -59,7 +59,7 @@ var ( featureConfigURLMatchVersion = semver.MustParse("1.8.5") ) -func GitHasFeatureConfigURLMatch() error { +func gitHasFeatureConfigURLMatch() error { cmd := exec.Command("git", "--version") buf, err := cmd.Output() diff --git a/git_test.go b/git_test.go index 901c50e0..b0d8af02 100644 --- a/git_test.go +++ b/git_test.go @@ -14,7 +14,7 @@ func TestGitConfigAll(t *testing.T) { } func TestGitConfigURL(t *testing.T) { - if GitHasFeatureConfigURLMatch() != nil { + if gitHasFeatureConfigURLMatch() != nil { t.Skip("Git does not have config --get-urlmatch feature") } diff --git a/go_import.go b/go_import.go index 0a5c02f9..d4f1594d 100644 --- a/go_import.go +++ b/go_import.go @@ -23,7 +23,7 @@ func detectGoImport(u *url.URL) (string, *url.URL, error) { }, } req, _ := http.NewRequest(http.MethodGet, goGetU.String(), nil) - req.Header.Add("User-Agent", fmt.Sprintf("ghq/%s (+https://github.com/motemen/ghq)", Version)) + req.Header.Add("User-Agent", fmt.Sprintf("ghq/%s (+https://github.com/motemen/ghq)", version)) resp, err := cli.Do(req) if err != nil { return "", nil, err diff --git a/logger/log.go b/logger/log.go index 1863da3c..71599c64 100644 --- a/logger/log.go +++ b/logger/log.go @@ -12,7 +12,7 @@ var logger = colorine.NewLogger( "hg": colorine.Verbose, "svn": colorine.Verbose, "darcs": colorine.Verbose, - "bzr" : colorine.Verbose, + "bzr": colorine.Verbose, "skip": colorine.Verbose, "cd": colorine.Verbose, "resolved": colorine.Verbose, @@ -30,6 +30,7 @@ func init() { logger.SetOutput(os.Stderr) } +// Log output func Log(prefix, message string) { logger.Log(prefix, message) } diff --git a/main.go b/main.go index 4bd7eb30..2bc4cd28 100644 --- a/main.go +++ b/main.go @@ -8,7 +8,7 @@ import ( "github.com/urfave/cli" ) -const Version = "0.10.2" +const version = "0.10.2" var revision = "HEAD" @@ -27,9 +27,9 @@ func newApp() *cli.App { app := cli.NewApp() app.Name = "ghq" app.Usage = "Manage GitHub repository clones" - app.Version = fmt.Sprintf("%s (rev:%s)", Version, revision) + app.Version = fmt.Sprintf("%s (rev:%s)", version, revision) app.Author = "motemen" app.Email = "motemen@gmail.com" - app.Commands = Commands + app.Commands = commands return app } diff --git a/remote_repository.go b/remote_repository.go index ff5c666f..db7fe92b 100644 --- a/remote_repository.go +++ b/remote_repository.go @@ -135,7 +135,7 @@ func (repo *OtherRepository) IsValid() bool { } func (repo *OtherRepository) VCS() (*VCSBackend, *url.URL) { - if err := GitHasFeatureConfigURLMatch(); err != nil { + if err := gitHasFeatureConfigURLMatch(); err != nil { logger.Log("warning", err.Error()) } else { // Respect 'ghq.url.https://ghe.example.com/.vcs' config variable diff --git a/url.go b/url.go index 479998db..6e2dedb0 100644 --- a/url.go +++ b/url.go @@ -14,14 +14,14 @@ import ( // (golang hasn't supported Perl-like negative look-behind match) var ( hasSchemePattern = regexp.MustCompile("^[^:]+://") - scpLikeUrlPattern = regexp.MustCompile("^([^@]+@)?([^:]+):(/?.+)$") + scpLikeURLPattern = regexp.MustCompile("^([^@]+@)?([^:]+):(/?.+)$") looksLikeAuthorityPattern = regexp.MustCompile(`[A-Za-z0-9]\.[A-Za-z]+(?::\d{1,5})?$`) ) -func NewURL(ref string) (*url.URL, error) { +func newURL(ref string) (*url.URL, error) { if !hasSchemePattern.MatchString(ref) { - if scpLikeUrlPattern.MatchString(ref) { - matched := scpLikeUrlPattern.FindStringSubmatch(ref) + if scpLikeURLPattern.MatchString(ref) { + matched := scpLikeURLPattern.FindStringSubmatch(ref) user := matched[1] host := matched[2] path := matched[3] @@ -58,7 +58,7 @@ func NewURL(ref string) (*url.URL, error) { return url, nil } -func ConvertGitURLHTTPToSSH(url *url.URL) (*url.URL, error) { +func convertGitURLHTTPToSSH(url *url.URL) (*url.URL, error) { user := "git" if url.User != nil { user = url.User.Username() diff --git a/url_test.go b/url_test.go index c903d9a1..0d1f0f57 100644 --- a/url_test.go +++ b/url_test.go @@ -62,7 +62,7 @@ func TestNewURL(t *testing.T) { if tc.setup != nil { defer tc.setup()() } - repo, err := NewURL(tc.url) + repo, err := newURL(tc.url) if err != nil { t.Errorf("error should be nil but: %s", err) } @@ -92,11 +92,11 @@ func TestConvertGitURLHTTPToSSH(t *testing.T) { for _, tc := range testCases { t.Run(tc.url, func(t *testing.T) { - httpsURL, err := NewURL(tc.url) + httpsURL, err := newURL(tc.url) if err != nil { t.Errorf("error should be nil but: %s", err) } - sshURL, err := ConvertGitURLHTTPToSSH(httpsURL) + sshURL, err := convertGitURLHTTPToSSH(httpsURL) if err != nil { t.Errorf("error should be nil but: %s", err) } diff --git a/vcs_test.go b/vcs_test.go index c59d5af5..a2ff614e 100644 --- a/vcs_test.go +++ b/vcs_test.go @@ -20,13 +20,13 @@ func TestVCSBackend(t *testing.T) { } defer os.RemoveAll(tempDir) localDir := filepath.Join(tempDir, "repo") - commands := []*exec.Cmd{} - lastCommand := func() *exec.Cmd { return commands[len(commands)-1] } + _commands := []*exec.Cmd{} + lastCommand := func() *exec.Cmd { return _commands[len(_commands)-1] } defer func(orig func(cmd *exec.Cmd) error) { cmdutil.CommandRunner = orig }(cmdutil.CommandRunner) cmdutil.CommandRunner = func(cmd *exec.Cmd) error { - commands = append(commands, cmd) + _commands = append(_commands, cmd) return nil }