From 7ea9614ffbf1379c5443871a02171398f2282f92 Mon Sep 17 00:00:00 2001 From: tcnksm Date: Tue, 29 Jul 2014 18:33:40 +0900 Subject: [PATCH] Refactoring git.go --- git.go | 30 +++++++++++++++++++++++++++--- git_test.go | 8 ++++++-- main.go | 19 ++++++------------- 3 files changed, 39 insertions(+), 18 deletions(-) diff --git a/git.go b/git.go index a21be50..0752cc4 100644 --- a/git.go +++ b/git.go @@ -2,6 +2,7 @@ package main import ( "bytes" + "fmt" "io/ioutil" "os/exec" "regexp" @@ -10,16 +11,39 @@ import ( var RepoNameRegexp = regexp.MustCompile(`.+/([^/]+)\.git$`) -func GitRepoName(url string) string { +func GetRepoName() (string, error) { + url, err := gitRemote() + if err != nil || url == "" { + return "", fmt.Errorf("Cound not retrieve remote repository url\n") + } + + repo := retrieveRepoName(url) + if repo == "" { + return "", fmt.Errorf("Cound not retrieve repository name\n") + } + return repo, nil +} + +func GetOwnerName() (string, error) { + owner, err := gitOwner() + if err != nil || owner == "" { + return "", fmt.Errorf("Cound not retrieve git user name\n") + } + return owner, err +} + +func retrieveRepoName(url string) string { matched := RepoNameRegexp.FindStringSubmatch(url) return matched[1] } -func GitRemote() (string, error) { +// git config --local remote.origin.url +func gitRemote() (string, error) { return gitConfig("--local", "remote.origin.url") } -func GitOwner() (string, error) { +// git config --global user.name +func gitOwner() (string, error) { return gitConfig("--global", "user.name") } diff --git a/git_test.go b/git_test.go index 1b20f24..b07a9d8 100644 --- a/git_test.go +++ b/git_test.go @@ -29,12 +29,16 @@ func TestGitConfig(t *testing.T) { url, err := gitConfig("remote.origin.url") Expect(err).NotTo(HaveOccurred()) Expect(url).To(Equal("https://github.com/tcnksm/test.git")) + + blank, err := gitConfig("nothing.nothing") + Expect(err).To(HaveOccurred()) + Expect(blank).To(Equal("")) } -func TestGitRepoName(t *testing.T) { +func TestRetrieveRepoName(t *testing.T) { RegisterTestingT(t) - repo := GitRepoName("https://github.com/tcnksm/ghr.git") + repo := retrieveRepoName("https://github.com/tcnksm/ghr.git") Expect(repo).To(Equal("ghr")) } diff --git a/main.go b/main.go index 6e39b70..102cdf6 100644 --- a/main.go +++ b/main.go @@ -58,25 +58,18 @@ func ghrMain() int { path := os.Args[2] debug(path) - // git config --global user.name - // tcnksm - owner, err := GitOwner() - if err != nil || owner == "" { - fmt.Fprintf(os.Stderr, "Please set `git config --global user.name`\n") + owner, err := GetOwnerName() + if err != nil { + fmt.Fprintf(os.Stderr, err.Error()) return 1 } debug(owner) - // git config --local remote.origin.url - // https://github.com/tcnksm/ghr.git - remoteURL, err := GitRemote() - if err != nil || remoteURL == "" { - fmt.Fprintf(os.Stderr, "Please set remote host of your project\n") + repo, err := GetRepoName() + if err != nil { + fmt.Fprintf(os.Stderr, err.Error()) return 1 } - debug(remoteURL) - - repo := GitRepoName(remoteURL) debug(repo) info := NewInfo()