Skip to content

Commit

Permalink
Merge pull request #143 from motemen/lint
Browse files Browse the repository at this point in the history
reduce linter warnings
  • Loading branch information
Songmu authored May 1, 2019
2 parents 8fb1e63 + c9eeabd commit 8e5d917
Show file tree
Hide file tree
Showing 11 changed files with 38 additions and 28 deletions.
9 changes: 9 additions & 0 deletions cmdutil/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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:], " "))
Expand All @@ -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)
}
18 changes: 9 additions & 9 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"golang.org/x/sync/errgroup"
)

var Commands = []cli.Command{
var commands = []cli.Command{
commandGet,
commandList,
commandLook,
Expand Down Expand Up @@ -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}}"
Expand Down Expand Up @@ -170,15 +170,15 @@ func doGet(c *cli.Context) error {
}
}

url, err := NewURL(argURL)
url, err := newURL(argURL)
if err != nil {
return err
}

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
}
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion git.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
2 changes: 1 addition & 1 deletion git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand Down
2 changes: 1 addition & 1 deletion go_import.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion logger/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -30,6 +30,7 @@ func init() {
logger.SetOutput(os.Stderr)
}

// Log output
func Log(prefix, message string) {
logger.Log(prefix, message)
}
6 changes: 3 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/urfave/cli"
)

const Version = "0.10.2"
const version = "0.10.2"

var revision = "HEAD"

Expand All @@ -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 = "[email protected]"
app.Commands = Commands
app.Commands = commands
return app
}
2 changes: 1 addition & 1 deletion remote_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions url.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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()
Expand Down
6 changes: 3 additions & 3 deletions url_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down
6 changes: 3 additions & 3 deletions vcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit 8e5d917

Please sign in to comment.