add context.go and move over getToken

This commit is contained in:
nate smith 2019-10-10 15:39:39 -05:00
parent 0e6a693649
commit 31b0163d53
2 changed files with 26 additions and 20 deletions

24
context/context.go Normal file
View file

@ -0,0 +1,24 @@
package context
import (
"io/ioutil"
"os/user"
"regexp"
)
// GetToken returns the authentication token as stored in the config file for the user running gh-cli
func GetToken() (string, error) {
usr, err := user.Current()
if err != nil {
return "", err
}
content, err := ioutil.ReadFile(usr.HomeDir + "/.config/hub")
if err != nil {
return "", err
}
r := regexp.MustCompile(`oauth_token: (\w+)`)
token := r.FindStringSubmatch(string(content))
return token[1], nil
}