X Tutup
Skip to content

Commit 87a1490

Browse files
committed
Improvements to update notifier authentication
- Check for updates even if `~/.config/gh` does not exist. In this case, the API call is unauthenticated. - Avoid having the update notifier ever triggering the OAuth flow.
1 parent aaad263 commit 87a1490

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed

command/root.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,19 @@ var initContext = func() context.Context {
6666
return ctx
6767
}
6868

69+
// BasicClient returns an API client that borrows from but does not depend on
70+
// user configuration
6971
func BasicClient() (*api.Client, error) {
70-
return apiClientForContext(initContext())
72+
opts := []api.ClientOption{
73+
api.AddHeader("User-Agent", fmt.Sprintf("GitHub CLI %s", Version)),
74+
}
75+
if c, err := context.ParseDefaultConfig(); err == nil {
76+
opts = append(opts, api.AddHeader("Authorization", fmt.Sprintf("token %s", c.Token)))
77+
}
78+
if verbose := os.Getenv("DEBUG"); verbose != "" {
79+
opts = append(opts, api.VerboseLog(os.Stderr))
80+
}
81+
return api.NewClient(opts...), nil
7182
}
7283

7384
func contextForCommand(cmd *cobra.Command) context.Context {

context/config_file.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"io/ioutil"
88
"os"
99

10+
"github.com/mitchellh/go-homedir"
1011
"gopkg.in/yaml.v3"
1112
)
1213

@@ -15,18 +16,30 @@ type configEntry struct {
1516
Token string `yaml:"oauth_token"`
1617
}
1718

19+
func parseOrSetupConfigFile(fn string) (*configEntry, error) {
20+
entry, err := parseConfigFile(fn)
21+
if err != nil && errors.Is(err, os.ErrNotExist) {
22+
return setupConfigFile(fn)
23+
}
24+
return entry, err
25+
}
26+
1827
func parseConfigFile(fn string) (*configEntry, error) {
1928
f, err := os.Open(fn)
2029
if err != nil {
21-
if errors.Is(err, os.ErrNotExist) {
22-
return setupConfigFile(fn)
23-
}
2430
return nil, err
2531
}
2632
defer f.Close()
2733
return parseConfig(f)
2834
}
2935

36+
// ParseDefaultConfig reads the configuration from ~/.config/gh
37+
func ParseDefaultConfig() (*configEntry, error) {
38+
// FIXME: this duplicates fsContext.configFile
39+
fn, _ := homedir.Expand("~/.config/gh")
40+
return parseConfigFile(fn)
41+
}
42+
3043
func parseConfig(r io.Reader) (*configEntry, error) {
3144
data, err := ioutil.ReadAll(r)
3245
if err != nil {

context/context.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func (c *fsContext) configFile() string {
4646

4747
func (c *fsContext) getConfig() (*configEntry, error) {
4848
if c.config == nil {
49-
entry, err := parseConfigFile(c.configFile())
49+
entry, err := parseOrSetupConfigFile(c.configFile())
5050
if err != nil {
5151
return nil, err
5252
}

0 commit comments

Comments
 (0)
X Tutup