Skip to content

Commit c469307

Browse files
committed
move config stuff to its own package
1 parent 815f461 commit c469307

File tree

14 files changed

+54
-38
lines changed

14 files changed

+54
-38
lines changed

cmd/gh/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"strings"
1111

1212
"github.com/cli/cli/command"
13-
"github.com/cli/cli/context"
13+
"github.com/cli/cli/internal/config"
1414
"github.com/cli/cli/update"
1515
"github.com/cli/cli/utils"
1616
"github.com/mgutz/ansi"
@@ -88,6 +88,6 @@ func checkForUpdate(currentVersion string) (*update.ReleaseInfo, error) {
8888
}
8989

9090
repo := updaterEnabled
91-
stateFilePath := path.Join(context.ConfigDir(), "state.yml")
91+
stateFilePath := path.Join(config.ConfigDir(), "state.yml")
9292
return update.CheckForUpdate(client, stateFilePath, repo, currentVersion)
9393
}

command/config_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"bytes"
55
"testing"
66

7-
"github.com/cli/cli/context"
7+
"github.com/cli/cli/internal/config"
88
)
99

1010
func TestConfigGet(t *testing.T) {
@@ -50,7 +50,7 @@ func TestConfigSet(t *testing.T) {
5050
initBlankContext("", "OWNER/REPO", "master")
5151

5252
buf := bytes.NewBufferString("")
53-
defer context.StubWriteConfig(buf)()
53+
defer config.StubWriteConfig(buf)()
5454
output, err := RunCommand(configSetCmd, "config set editor ed")
5555
if err != nil {
5656
t.Fatalf("error running command `config set editor ed`: %v", err)
@@ -80,7 +80,7 @@ editor: ed
8080
initBlankContext(cfg, "OWNER/REPO", "master")
8181

8282
buf := bytes.NewBufferString("")
83-
defer context.StubWriteConfig(buf)()
83+
defer config.StubWriteConfig(buf)()
8484

8585
output, err := RunCommand(configSetCmd, "config set editor vim")
8686
if err != nil {
@@ -122,7 +122,7 @@ func TestConfigSetHost(t *testing.T) {
122122
initBlankContext("", "OWNER/REPO", "master")
123123

124124
buf := bytes.NewBufferString("")
125-
defer context.StubWriteConfig(buf)()
125+
defer config.StubWriteConfig(buf)()
126126
output, err := RunCommand(configSetCmd, "config set -hgithub.com git_protocol ssh")
127127
if err != nil {
128128
t.Fatalf("error running command `config set editor ed`: %v", err)
@@ -152,7 +152,7 @@ hosts:
152152
initBlankContext(cfg, "OWNER/REPO", "master")
153153

154154
buf := bytes.NewBufferString("")
155-
defer context.StubWriteConfig(buf)()
155+
defer config.StubWriteConfig(buf)()
156156

157157
output, err := RunCommand(configSetCmd, "config set -hgithub.com git_protocol https")
158158
if err != nil {

command/root.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@ import (
1010

1111
"github.com/cli/cli/api"
1212
"github.com/cli/cli/context"
13+
"github.com/cli/cli/internal/config"
1314
"github.com/cli/cli/internal/ghrepo"
1415
"github.com/cli/cli/utils"
1516

1617
"github.com/spf13/cobra"
1718
"github.com/spf13/pflag"
1819
)
1920

20-
// TODO these are sprinkled across command, context, and ghrepo
21+
// TODO these are sprinkled across command, context, config, and ghrepo
2122
const defaultHostname = "github.com"
2223

2324
// Version is dynamically set by the toolchain or overridden by the Makefile.
@@ -110,7 +111,7 @@ func BasicClient() (*api.Client, error) {
110111
}
111112
opts = append(opts, api.AddHeader("User-Agent", fmt.Sprintf("GitHub CLI %s", Version)))
112113

113-
c, err := context.ParseDefaultConfig()
114+
c, err := config.ParseDefaultConfig()
114115
if err != nil {
115116
return nil, err
116117
}

command/testing.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
"github.com/cli/cli/api"
1212
"github.com/cli/cli/context"
13+
"github.com/cli/cli/internal/config"
1314
)
1415

1516
const defaultTestConfig = `hosts:
@@ -84,7 +85,7 @@ func initBlankContext(cfg, repo, branch string) {
8485

8586
// NOTE we are not restoring the original readConfig; we never want to touch the config file on
8687
// disk during tests.
87-
context.StubConfig(cfg)
88+
config.StubConfig(cfg)
8889

8990
return ctx
9091
}

context/blank_context.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"strings"
66

77
"github.com/cli/cli/git"
8+
"github.com/cli/cli/internal/config"
89
"github.com/cli/cli/internal/ghrepo"
910
)
1011

@@ -22,12 +23,12 @@ type blankContext struct {
2223
remotes Remotes
2324
}
2425

25-
func (c *blankContext) Config() (Config, error) {
26-
config, err := ParseConfig("boom.txt")
26+
func (c *blankContext) Config() (config.Config, error) {
27+
cfg, err := config.ParseConfig("boom.txt")
2728
if err != nil {
2829
panic(fmt.Sprintf("failed to parse config during tests. did you remember to stub? error: %s", err))
2930
}
30-
return config, nil
31+
return cfg, nil
3132
}
3233

3334
func (c *blankContext) AuthToken() (string, error) {

context/context.go

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,17 @@ package context
33
import (
44
"errors"
55
"fmt"
6-
"path"
76
"sort"
87

98
"github.com/cli/cli/api"
109
"github.com/cli/cli/git"
10+
"github.com/cli/cli/internal/config"
1111
"github.com/cli/cli/internal/ghrepo"
12-
"github.com/mitchellh/go-homedir"
1312
)
1413

14+
// TODO these are sprinkled across command, context, config, and ghrepo
15+
const defaultHostname = "github.com"
16+
1517
// Context represents the interface for querying information about the current environment
1618
type Context interface {
1719
AuthToken() (string, error)
@@ -22,7 +24,7 @@ type Context interface {
2224
Remotes() (Remotes, error)
2325
BaseRepo() (ghrepo.Interface, error)
2426
SetBaseRepo(string)
25-
Config() (Config, error)
27+
Config() (config.Config, error)
2628
}
2729

2830
// cap the number of git remotes looked up, since the user might have an
@@ -152,25 +154,16 @@ func New() Context {
152154

153155
// A Context implementation that queries the filesystem
154156
type fsContext struct {
155-
config Config
157+
config config.Config
156158
remotes Remotes
157159
branch string
158160
baseRepo ghrepo.Interface
159161
authToken string
160162
}
161163

162-
func ConfigDir() string {
163-
dir, _ := homedir.Expand("~/.config/gh")
164-
return dir
165-
}
166-
167-
func configFile() string {
168-
return path.Join(ConfigDir(), "config.yml")
169-
}
170-
171-
func (c *fsContext) Config() (Config, error) {
164+
func (c *fsContext) Config() (config.Config, error) {
172165
if c.config == nil {
173-
config, err := parseOrSetupConfigFile(configFile())
166+
config, err := config.ParseOrSetupConfigFile(config.ConfigFile())
174167
if err != nil {
175168
return nil, err
176169
}

context/remote_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,21 @@ import (
44
"bytes"
55
"errors"
66
"net/url"
7+
"reflect"
78
"testing"
89

910
"github.com/cli/cli/api"
1011
"github.com/cli/cli/git"
1112
"github.com/cli/cli/internal/ghrepo"
1213
)
1314

15+
func eq(t *testing.T, got interface{}, expected interface{}) {
16+
t.Helper()
17+
if !reflect.DeepEqual(got, expected) {
18+
t.Errorf("expected: %v, got: %v", expected, got)
19+
}
20+
}
21+
1422
func Test_Remotes_FindByName(t *testing.T) {
1523
list := Remotes{
1624
&Remote{Remote: &git.Remote{Name: "mona"}, Owner: "monalisa", Repo: "myfork"},
Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,27 @@
1-
package context
1+
package config
22

33
import (
44
"errors"
55
"fmt"
66
"io"
77
"io/ioutil"
88
"os"
9+
"path"
910

11+
"github.com/mitchellh/go-homedir"
1012
"gopkg.in/yaml.v3"
1113
)
1214

13-
func parseOrSetupConfigFile(fn string) (Config, error) {
15+
func ConfigDir() string {
16+
dir, _ := homedir.Expand("~/.config/gh")
17+
return dir
18+
}
19+
20+
func ConfigFile() string {
21+
return path.Join(ConfigDir(), "config.yml")
22+
}
23+
24+
func ParseOrSetupConfigFile(fn string) (Config, error) {
1425
config, err := ParseConfig(fn)
1526
if err != nil && errors.Is(err, os.ErrNotExist) {
1627
return setupConfigFile(fn)
@@ -19,7 +30,7 @@ func parseOrSetupConfigFile(fn string) (Config, error) {
1930
}
2031

2132
func ParseDefaultConfig() (Config, error) {
22-
return ParseConfig(configFile())
33+
return ParseConfig(ConfigFile())
2334
}
2435

2536
var ReadConfigFile = func(fn string) ([]byte, error) {
@@ -38,7 +49,7 @@ var ReadConfigFile = func(fn string) ([]byte, error) {
3849
}
3950

4051
var WriteConfigFile = func(fn string, data []byte) error {
41-
cfgFile, err := os.OpenFile(configFile(), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600) // cargo coded from setup
52+
cfgFile, err := os.OpenFile(ConfigFile(), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600) // cargo coded from setup
4253
if err != nil {
4354
return err
4455
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package context
1+
package config
22

33
import (
44
"bytes"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package context
1+
package config
22

33
import (
44
"bufio"

0 commit comments

Comments
 (0)