cli/pkg/cmd/auth/shared/client.go
Mislav Marohnić c9407b2629 More descriptive error when aborting auth due to environment variables
Old message:

    read-only token in GH_TOKEN cannot be modified

This message was vague and some users did not understand that this
refers to the value that is read from environment variables.

New message:

    $ GH_TOKEN=123 ghd auth login -h github.com
    The value of the GH_TOKEN environment variable is being used for authentication.
    To have GitHub CLI store credentials instead, first clear the value from the environment.
2021-01-20 18:27:35 +01:00

34 lines
764 B
Go

package shared
import (
"fmt"
"net/http"
"github.com/cli/cli/api"
"github.com/cli/cli/internal/config"
)
var ClientFromCfg = func(hostname string, cfg config.Config) (*api.Client, error) {
var opts []api.ClientOption
token, err := cfg.Get(hostname, "oauth_token")
if err != nil {
return nil, err
}
if token == "" {
return nil, fmt.Errorf("no token found in config for %s", hostname)
}
opts = append(opts,
// no access to Version so the user agent is more generic here.
api.AddHeader("User-Agent", "GitHub CLI"),
api.AddHeaderFunc("Authorization", func(req *http.Request) (string, error) {
return fmt.Sprintf("token %s", token), nil
}),
)
httpClient := api.NewHTTPClient(opts...)
return api.NewClientFromHTTP(httpClient), nil
}