From c4693077aaa4b1317f9f8d807b61db360b7b48a5 Mon Sep 17 00:00:00 2001 From: vilmibm Date: Mon, 20 Apr 2020 13:57:16 -0500 Subject: [PATCH] move config stuff to its own package --- cmd/gh/main.go | 4 ++-- command/config_test.go | 10 ++++---- command/root.go | 5 ++-- command/testing.go | 3 ++- context/blank_context.go | 7 +++--- context/context.go | 23 +++++++------------ context/remote_test.go | 8 +++++++ {context => internal/config}/config_file.go | 19 +++++++++++---- .../config}/config_file_test.go | 2 +- {context => internal/config}/config_setup.go | 2 +- .../config}/config_success.go | 2 +- {context => internal/config}/config_type.go | 4 ++-- {context => internal/config}/testing.go | 2 +- internal/ghrepo/repo.go | 1 + 14 files changed, 54 insertions(+), 38 deletions(-) rename {context => internal/config}/config_file.go (89%) rename {context => internal/config}/config_file_test.go (99%) rename {context => internal/config}/config_setup.go (99%) rename {context => internal/config}/config_success.go (98%) rename {context => internal/config}/config_type.go (98%) rename {context => internal/config}/testing.go (97%) diff --git a/cmd/gh/main.go b/cmd/gh/main.go index 17f2042f2..9ddf79315 100644 --- a/cmd/gh/main.go +++ b/cmd/gh/main.go @@ -10,7 +10,7 @@ import ( "strings" "github.com/cli/cli/command" - "github.com/cli/cli/context" + "github.com/cli/cli/internal/config" "github.com/cli/cli/update" "github.com/cli/cli/utils" "github.com/mgutz/ansi" @@ -88,6 +88,6 @@ func checkForUpdate(currentVersion string) (*update.ReleaseInfo, error) { } repo := updaterEnabled - stateFilePath := path.Join(context.ConfigDir(), "state.yml") + stateFilePath := path.Join(config.ConfigDir(), "state.yml") return update.CheckForUpdate(client, stateFilePath, repo, currentVersion) } diff --git a/command/config_test.go b/command/config_test.go index d75408d48..a610cb0c8 100644 --- a/command/config_test.go +++ b/command/config_test.go @@ -4,7 +4,7 @@ import ( "bytes" "testing" - "github.com/cli/cli/context" + "github.com/cli/cli/internal/config" ) func TestConfigGet(t *testing.T) { @@ -50,7 +50,7 @@ func TestConfigSet(t *testing.T) { initBlankContext("", "OWNER/REPO", "master") buf := bytes.NewBufferString("") - defer context.StubWriteConfig(buf)() + defer config.StubWriteConfig(buf)() output, err := RunCommand(configSetCmd, "config set editor ed") if err != nil { t.Fatalf("error running command `config set editor ed`: %v", err) @@ -80,7 +80,7 @@ editor: ed initBlankContext(cfg, "OWNER/REPO", "master") buf := bytes.NewBufferString("") - defer context.StubWriteConfig(buf)() + defer config.StubWriteConfig(buf)() output, err := RunCommand(configSetCmd, "config set editor vim") if err != nil { @@ -122,7 +122,7 @@ func TestConfigSetHost(t *testing.T) { initBlankContext("", "OWNER/REPO", "master") buf := bytes.NewBufferString("") - defer context.StubWriteConfig(buf)() + defer config.StubWriteConfig(buf)() output, err := RunCommand(configSetCmd, "config set -hgithub.com git_protocol ssh") if err != nil { t.Fatalf("error running command `config set editor ed`: %v", err) @@ -152,7 +152,7 @@ hosts: initBlankContext(cfg, "OWNER/REPO", "master") buf := bytes.NewBufferString("") - defer context.StubWriteConfig(buf)() + defer config.StubWriteConfig(buf)() output, err := RunCommand(configSetCmd, "config set -hgithub.com git_protocol https") if err != nil { diff --git a/command/root.go b/command/root.go index 98939a279..52c268b5e 100644 --- a/command/root.go +++ b/command/root.go @@ -10,6 +10,7 @@ import ( "github.com/cli/cli/api" "github.com/cli/cli/context" + "github.com/cli/cli/internal/config" "github.com/cli/cli/internal/ghrepo" "github.com/cli/cli/utils" @@ -17,7 +18,7 @@ import ( "github.com/spf13/pflag" ) -// TODO these are sprinkled across command, context, and ghrepo +// TODO these are sprinkled across command, context, config, and ghrepo const defaultHostname = "github.com" // Version is dynamically set by the toolchain or overridden by the Makefile. @@ -110,7 +111,7 @@ func BasicClient() (*api.Client, error) { } opts = append(opts, api.AddHeader("User-Agent", fmt.Sprintf("GitHub CLI %s", Version))) - c, err := context.ParseDefaultConfig() + c, err := config.ParseDefaultConfig() if err != nil { return nil, err } diff --git a/command/testing.go b/command/testing.go index 9de9f4756..b9b0ba04a 100644 --- a/command/testing.go +++ b/command/testing.go @@ -10,6 +10,7 @@ import ( "github.com/cli/cli/api" "github.com/cli/cli/context" + "github.com/cli/cli/internal/config" ) const defaultTestConfig = `hosts: @@ -84,7 +85,7 @@ func initBlankContext(cfg, repo, branch string) { // NOTE we are not restoring the original readConfig; we never want to touch the config file on // disk during tests. - context.StubConfig(cfg) + config.StubConfig(cfg) return ctx } diff --git a/context/blank_context.go b/context/blank_context.go index b04df0890..c5ceddff7 100644 --- a/context/blank_context.go +++ b/context/blank_context.go @@ -5,6 +5,7 @@ import ( "strings" "github.com/cli/cli/git" + "github.com/cli/cli/internal/config" "github.com/cli/cli/internal/ghrepo" ) @@ -22,12 +23,12 @@ type blankContext struct { remotes Remotes } -func (c *blankContext) Config() (Config, error) { - config, err := ParseConfig("boom.txt") +func (c *blankContext) Config() (config.Config, error) { + cfg, err := config.ParseConfig("boom.txt") if err != nil { panic(fmt.Sprintf("failed to parse config during tests. did you remember to stub? error: %s", err)) } - return config, nil + return cfg, nil } func (c *blankContext) AuthToken() (string, error) { diff --git a/context/context.go b/context/context.go index 92f48d5cb..5f36446d9 100644 --- a/context/context.go +++ b/context/context.go @@ -3,15 +3,17 @@ package context import ( "errors" "fmt" - "path" "sort" "github.com/cli/cli/api" "github.com/cli/cli/git" + "github.com/cli/cli/internal/config" "github.com/cli/cli/internal/ghrepo" - "github.com/mitchellh/go-homedir" ) +// TODO these are sprinkled across command, context, config, and ghrepo +const defaultHostname = "github.com" + // Context represents the interface for querying information about the current environment type Context interface { AuthToken() (string, error) @@ -22,7 +24,7 @@ type Context interface { Remotes() (Remotes, error) BaseRepo() (ghrepo.Interface, error) SetBaseRepo(string) - Config() (Config, error) + Config() (config.Config, error) } // cap the number of git remotes looked up, since the user might have an @@ -152,25 +154,16 @@ func New() Context { // A Context implementation that queries the filesystem type fsContext struct { - config Config + config config.Config remotes Remotes branch string baseRepo ghrepo.Interface authToken string } -func ConfigDir() string { - dir, _ := homedir.Expand("~/.config/gh") - return dir -} - -func configFile() string { - return path.Join(ConfigDir(), "config.yml") -} - -func (c *fsContext) Config() (Config, error) { +func (c *fsContext) Config() (config.Config, error) { if c.config == nil { - config, err := parseOrSetupConfigFile(configFile()) + config, err := config.ParseOrSetupConfigFile(config.ConfigFile()) if err != nil { return nil, err } diff --git a/context/remote_test.go b/context/remote_test.go index 5c3c46bc7..4b6838e23 100644 --- a/context/remote_test.go +++ b/context/remote_test.go @@ -4,6 +4,7 @@ import ( "bytes" "errors" "net/url" + "reflect" "testing" "github.com/cli/cli/api" @@ -11,6 +12,13 @@ import ( "github.com/cli/cli/internal/ghrepo" ) +func eq(t *testing.T, got interface{}, expected interface{}) { + t.Helper() + if !reflect.DeepEqual(got, expected) { + t.Errorf("expected: %v, got: %v", expected, got) + } +} + func Test_Remotes_FindByName(t *testing.T) { list := Remotes{ &Remote{Remote: &git.Remote{Name: "mona"}, Owner: "monalisa", Repo: "myfork"}, diff --git a/context/config_file.go b/internal/config/config_file.go similarity index 89% rename from context/config_file.go rename to internal/config/config_file.go index 33294717a..2670aff20 100644 --- a/context/config_file.go +++ b/internal/config/config_file.go @@ -1,4 +1,4 @@ -package context +package config import ( "errors" @@ -6,11 +6,22 @@ import ( "io" "io/ioutil" "os" + "path" + "github.com/mitchellh/go-homedir" "gopkg.in/yaml.v3" ) -func parseOrSetupConfigFile(fn string) (Config, error) { +func ConfigDir() string { + dir, _ := homedir.Expand("~/.config/gh") + return dir +} + +func ConfigFile() string { + return path.Join(ConfigDir(), "config.yml") +} + +func ParseOrSetupConfigFile(fn string) (Config, error) { config, err := ParseConfig(fn) if err != nil && errors.Is(err, os.ErrNotExist) { return setupConfigFile(fn) @@ -19,7 +30,7 @@ func parseOrSetupConfigFile(fn string) (Config, error) { } func ParseDefaultConfig() (Config, error) { - return ParseConfig(configFile()) + return ParseConfig(ConfigFile()) } var ReadConfigFile = func(fn string) ([]byte, error) { @@ -38,7 +49,7 @@ var ReadConfigFile = func(fn string) ([]byte, error) { } var WriteConfigFile = func(fn string, data []byte) error { - cfgFile, err := os.OpenFile(configFile(), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600) // cargo coded from setup + cfgFile, err := os.OpenFile(ConfigFile(), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600) // cargo coded from setup if err != nil { return err } diff --git a/context/config_file_test.go b/internal/config/config_file_test.go similarity index 99% rename from context/config_file_test.go rename to internal/config/config_file_test.go index 2ea48e7a7..4ddd63435 100644 --- a/context/config_file_test.go +++ b/internal/config/config_file_test.go @@ -1,4 +1,4 @@ -package context +package config import ( "bytes" diff --git a/context/config_setup.go b/internal/config/config_setup.go similarity index 99% rename from context/config_setup.go rename to internal/config/config_setup.go index 3c63d1d5a..9bc0fb775 100644 --- a/context/config_setup.go +++ b/internal/config/config_setup.go @@ -1,4 +1,4 @@ -package context +package config import ( "bufio" diff --git a/context/config_success.go b/internal/config/config_success.go similarity index 98% rename from context/config_success.go rename to internal/config/config_success.go index 1919fc1a4..c7a681bac 100644 --- a/context/config_success.go +++ b/internal/config/config_success.go @@ -1,4 +1,4 @@ -package context +package config const oauthSuccessPage = ` diff --git a/context/config_type.go b/internal/config/config_type.go similarity index 98% rename from context/config_type.go rename to internal/config/config_type.go index 23967684f..5b30ef70e 100644 --- a/context/config_type.go +++ b/internal/config/config_type.go @@ -1,4 +1,4 @@ -package context +package config import ( "errors" @@ -149,7 +149,7 @@ func (c *fileConfig) Write() error { return err } - return WriteConfigFile(configFile(), marshalled) + return WriteConfigFile(ConfigFile(), marshalled) } func (c *fileConfig) Hosts() ([]*HostConfig, error) { diff --git a/context/testing.go b/internal/config/testing.go similarity index 97% rename from context/testing.go rename to internal/config/testing.go index 5788e23fc..a91cedfae 100644 --- a/context/testing.go +++ b/internal/config/testing.go @@ -1,4 +1,4 @@ -package context +package config import ( "io" diff --git a/internal/ghrepo/repo.go b/internal/ghrepo/repo.go index a4bfa82d6..dc3115b90 100644 --- a/internal/ghrepo/repo.go +++ b/internal/ghrepo/repo.go @@ -6,6 +6,7 @@ import ( "strings" ) +// TODO these are sprinkled across command, context, config, and ghrepo const defaultHostname = "github.com" // Interface describes an object that represents a GitHub repository