From 37761c97fa27c07d233a5e5250be519c44806e3e Mon Sep 17 00:00:00 2001 From: vilmibm Date: Wed, 27 May 2020 20:57:59 -0500 Subject: [PATCH] partial cleanup --- command/alias_test.go | 23 +++++++++++++++++++++++ internal/config/alias_config.go | 5 +---- internal/config/config_type.go | 33 ++++++++++----------------------- 3 files changed, 34 insertions(+), 27 deletions(-) diff --git a/command/alias_test.go b/command/alias_test.go index 1b0fc35ff..ad8ca33d6 100644 --- a/command/alias_test.go +++ b/command/alias_test.go @@ -23,6 +23,29 @@ func TestAliasSet_gh_command(t *testing.T) { eq(t, err.Error(), `could not create alias: "pr" is already a gh command`) } +func TestAliasSet_empty_aliases(t *testing.T) { + cfg := `--- +aliases: +hosts: + github.com: + user: OWNER + oauth_token: token123 +` + initBlankContext(cfg, "OWNER/REPO", "trunk") + + buf := bytes.NewBufferString("") + defer config.StubWriteConfig(buf)() + output, err := RunCommand("alias set co pr checkout -Rcool/repo") + + if err != nil { + t.Fatalf("unexpected error: %s", err) + } + + test.ExpectLines(t, output.String(), "TODO") + + // TODO +} + func TestAliasSet_existing_alias(t *testing.T) { cfg := `--- hosts: diff --git a/internal/config/alias_config.go b/internal/config/alias_config.go index 59e6a24c5..bbeaba75c 100644 --- a/internal/config/alias_config.go +++ b/internal/config/alias_config.go @@ -30,7 +30,7 @@ func (a *AliasConfig) Get(alias string) string { func (a *AliasConfig) Add(alias, expansion string) error { if a.Root == nil { // TODO awful hack bad type conversion i'm sorry - entry, err := a.Parent.(*fileConfig).FindEntryPrime("aliases") + entry, err := a.Parent.(*fileConfig).FindEntry("aliases") var notFound *NotFoundError @@ -54,9 +54,6 @@ func (a *AliasConfig) Add(alias, expansion string) error { // Empty aliases; inject new value node after existing aliases key newContent := []*yaml.Node{} for i := 0; i < len(a.Parent.Root().Content); i++ { - node := a.Parent.Root().Content[i] - if i == entry.Index { - } if i == entry.Index { newContent = append(newContent, entry.KeyNode, valueNode) i++ diff --git a/internal/config/config_type.go b/internal/config/config_type.go index ca8d23a29..55bba7d5b 100644 --- a/internal/config/config_type.go +++ b/internal/config/config_type.go @@ -41,18 +41,20 @@ func (cm *ConfigMap) Empty() bool { } func (cm *ConfigMap) GetStringValue(key string) (string, error) { - _, valueNode, err := cm.FindEntry(key) + entry, err := cm.FindEntry(key) if err != nil { return "", err } - return valueNode.Value, nil + return entry.ValueNode.Value, nil } func (cm *ConfigMap) SetStringValue(key, value string) error { - _, valueNode, err := cm.FindEntry(key) + entry, err := cm.FindEntry(key) var notFound *NotFoundError + valueNode := entry.ValueNode + if err != nil && errors.As(err, ¬Found) { keyNode := &yaml.Node{ Kind: yaml.ScalarNode, @@ -79,7 +81,7 @@ type ConfigEntry struct { Index int } -func (cm *ConfigMap) FindEntryPrime(key string) (ce *ConfigEntry, err error) { +func (cm *ConfigMap) FindEntry(key string) (ce *ConfigEntry, err error) { err = nil ce = &ConfigEntry{} @@ -99,21 +101,6 @@ func (cm *ConfigMap) FindEntryPrime(key string) (ce *ConfigEntry, err error) { return ce, &NotFoundError{errors.New("not found")} } -func (cm *ConfigMap) FindEntry(key string) (keyNode, valueNode *yaml.Node, err error) { - err = nil - - topLevelKeys := cm.Root.Content - for i, v := range topLevelKeys { - if v.Value == key && i+1 < len(topLevelKeys) { - keyNode = v - valueNode = topLevelKeys[i+1] - return - } - } - - return nil, nil, &NotFoundError{errors.New("not found")} -} - func NewConfig(root *yaml.Node) Config { return &fileConfig{ ConfigMap: ConfigMap{Root: root.Content[0]}, @@ -204,14 +191,14 @@ func (c *fileConfig) Write() error { } func (c *fileConfig) Aliases() (*AliasConfig, error) { - _, aliasesEntry, err := c.FindEntry("aliases") + entry, err := c.FindEntry("aliases") var nfe *NotFoundError if err != nil && errors.As(err, &nfe) { // TODO does this make sense at all? want to simulate existing but empty key. return &AliasConfig{Parent: c}, nil } - aliasConfig, err := c.parseAliasConfig(aliasesEntry) + aliasConfig, err := c.parseAliasConfig(entry.ValueNode) if err != nil { return nil, err } @@ -239,12 +226,12 @@ func (c *fileConfig) Hosts() ([]*HostConfig, error) { return c.hosts, nil } - _, hostsEntry, err := c.FindEntry("hosts") + entry, err := c.FindEntry("hosts") if err != nil { return nil, fmt.Errorf("could not find hosts config: %w", err) } - hostConfigs, err := c.parseHosts(hostsEntry) + hostConfigs, err := c.parseHosts(entry.ValueNode) if err != nil { return nil, fmt.Errorf("could not parse hosts config: %w", err) }