From cabf0b19727fd02ebef733715fe068f685a8d50b Mon Sep 17 00:00:00 2001 From: vilmibm Date: Wed, 10 Jun 2020 16:37:07 -0500 Subject: [PATCH 1/3] populate initial config --- context/context.go | 5 ++- internal/config/config_type.go | 57 ++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/context/context.go b/context/context.go index 236f9e722..24f81142c 100644 --- a/context/context.go +++ b/context/context.go @@ -165,7 +165,10 @@ func (c *fsContext) Config() (config.Config, error) { if c.config == nil { cfg, err := config.ParseDefaultConfig() if errors.Is(err, os.ErrNotExist) { - cfg = config.NewBlankConfig() + cfg, err = config.InitDefaultConfig() + if err != nil { + return nil, fmt.Errorf("could not create default config: %w", err) + } } else if err != nil { return nil, err } diff --git a/internal/config/config_type.go b/internal/config/config_type.go index 8db26f965..7f98341b3 100644 --- a/internal/config/config_type.go +++ b/internal/config/config_type.go @@ -122,6 +122,63 @@ func NewConfig(root *yaml.Node) Config { } } +func InitDefaultConfig() (Config, error) { + cfg := NewConfig(&yaml.Node{ + Kind: yaml.DocumentNode, + Content: []*yaml.Node{ + { + Kind: yaml.MappingNode, + Content: []*yaml.Node{ + { + HeadComment: "What protocol to use when performing git operations. Supported values: ssh, https", + Kind: yaml.ScalarNode, + Value: "git_protocol", + }, + { + Kind: yaml.ScalarNode, + Value: "https", + }, + { + HeadComment: "What editor gh should run when creating issues, pull requests, etc. If blank, will refer to environment.", + Kind: yaml.ScalarNode, + Value: "editor", + }, + { + Kind: yaml.ScalarNode, + Value: "", + }, + { + HeadComment: "Aliases allow you to create nicknames for gh commands", + Kind: yaml.ScalarNode, + Value: "aliases", + }, + { + Kind: yaml.MappingNode, + Content: []*yaml.Node{ + { + Kind: yaml.ScalarNode, + Value: "co", + }, + { + Kind: yaml.ScalarNode, + Value: "pr checkout", + }, + }, + }, + }, + }, + }, + }) + + err := cfg.Write() + if err != nil { + return nil, err + } + + return cfg, nil + +} + func NewBlankConfig() Config { return NewConfig(&yaml.Node{ Kind: yaml.DocumentNode, From 47102fe4277ebd7346317573c2b9eae98bef60ab Mon Sep 17 00:00:00 2001 From: vilmibm Date: Thu, 11 Jun 2020 14:04:29 -0500 Subject: [PATCH 2/3] just fill in the blank config with default content --- context/context.go | 5 +---- internal/config/config_type.go | 19 ++----------------- 2 files changed, 3 insertions(+), 21 deletions(-) diff --git a/context/context.go b/context/context.go index 24f81142c..236f9e722 100644 --- a/context/context.go +++ b/context/context.go @@ -165,10 +165,7 @@ func (c *fsContext) Config() (config.Config, error) { if c.config == nil { cfg, err := config.ParseDefaultConfig() if errors.Is(err, os.ErrNotExist) { - cfg, err = config.InitDefaultConfig() - if err != nil { - return nil, fmt.Errorf("could not create default config: %w", err) - } + cfg = config.NewBlankConfig() } else if err != nil { return nil, err } diff --git a/internal/config/config_type.go b/internal/config/config_type.go index 7f98341b3..a57d21dec 100644 --- a/internal/config/config_type.go +++ b/internal/config/config_type.go @@ -122,8 +122,8 @@ func NewConfig(root *yaml.Node) Config { } } -func InitDefaultConfig() (Config, error) { - cfg := NewConfig(&yaml.Node{ +func NewBlankConfig() Config { + return NewConfig(&yaml.Node{ Kind: yaml.DocumentNode, Content: []*yaml.Node{ { @@ -169,21 +169,6 @@ func InitDefaultConfig() (Config, error) { }, }, }) - - err := cfg.Write() - if err != nil { - return nil, err - } - - return cfg, nil - -} - -func NewBlankConfig() Config { - return NewConfig(&yaml.Node{ - Kind: yaml.DocumentNode, - Content: []*yaml.Node{{Kind: yaml.MappingNode}}, - }) } // This type implements a Config interface and represents a config file on disk. From 64a7fd420091a9da94f78003474781e0d4a44050 Mon Sep 17 00:00:00 2001 From: vilmibm Date: Thu, 11 Jun 2020 14:04:41 -0500 Subject: [PATCH 3/3] test default configuration --- internal/config/config_file_test.go | 9 +++++---- internal/config/config_type_test.go | 26 +++++++++++++++++++++----- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/internal/config/config_file_test.go b/internal/config/config_file_test.go index edc3a6ae9..d19130bb5 100644 --- a/internal/config/config_file_test.go +++ b/internal/config/config_file_test.go @@ -7,6 +7,7 @@ import ( "reflect" "testing" + "github.com/stretchr/testify/assert" "gopkg.in/yaml.v3" ) @@ -96,16 +97,16 @@ github.com: defer StubBackupConfig()() _, err := ParseConfig("config.yml") - eq(t, err, nil) + assert.Nil(t, err) - expectedMain := "" + expectedMain := "# What protocol to use when performing git operations. Supported values: ssh, https\ngit_protocol: https\n# What editor gh should run when creating issues, pull requests, etc. If blank, will refer to environment.\neditor:\n# Aliases allow you to create nicknames for gh commands\naliases:\n co: pr checkout\n" expectedHosts := `github.com: user: keiyuri oauth_token: "123456" ` - eq(t, mainBuf.String(), expectedMain) - eq(t, hostsBuf.String(), expectedHosts) + assert.Equal(t, expectedMain, mainBuf.String()) + assert.Equal(t, expectedHosts, hostsBuf.String()) } func Test_parseConfigFile(t *testing.T) { diff --git a/internal/config/config_type_test.go b/internal/config/config_type_test.go index 1c8105186..df33effa3 100644 --- a/internal/config/config_type_test.go +++ b/internal/config/config_type_test.go @@ -19,7 +19,8 @@ func Test_fileConfig_Set(t *testing.T) { assert.NoError(t, c.Set("github.com", "user", "hubot")) assert.NoError(t, c.Write()) - assert.Equal(t, "editor: nano\n", mainBuf.String()) + expected := "# What protocol to use when performing git operations. Supported values: ssh, https\ngit_protocol: https\n# What editor gh should run when creating issues, pull requests, etc. If blank, will refer to environment.\neditor: nano\n# Aliases allow you to create nicknames for gh commands\naliases:\n co: pr checkout\n" + assert.Equal(t, expected, mainBuf.String()) assert.Equal(t, `github.com: git_protocol: ssh user: hubot @@ -28,14 +29,29 @@ example.com: `, hostsBuf.String()) } -func Test_fileConfig_Write(t *testing.T) { +func Test_defaultConfig(t *testing.T) { mainBuf := bytes.Buffer{} hostsBuf := bytes.Buffer{} defer StubWriteConfig(&mainBuf, &hostsBuf)() - c := NewBlankConfig() - assert.NoError(t, c.Write()) + cfg := NewBlankConfig() + assert.NoError(t, cfg.Write()) - assert.Equal(t, "", mainBuf.String()) + expected := "# What protocol to use when performing git operations. Supported values: ssh, https\ngit_protocol: https\n# What editor gh should run when creating issues, pull requests, etc. If blank, will refer to environment.\neditor:\n# Aliases allow you to create nicknames for gh commands\naliases:\n co: pr checkout\n" + assert.Equal(t, expected, mainBuf.String()) assert.Equal(t, "", hostsBuf.String()) + + proto, err := cfg.Get("", "git_protocol") + assert.Nil(t, err) + assert.Equal(t, "https", proto) + + editor, err := cfg.Get("", "editor") + assert.Nil(t, err) + assert.Equal(t, "", editor) + + aliases, err := cfg.Aliases() + assert.Nil(t, err) + assert.Equal(t, len(aliases.All()), 1) + expansion, _ := aliases.Get("co") + assert.Equal(t, expansion, "pr checkout") }