Skip to content

Commit

Permalink
Expose additional config functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
samcoe committed Jun 2, 2022
1 parent 0186f20 commit f9f38f0
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 8 deletions.
4 changes: 4 additions & 0 deletions internal/yamlmap/yaml_map.go
Expand Up @@ -147,6 +147,10 @@ func (m *Map) SetEntry(key string, value *Map) {
// has no impact for our purposes.
func (m *Map) SetModified() {
// Can not mark a non-mapping node as modified
if m.Node.Kind != yaml.MappingNode && m.Node.Tag == "!!null" {
m.Node.Kind = yaml.MappingNode
m.Node.Tag = "!!map"
}
if m.Node.Kind == yaml.MappingNode {
m.Node.Value = modified
}
Expand Down
7 changes: 7 additions & 0 deletions pkg/auth/auth.go
Expand Up @@ -4,13 +4,15 @@ package auth

import (
"os"
"strconv"
"strings"

"github.com/cli/go-gh/internal/set"
"github.com/cli/go-gh/pkg/config"
)

const (
codespaces = "CODESPACES"
defaultSource = "default"
ghEnterpriseToken = "GH_ENTERPRISE_TOKEN"
ghHost = "GH_HOST"
Expand Down Expand Up @@ -40,6 +42,11 @@ func tokenForHost(cfg *config.Config, host string) (string, string) {
if token := os.Getenv(githubEnterpriseToken); token != "" {
return token, githubEnterpriseToken
}
if isCodespaces, _ := strconv.ParseBool(os.Getenv(codespaces)); isCodespaces {
if token := os.Getenv(githubToken); token != "" {
return token, githubToken
}
}
if cfg != nil {
token, _ := cfg.Get([]string{hostsKey, host, oauthToken})
return token, oauthToken
Expand Down
10 changes: 5 additions & 5 deletions pkg/config/config.go
Expand Up @@ -205,11 +205,11 @@ func load(generalFilePath, hostsFilePath string) (*Config, error) {
}

func generalConfigFile() string {
return filepath.Join(configDir(), "config.yml")
return filepath.Join(ConfigDir(), "config.yml")
}

func hostsConfigFile() string {
return filepath.Join(configDir(), "hosts.yml")
return filepath.Join(ConfigDir(), "hosts.yml")
}

func mapFromFile(filename string) (*yamlmap.Map, error) {
Expand All @@ -225,7 +225,7 @@ func mapFromString(str string) (*yamlmap.Map, error) {
}

// Config path precedence: GH_CONFIG_DIR, XDG_CONFIG_HOME, AppData (windows only), HOME.
func configDir() string {
func ConfigDir() string {
var path string
if a := os.Getenv(ghConfigDir); a != "" {
path = a
Expand All @@ -241,7 +241,7 @@ func configDir() string {
}

// State path precedence: XDG_STATE_HOME, LocalAppData (windows only), HOME.
func stateDir() string {
func StateDir() string {
var path string
if a := os.Getenv(xdgStateHome); a != "" {
path = filepath.Join(a, "gh")
Expand All @@ -255,7 +255,7 @@ func stateDir() string {
}

// Data path precedence: XDG_DATA_HOME, LocalAppData (windows only), HOME.
func dataDir() string {
func DataDir() string {
var path string
if a := os.Getenv(xdgDataHome); a != "" {
path = filepath.Join(a, "gh")
Expand Down
6 changes: 3 additions & 3 deletions pkg/config/config_test.go
Expand Up @@ -92,7 +92,7 @@ func TestConfigDir(t *testing.T) {
defer os.Setenv(k, old)
}
}
assert.Equal(t, tt.output, configDir())
assert.Equal(t, tt.output, ConfigDir())
})
}
}
Expand Down Expand Up @@ -156,7 +156,7 @@ func TestStateDir(t *testing.T) {
defer os.Setenv(k, old)
}
}
assert.Equal(t, tt.output, stateDir())
assert.Equal(t, tt.output, StateDir())
})
}
}
Expand Down Expand Up @@ -220,7 +220,7 @@ func TestDataDir(t *testing.T) {
defer os.Setenv(k, old)
}
}
assert.Equal(t, tt.output, dataDir())
assert.Equal(t, tt.output, DataDir())
})
}
}
Expand Down

0 comments on commit f9f38f0

Please sign in to comment.