Skip to content

Commit

Permalink
Use go-gh config package (#5771)
Browse files Browse the repository at this point in the history
  • Loading branch information
samcoe committed Jun 23, 2022
1 parent 5227af0 commit cacff4a
Show file tree
Hide file tree
Showing 82 changed files with 1,377 additions and 3,332 deletions.
7 changes: 5 additions & 2 deletions api/http_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

type configGetter interface {
Get(string, string) (string, error)
AuthToken(string) (string, string)
}

type HTTPClientOptions struct {
Expand Down Expand Up @@ -52,7 +53,9 @@ func NewHTTPClient(opts HTTPClientOptions) (*http.Client, error) {
return nil, err
}

client.Transport = AddAuthTokenHeader(client.Transport, opts.Config)
if opts.Config != nil {
client.Transport = AddAuthTokenHeader(client.Transport, opts.Config)
}

return client, nil
}
Expand All @@ -75,7 +78,7 @@ func AddCacheTTLHeader(rt http.RoundTripper, ttl time.Duration) http.RoundTrippe
func AddAuthTokenHeader(rt http.RoundTripper, cfg configGetter) http.RoundTripper {
return &funcTripper{roundTrip: func(req *http.Request) (*http.Response, error) {
hostname := ghinstance.NormalizeHostname(getHost(req))
if token, err := cfg.Get(hostname, "oauth_token"); err == nil && token != "" {
if token, _ := cfg.AuthToken(hostname); token != "" {
req.Header.Set("Authorization", fmt.Sprintf("token %s", token))
}
return rt.RoundTrip(req)
Expand Down
4 changes: 4 additions & 0 deletions api/http_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,10 @@ func (c tinyConfig) Get(host, key string) (string, error) {
return c[fmt.Sprintf("%s:%s", host, key)], nil
}

func (c tinyConfig) AuthToken(host string) (string, string) {
return c[fmt.Sprintf("%s:%s", host, "oauth_token")], "oauth_token"
}

var requestAtRE = regexp.MustCompile(`(?m)^\* Request at .+`)
var dateRE = regexp.MustCompile(`(?m)^< Date: .+`)
var hostWithPortRE = regexp.MustCompile(`127\.0\.0\.1:\d+`)
Expand Down
27 changes: 12 additions & 15 deletions cmd/gh/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,8 @@ func mainRun() exitCode {
return exitError
}

// TODO: remove after FromFullName has been revisited
if host, err := cfg.DefaultHost(); err == nil {
ghrepo.SetDefaultHost(host)
}
host, _ := cfg.DefaultHost()
ghrepo.SetDefaultHost(host)

expandedArgs := []string{}
if len(os.Args) > 0 {
Expand Down Expand Up @@ -170,18 +168,17 @@ func mainRun() exitCode {
// provide completions for aliases and extensions
rootCmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
var results []string
if aliases, err := cfg.Aliases(); err == nil {
for aliasName, aliasValue := range aliases.All() {
if strings.HasPrefix(aliasName, toComplete) {
var s string
if strings.HasPrefix(aliasValue, "!") {
s = fmt.Sprintf("%s\tShell alias", aliasName)
} else {
aliasValue = text.Truncate(80, aliasValue)
s = fmt.Sprintf("%s\tAlias for %s", aliasName, aliasValue)
}
results = append(results, s)
aliases := cfg.Aliases()
for aliasName, aliasValue := range aliases.All() {
if strings.HasPrefix(aliasName, toComplete) {
var s string
if strings.HasPrefix(aliasValue, "!") {
s = fmt.Sprintf("%s\tShell alias", aliasName)
} else {
aliasValue = text.Truncate(80, aliasValue)
s = fmt.Sprintf("%s\tAlias for %s", aliasName, aliasValue)
}
results = append(results, s)
}
}
for _, ext := range cmdFactory.ExtensionManager.List() {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/charmbracelet/glamour v0.4.0
github.com/charmbracelet/lipgloss v0.5.0
github.com/cli/browser v1.1.0
github.com/cli/go-gh v0.0.4-0.20220614183308-ef2bca923638
github.com/cli/go-gh v0.0.4-0.20220623035622-91ca4ef447d4
github.com/cli/oauth v0.9.0
github.com/cli/safeexec v1.0.0
github.com/cli/shurcooL-graphql v0.0.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ github.com/cli/browser v1.1.0 h1:xOZBfkfY9L9vMBgqb1YwRirGu6QFaQ5dP/vXt5ENSOY=
github.com/cli/browser v1.1.0/go.mod h1:HKMQAt9t12kov91Mn7RfZxyJQQgWgyS/3SZswlZ5iTI=
github.com/cli/crypto v0.0.0-20210929142629-6be313f59b03 h1:3f4uHLfWx4/WlnMPXGai03eoWAI+oGHJwr+5OXfxCr8=
github.com/cli/crypto v0.0.0-20210929142629-6be313f59b03/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
github.com/cli/go-gh v0.0.4-0.20220614183308-ef2bca923638 h1:7MXhocX2RDlWrjKZ1pZsy8eMNGa3xkZzPrGC1IPBfx4=
github.com/cli/go-gh v0.0.4-0.20220614183308-ef2bca923638/go.mod h1:Y/QFb/VxnXQH0W4VlP+507HVxMzQ430x8kdjUuVcono=
github.com/cli/go-gh v0.0.4-0.20220623035622-91ca4ef447d4 h1:6WrekNBE2Y+Xl9OCl7vsg49SSN68hwaVryfEawQevaQ=
github.com/cli/go-gh v0.0.4-0.20220623035622-91ca4ef447d4/go.mod h1:Y/QFb/VxnXQH0W4VlP+507HVxMzQ430x8kdjUuVcono=
github.com/cli/oauth v0.9.0 h1:nxBC0Df4tUzMkqffAB+uZvisOwT3/N9FpkfdTDtafxc=
github.com/cli/oauth v0.9.0/go.mod h1:qd/FX8ZBD6n1sVNQO3aIdRxeu5LGw9WhKnYhIIoC2A4=
github.com/cli/safeexec v1.0.0 h1:0VngyaIyqACHdcMNWfo6+KdUYnqEr2Sg+bSP1pdF+dI=
Expand Down
15 changes: 4 additions & 11 deletions internal/authflow/flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@ var (

type iconfig interface {
Get(string, string) (string, error)
Set(string, string, string) error
Set(string, string, string)
Write() error
WriteHosts() error
}

func AuthFlowWithConfig(cfg iconfig, IO *iostreams.IOStreams, hostname, notice string, additionalScopes []string, isInteractive bool) (string, error) {
Expand All @@ -55,16 +54,10 @@ func AuthFlowWithConfig(cfg iconfig, IO *iostreams.IOStreams, hostname, notice s
return "", err
}

err = cfg.Set(hostname, "user", userLogin)
if err != nil {
return "", err
}
err = cfg.Set(hostname, "oauth_token", token)
if err != nil {
return "", err
}
cfg.Set(hostname, "user", userLogin)
cfg.Set(hostname, "oauth_token", token)

return token, cfg.WriteHosts()
return token, cfg.Write()
}

func authFlow(oauthHost string, IO *iostreams.IOStreams, notice string, additionalScopes []string, isInteractive bool, browserLauncher string) (string, string, error) {
Expand Down
60 changes: 0 additions & 60 deletions internal/config/alias_config.go

This file was deleted.

0 comments on commit cacff4a

Please sign in to comment.