X Tutup
Skip to content

Commit 4b415f8

Browse files
author
Nate Smith
authored
Merge pull request cli#5022 from cli/config-defaults
add GetOrDefault and related methods to Config
2 parents d0c7c48 + 30a30be commit 4b415f8

File tree

24 files changed

+94
-30
lines changed

24 files changed

+94
-30
lines changed

internal/config/config_file_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,13 @@ example.com:
8080
`)()
8181
config, err := parseConfig("config.yml")
8282
assert.NoError(t, err)
83-
val, err := config.Get("example.com", "git_protocol")
83+
val, err := config.GetOrDefault("example.com", "git_protocol")
8484
assert.NoError(t, err)
8585
assert.Equal(t, "https", val)
86-
val, err = config.Get("github.com", "git_protocol")
86+
val, err = config.GetOrDefault("github.com", "git_protocol")
8787
assert.NoError(t, err)
8888
assert.Equal(t, "ssh", val)
89-
val, err = config.Get("nonexistent.io", "git_protocol")
89+
val, err = config.GetOrDefault("nonexistent.io", "git_protocol")
9090
assert.NoError(t, err)
9191
assert.Equal(t, "ssh", val)
9292
}

internal/config/config_type.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ import (
99
// This interface describes interacting with some persistent configuration for gh.
1010
type Config interface {
1111
Get(string, string) (string, error)
12+
GetOrDefault(string, string) (string, error)
1213
GetWithSource(string, string) (string, string, error)
14+
GetOrDefaultWithSource(string, string) (string, string, error)
15+
Default(string) string
1316
Set(string, string, string) error
1417
UnsetHost(string)
1518
Hosts() ([]string, error)

internal/config/config_type_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func Test_defaultConfig(t *testing.T) {
5858
assert.Equal(t, expected, mainBuf.String())
5959
assert.Equal(t, "", hostsBuf.String())
6060

61-
proto, err := cfg.Get("", "git_protocol")
61+
proto, err := cfg.GetOrDefault("", "git_protocol")
6262
assert.NoError(t, err)
6363
assert.Equal(t, "https", proto)
6464

internal/config/from_env.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,24 @@ func (c *envConfig) GetWithSource(hostname, key string) (string, string, error)
7676
return c.Config.GetWithSource(hostname, key)
7777
}
7878

79+
func (c *envConfig) GetOrDefault(hostname, key string) (val string, err error) {
80+
val, _, err = c.GetOrDefaultWithSource(hostname, key)
81+
return
82+
}
83+
84+
func (c *envConfig) GetOrDefaultWithSource(hostname, key string) (val string, src string, err error) {
85+
val, src, err = c.GetWithSource(hostname, key)
86+
if err == nil && val == "" {
87+
val = c.Default(key)
88+
}
89+
90+
return
91+
}
92+
93+
func (c *envConfig) Default(key string) string {
94+
return c.Config.Default(key)
95+
}
96+
7997
func (c *envConfig) CheckWriteable(hostname, key string) error {
8098
if hostname != "" && key == "oauth_token" {
8199
if token, env := AuthTokenFromEnv(hostname); token != "" {

internal/config/from_file.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,24 @@ func (c *fileConfig) GetWithSource(hostname, key string) (string, string, error)
6565
return "", defaultSource, err
6666
}
6767

68-
if value == "" {
69-
return defaultFor(key), defaultSource, nil
68+
return value, defaultSource, nil
69+
}
70+
71+
func (c *fileConfig) GetOrDefault(hostname, key string) (val string, err error) {
72+
val, _, err = c.GetOrDefaultWithSource(hostname, key)
73+
return
74+
}
75+
76+
func (c *fileConfig) GetOrDefaultWithSource(hostname, key string) (val string, src string, err error) {
77+
val, src, err = c.GetWithSource(hostname, key)
78+
if err != nil && val == "" {
79+
val = c.Default(key)
7080
}
81+
return
82+
}
7183

72-
return value, defaultSource, nil
84+
func (c *fileConfig) Default(key string) string {
85+
return defaultFor(key)
7386
}
7487

7588
func (c *fileConfig) Set(hostname, key, value string) error {

internal/config/stub.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,23 @@ func (c ConfigStub) GetWithSource(host, key string) (string, string, error) {
2525
return "", "", errors.New("not found")
2626
}
2727

28+
func (c ConfigStub) GetOrDefault(hostname, key string) (val string, err error) {
29+
val, _, err = c.GetOrDefaultWithSource(hostname, key)
30+
return
31+
}
32+
33+
func (c ConfigStub) GetOrDefaultWithSource(hostname, key string) (val string, src string, err error) {
34+
val, src, err = c.GetWithSource(hostname, key)
35+
if err == nil && val == "" {
36+
val = c.Default(key)
37+
}
38+
return
39+
}
40+
41+
func (c ConfigStub) Default(key string) string {
42+
return defaultFor(key)
43+
}
44+
2845
func (c ConfigStub) Set(host, key, value string) error {
2946
c[genKey(host, key)] = value
3047
return nil

pkg/cmd/auth/gitcredential/helper_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,18 @@ import (
88
"github.com/cli/cli/v2/pkg/iostreams"
99
)
1010

11+
// why not just use the config stub argh
1112
type tinyConfig map[string]string
1213

1314
func (c tinyConfig) GetWithSource(host, key string) (string, string, error) {
1415
return c[fmt.Sprintf("%s:%s", host, key)], c["_source"], nil
1516
}
1617

18+
func (c tinyConfig) Get(host, key string) (val string, err error) {
19+
val, _, err = c.GetWithSource(host, key)
20+
return
21+
}
22+
1723
func Test_helperRun(t *testing.T) {
1824
tests := []struct {
1925
name string

pkg/cmd/auth/refresh/refresh.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ func refreshRun(opts *RefreshOptions) error {
146146
credentialFlow := &shared.GitCredentialFlow{
147147
Executable: opts.MainExecutable,
148148
}
149-
gitProtocol, _ := cfg.Get(hostname, "git_protocol")
149+
gitProtocol, _ := cfg.GetOrDefault(hostname, "git_protocol")
150150
if opts.Interactive && gitProtocol == "https" {
151151
if err := credentialFlow.Prompt(hostname); err != nil {
152152
return err

pkg/cmd/auth/status/status.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func NewCmdStatus(f *cmdutil.Factory, runF func(*StatusOptions) error) *cobra.Co
3535
Args: cobra.ExactArgs(0),
3636
Short: "View authentication status",
3737
Long: heredoc.Doc(`Verifies and displays information about your authentication state.
38-
38+
3939
This command will test your authentication state for each GitHub host that gh knows about and
4040
report on any issues.
4141
`),
@@ -127,7 +127,7 @@ func statusRun(opts *StatusOptions) error {
127127
addMsg("%s %s: api call failed: %s", cs.Red("X"), hostname, err)
128128
}
129129
addMsg("%s Logged in to %s as %s (%s)", cs.SuccessIcon(), hostname, cs.Bold(username), tokenSource)
130-
proto, _ := cfg.Get(hostname, "git_protocol")
130+
proto, _ := cfg.GetOrDefault(hostname, "git_protocol")
131131
if proto != "" {
132132
addMsg("%s Git operations for %s configured to use %s protocol.",
133133
cs.SuccessIcon(), hostname, cs.Bold(proto))

pkg/cmd/config/get/get.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func NewCmdConfigGet(f *cmdutil.Factory, runF func(*GetOptions) error) *cobra.Co
5353
}
5454

5555
func getRun(opts *GetOptions) error {
56-
val, err := opts.Config.Get(opts.Hostname, opts.Key)
56+
val, err := opts.Config.GetOrDefault(opts.Hostname, opts.Key)
5757
if err != nil {
5858
return err
5959
}

0 commit comments

Comments
 (0)
X Tutup