diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2018-08-08 20:45:12 +1000 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2018-08-08 20:45:12 +1000 |
commit | 4281cc2884b96d475c88fead179e25d890e1edd0 (patch) | |
tree | 2dd1d3967d6ab7fd327448995f5a742b09a00d16 /vendor/github.com | |
parent | 8013f18177bcad2053c2802be4724a900c751a85 (diff) |
add git config check and editing ability
Diffstat (limited to 'vendor/github.com')
-rw-r--r-- | vendor/github.com/tcnksm/go-gitconfig/LICENSE | 22 | ||||
-rw-r--r-- | vendor/github.com/tcnksm/go-gitconfig/gitconfig.go | 113 |
2 files changed, 135 insertions, 0 deletions
diff --git a/vendor/github.com/tcnksm/go-gitconfig/LICENSE b/vendor/github.com/tcnksm/go-gitconfig/LICENSE new file mode 100644 index 000000000..3457f2566 --- /dev/null +++ b/vendor/github.com/tcnksm/go-gitconfig/LICENSE @@ -0,0 +1,22 @@ +Copyright (c) 2014 tcnksm + +MIT License + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\ No newline at end of file diff --git a/vendor/github.com/tcnksm/go-gitconfig/gitconfig.go b/vendor/github.com/tcnksm/go-gitconfig/gitconfig.go new file mode 100644 index 000000000..46c6a10f6 --- /dev/null +++ b/vendor/github.com/tcnksm/go-gitconfig/gitconfig.go @@ -0,0 +1,113 @@ +// Package gitconfig enables you to use `~/.gitconfig` values in Golang. +// +// For a full guide visit http://github.com/tcnksm/go-gitconfig +// +// package main +// +// import ( +// "github.com/tcnksm/go-gitconfig" +// "fmt" +// ) +// +// func main() { +// user, err := gitconfig.Global("user.name") +// if err == nil { +// fmt.Println(user) +// } +// } +// +package gitconfig + +import ( + "bytes" + "fmt" + "io/ioutil" + "os/exec" + "regexp" + "strings" + "syscall" +) + +// Entire extracts configuration value from `$HOME/.gitconfig` file , +// `$GIT_CONFIG`, /etc/gitconfig or include.path files. +func Entire(key string) (string, error) { + return execGitConfig(key) +} + +// Global extracts configuration value from `$HOME/.gitconfig` file or `$GIT_CONFIG`. +func Global(key string) (string, error) { + return execGitConfig("--global", key) +} + +// Local extracts configuration value from current project repository. +func Local(key string) (string, error) { + return execGitConfig("--local", key) +} + +// GithubUser extracts github.user name from `Entire gitconfig` +// This is same as Entire("github.user") +func GithubUser() (string, error) { + return Entire("github.user") +} + +// Username extracts git user name from `Entire gitconfig`. +// This is same as Entire("user.name") +func Username() (string, error) { + return Entire("user.name") +} + +// Email extracts git user email from `$HOME/.gitconfig` file or `$GIT_CONFIG`. +// This is same as Global("user.email") +func Email() (string, error) { + return Entire("user.email") +} + +// OriginURL extract remote origin url from current project repository. +// This is same as Local("remote.origin.url") +func OriginURL() (string, error) { + return Local("remote.origin.url") +} + +// Repository extract repository name of current project repository. +func Repository() (string, error) { + url, err := OriginURL() + if err != nil { + return "", err + } + + repo := retrieveRepoName(url) + return repo, nil +} + +// Github extracts github token from `Entire gitconfig`. +// This is same as Entire("github.token") +func GithubToken() (string, error) { + return Entire("github.token") +} + +func execGitConfig(args ...string) (string, error) { + gitArgs := append([]string{"config", "--get", "--null"}, args...) + var stdout bytes.Buffer + cmd := exec.Command("git", gitArgs...) + cmd.Stdout = &stdout + cmd.Stderr = ioutil.Discard + + err := cmd.Run() + if exitError, ok := err.(*exec.ExitError); ok { + if waitStatus, ok := exitError.Sys().(syscall.WaitStatus); ok { + if waitStatus.ExitStatus() == 1 { + return "", fmt.Errorf("the key `%s` is not found", args[len(args)-1]) + } + } + return "", err + } + + return strings.TrimRight(stdout.String(), "\000"), nil +} + +var RepoNameRegexp = regexp.MustCompile(`.+/([^/]+)(\.git)?$`) + +func retrieveRepoName(url string) string { + matched := RepoNameRegexp.FindStringSubmatch(url) + return strings.TrimSuffix(matched[1], ".git") +} |