diff --git a/api/http_client.go b/api/http_client.go index 6e92df0e9..bd70c1015 100644 --- a/api/http_client.go +++ b/api/http_client.go @@ -12,15 +12,14 @@ import ( ghAPI "github.com/cli/go-gh/pkg/api" ) -type configGetter interface { - Get(string, string) (string, error) +type tokenGetter interface { AuthToken(string) (string, string) } type HTTPClientOptions struct { AppVersion string CacheTTL time.Duration - Config configGetter + Config tokenGetter EnableCache bool Log io.Writer SkipAcceptHeaders bool @@ -75,7 +74,7 @@ func AddCacheTTLHeader(rt http.RoundTripper, ttl time.Duration) http.RoundTrippe } // AddAuthToken adds an authentication token header for the host specified by the request. -func AddAuthTokenHeader(rt http.RoundTripper, cfg configGetter) http.RoundTripper { +func AddAuthTokenHeader(rt http.RoundTripper, cfg tokenGetter) http.RoundTripper { return &funcTripper{roundTrip: func(req *http.Request) (*http.Response, error) { hostname := ghinstance.NormalizeHostname(getHost(req)) if token, _ := cfg.AuthToken(hostname); token != "" { diff --git a/api/http_client_test.go b/api/http_client_test.go index fa94f84cc..ebd360af9 100644 --- a/api/http_client_test.go +++ b/api/http_client_test.go @@ -16,7 +16,7 @@ import ( func TestNewHTTPClient(t *testing.T) { type args struct { - config configGetter + config tokenGetter appVersion string setAccept bool } @@ -207,10 +207,6 @@ func TestNewHTTPClient(t *testing.T) { type tinyConfig map[string]string -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" } diff --git a/cmd/gh/main.go b/cmd/gh/main.go index 444e1262f..5ad340c29 100644 --- a/cmd/gh/main.go +++ b/cmd/gh/main.go @@ -98,9 +98,6 @@ func mainRun() exitCode { return exitError } - host, _ := cfg.DefaultHost() - ghrepo.SetDefaultHost(host) - expandedArgs := []string{} if len(os.Args) > 0 { expandedArgs = os.Args[1:] diff --git a/internal/authflow/flow.go b/internal/authflow/flow.go index 8399a4cce..6c4976134 100644 --- a/internal/authflow/flow.go +++ b/internal/authflow/flow.go @@ -141,12 +141,8 @@ type cfg struct { authToken string } -func (c cfg) Get(_, _ string) (string, error) { - return "", nil -} - -func (c cfg) AuthToken(_ string) (string, string) { - return c.authToken, "" +func (c cfg) AuthToken(hostname string) (string, string) { + return c.authToken, "oauth_token" } func getViewer(hostname, token string) (string, error) { diff --git a/internal/ghrepo/repo.go b/internal/ghrepo/repo.go index 716e789a1..fbe93514d 100644 --- a/internal/ghrepo/repo.go +++ b/internal/ghrepo/repo.go @@ -6,6 +6,7 @@ import ( "strings" "github.com/cli/cli/v2/internal/ghinstance" + ghAuth "github.com/cli/go-gh/pkg/auth" "github.com/cli/go-gh/pkg/repository" ) @@ -35,19 +36,9 @@ func FullName(r Interface) string { return fmt.Sprintf("%s/%s", r.RepoOwner(), r.RepoName()) } -var defaultHostOverride string - func defaultHost() string { - if defaultHostOverride != "" { - return defaultHostOverride - } - return ghinstance.Default() -} - -// SetDefaultHost overrides the default GitHub hostname for FromFullName. -// TODO: remove after FromFullName approach is revisited -func SetDefaultHost(host string) { - defaultHostOverride = host + host, _ := ghAuth.DefaultHost() + return host } // FromFullName extracts the GitHub repository information from the following diff --git a/internal/ghrepo/repo_test.go b/internal/ghrepo/repo_test.go index 46fa37827..b013dd42c 100644 --- a/internal/ghrepo/repo_test.go +++ b/internal/ghrepo/repo_test.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "net/url" + "os" "testing" ) @@ -194,7 +195,9 @@ func TestFromFullName(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if tt.hostOverride != "" { - SetDefaultHost(tt.hostOverride) + old := os.Getenv("GH_HOST") + os.Setenv("GH_HOST", tt.hostOverride) + defer os.Setenv("GH_HOST", old) } r, err := FromFullName(tt.input) if tt.wantErr != nil {