-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- extract common code for listing repos - extend and unify ways to infer/set the access token - unify common flags
- Loading branch information
1 parent
51e8c1a
commit 9e64774
Showing
13 changed files
with
414 additions
and
387 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package auth | ||
|
||
import ( | ||
"os" | ||
|
||
"gopkg.in/yaml.v2" | ||
) | ||
|
||
// GetToken tries to infer the access token | ||
// from environment variables and config files. | ||
func GetToken() string { | ||
var token string | ||
|
||
// gh-tools specific env variable. | ||
if token = os.Getenv("GHTOOLS_TOKEN"); token != "" { | ||
return token | ||
} | ||
// Generic env variable. | ||
if token = os.Getenv("GITHUB_TOKEN"); token != "" { | ||
return token | ||
} | ||
// Read the token from gh-tools auth file ~/.config/gh-tools/auth.yml | ||
if token = fromAuthFile(); token != "" { | ||
return token | ||
} | ||
// Try to read the token from gh cli's config file ~/.config/gh/hosts.yml | ||
if token = fromGhCliConfig(); token != "" { | ||
return token | ||
} | ||
|
||
return "" | ||
} | ||
|
||
func fromAuthFile() string { | ||
path := "/.config/gh-tools/auth.yml" | ||
|
||
home, err := os.UserHomeDir() | ||
if err != nil { | ||
return "" | ||
} | ||
path = home + path | ||
|
||
file, err := os.Open(path) | ||
if err != nil { | ||
return "" | ||
} | ||
|
||
auth := struct { | ||
OauthToken string `yaml:"oauth_token"` | ||
}{} | ||
err = yaml.NewDecoder(file).Decode(auth) | ||
if err != nil { | ||
return "" | ||
} | ||
|
||
return auth.OauthToken | ||
} | ||
|
||
func fromGhCliConfig() string { | ||
path := "/.config/gh/hosts.yml" | ||
|
||
home, err := os.UserHomeDir() | ||
if err != nil { | ||
return "" | ||
} | ||
path = home + path | ||
|
||
file, err := os.Open(path) | ||
if err != nil { | ||
return "" | ||
} | ||
|
||
hosts := map[string]struct { | ||
OauthToken string `yaml:"oauth_token"` | ||
User string `yaml:"user"` | ||
}{} | ||
err = yaml.NewDecoder(file).Decode(hosts) | ||
if err != nil { | ||
return "" | ||
} | ||
|
||
auth := hosts["github.com"] | ||
|
||
return auth.OauthToken | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.