Skip to content

Commit

Permalink
Use go-gh config package
Browse files Browse the repository at this point in the history
  • Loading branch information
samcoe committed Jun 8, 2022
1 parent 4b488a7 commit 1e0d995
Show file tree
Hide file tree
Showing 77 changed files with 1,342 additions and 3,327 deletions.
37 changes: 16 additions & 21 deletions cmd/gh/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,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 @@ -171,18 +169,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 Expand Up @@ -361,12 +358,10 @@ func basicClient(currentVersion string) (*api.Client, error) {
opts = append(opts, api.VerboseLog(colorable.NewColorable(os.Stderr), logTraffic, colorize))
}
opts = append(opts, api.AddHeader("User-Agent", fmt.Sprintf("GitHub CLI %s", currentVersion)))

token, _ := config.AuthTokenFromEnv(ghinstance.Default())
if token == "" {
if c, err := config.ParseDefaultConfig(); err == nil {
token, _ = c.Get(ghinstance.Default(), "oauth_token")
}
cfg, err := config.NewConfig()
var token string
if err == nil {
token, _ = cfg.AuthToken(ghinstance.Default())
}
if token != "" {
opts = append(opts, api.AddHeader("Authorization", fmt.Sprintf("token %s", token)))
Expand Down
15 changes: 4 additions & 11 deletions internal/authflow/flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,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 @@ -49,16 +48,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.

223 changes: 223 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
package config

import (
"os"
"path/filepath"

ghAuth "github.com/cli/go-gh/pkg/auth"
ghConfig "github.com/cli/go-gh/pkg/config"
)

const (
hosts = "hosts"
aliases = "aliases"
)

// This interface describes interacting with some persistent configuration for gh.
//go:generate moq -rm -out config_mock.go . Config
type Config interface {
AuthToken(string) (string, string)
Get(string, string) (string, error)
GetOrDefault(string, string) (string, error)
Set(string, string, string)
UnsetHost(string)
Hosts() []string
DefaultHost() (string, string)
Aliases() *AliasConfig
Write() error
}

func NewConfig() (Config, error) {
c, err := ghConfig.Read()
if err != nil {
return nil, err
}
return &cfg{c}, nil
}

// Implements Config interface
type cfg struct {
cfg *ghConfig.Config
}

func (c *cfg) AuthToken(hostname string) (string, string) {
return ghAuth.TokenForHost(hostname)
}

func (c *cfg) Get(hostname, key string) (string, error) {
if hostname != "" {
val, err := c.cfg.Get([]string{hosts, hostname, key})
if err == nil {
return val, err
}
}

return c.cfg.Get([]string{key})
}

func (c *cfg) GetOrDefault(hostname, key string) (string, error) {
var val string
var err error
if hostname != "" {
val, err = c.cfg.Get([]string{hosts, hostname, key})
if err == nil {
return val, err
}
}

val, err = c.cfg.Get([]string{key})
if err == nil {
return val, err
}

if defaultExists(key) {
return defaultFor(key), nil
}

return val, err
}

func (c *cfg) Set(hostname, key, value string) {
if hostname == "" {
c.cfg.Set([]string{key}, value)
}
c.cfg.Set([]string{hosts, hostname, key}, value)
}

func (c *cfg) UnsetHost(hostname string) {
if hostname == "" {
return
}
_ = c.cfg.Remove([]string{hosts, hostname})
}

func (c *cfg) Hosts() []string {
return ghAuth.KnownHosts()
}

func (c *cfg) DefaultHost() (string, string) {
return ghAuth.DefaultHost()
}

func (c *cfg) Aliases() *AliasConfig {
return &AliasConfig{cfg: c.cfg}
}

func (c *cfg) Write() error {
return ghConfig.Write(c.cfg)
}

func defaultFor(key string) string {
for _, co := range configOptions {
if co.Key == key {
return co.DefaultValue
}
}
return ""
}

func defaultExists(key string) bool {
for _, co := range configOptions {
if co.Key == key {
return true
}
}
return false
}

type AliasConfig struct {
cfg *ghConfig.Config
}

func (a *AliasConfig) Get(alias string) (string, error) {
return a.cfg.Get([]string{aliases, alias})
}

func (a *AliasConfig) Add(alias, expansion string) {
a.cfg.Set([]string{aliases, alias}, expansion)
}

func (a *AliasConfig) Delete(alias string) error {
return a.cfg.Remove([]string{aliases, alias})
}

func (a *AliasConfig) All() map[string]string {
out := map[string]string{}
keys, err := a.cfg.Keys([]string{aliases})
if err != nil {
return out
}
for _, key := range keys {
val, _ := a.cfg.Get([]string{aliases, key})
out[key] = val
}
return out
}

type ConfigOption struct {
Key string
Description string
DefaultValue string
AllowedValues []string
}

var configOptions = []ConfigOption{
{
Key: "git_protocol",
Description: "the protocol to use for git clone and push operations",
DefaultValue: "https",
AllowedValues: []string{"https", "ssh"},
},
{
Key: "editor",
Description: "the text editor program to use for authoring text",
DefaultValue: "",
},
{
Key: "prompt",
Description: "toggle interactive prompting in the terminal",
DefaultValue: "enabled",
AllowedValues: []string{"enabled", "disabled"},
},
{
Key: "pager",
Description: "the terminal pager program to send standard output to",
DefaultValue: "",
},
{
Key: "http_unix_socket",
Description: "the path to a Unix socket through which to make an HTTP connection",
DefaultValue: "",
},
{
Key: "browser",
Description: "the web browser to use for opening URLs",
DefaultValue: "",
},
}

func ConfigOptions() []ConfigOption {
return configOptions
}

func HomeDirPath(subdir string) (string, error) {
homeDir, err := os.UserHomeDir()
if err != nil {
return "", err
}

newPath := filepath.Join(homeDir, subdir)
return newPath, nil
}

func StateDir() string {
return ghConfig.StateDir()
}

func DataDir() string {
return ghConfig.DataDir()
}

func ConfigDir() string {
return ghConfig.ConfigDir()
}

0 comments on commit 1e0d995

Please sign in to comment.