Skip to content

Commit

Permalink
Merge pull request #87 from tcnksm/default-user-iss86
Browse files Browse the repository at this point in the history
[incompatible] retrieve owner from origin url first
  • Loading branch information
Songmu authored May 7, 2018
2 parents 177f723 + ed68394 commit dc78c94
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 15 deletions.
46 changes: 31 additions & 15 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"io"
"log"
"os"
"regexp"
"runtime"
"time"

Expand All @@ -26,7 +27,7 @@ const (
EnvGitHubAPI = "GITHUB_API"

// EnvDebug is an environment var to handle debug mode
EnvDebug = "GHR_DEBUG"
EnvDebug = "GHR_DEBUG"
)

// Exit codes are set to a value that represnet an exit code for a paticular error.
Expand Down Expand Up @@ -163,20 +164,25 @@ func (cli *CLI) Run(args []string) int {
// If it's not provided via command line flag, read it from .gitconfig
// (github user or git user).
if len(owner) == 0 {
var err error
owner, err = gitconfig.GithubUser()
if err != nil {
owner, err = gitconfig.Username()
origin, err := gitconfig.OriginURL()
if err == nil {
owner = retrieveOwnerName(origin)
}

if err != nil {
PrintRedf(cli.errStream,
"Failed to set up ghr: repository owner name not found\n")
fmt.Fprintf(cli.errStream,
"Please set it via `-u` option.\n\n"+
"You can set default owner name in `github.username` or `user.name`\n"+
"in `~/.gitconfig` file\n")
return ExitCodeOwnerNotFound
if len(owner) == 0 {
owner, err = gitconfig.GithubUser()
if err != nil {
owner, err = gitconfig.Username()
}

if err != nil {
PrintRedf(cli.errStream,
"Failed to set up ghr: repository owner name not found\n")
fmt.Fprintf(cli.errStream,
"Please set it via `-u` option.\n\n"+
"You can set default owner name in `github.username` or `user.name`\n"+
"in `~/.gitconfig` file\n")
return ExitCodeOwnerNotFound
}
}
}
Debugf("Owner: %s", owner)
Expand All @@ -193,7 +199,7 @@ func (cli *CLI) Run(args []string) int {
"ghr reads it from `.git/config` file. Change directory to \n"+
"repository root directory or setup git repository.\n"+
"Or set it via `-r` option.\n")
return ExitCodeOwnerNotFound
return ExitCodeRepoNotFound
}
}
Debugf("Repository: %s", repo)
Expand Down Expand Up @@ -299,6 +305,16 @@ func (cli *CLI) Run(args []string) int {
return ExitCodeOK
}

var ownerNameReg = regexp.MustCompile(`([-a-zA-Z0-9]+)/[^/]+$`)

func retrieveOwnerName(repoURL string) string {
matched := ownerNameReg.FindStringSubmatch(repoURL)
if len(matched) < 2 {
return ""
}
return matched[1]
}

// maskString is used to mask a string which should not be displayed
// directly, like the auth token
func maskString(s string) string {
Expand Down
20 changes: 20 additions & 0 deletions cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,23 @@ func TestRun_versionFlag(t *testing.T) {
t.Fatalf("%q output %q, want = %q", command, got, want)
}
}

func TestRetrieveOwnerName(t *testing.T) {
testCases := []struct {
in, expected string
}{
{"[email protected]:tcnksm/ghr.git", "tcnksm"},
{"https://github.com/motemen/ghq.git", "motemen"},
{"git://github.com/Songmu/ghg.git", "Songmu"},
{"hoge", ""},
}

for _, tc := range testCases {
t.Run(tc.in, func(t *testing.T) {
out := retrieveOwnerName(tc.in)
if out != tc.expected {
t.Errorf("out: %s, expected: %s", out, tc.expected)
}
})
}
}

0 comments on commit dc78c94

Please sign in to comment.