X Tutup
Skip to content

Commit 7b4c0c5

Browse files
committed
WIP: handle empty aliases key
1 parent 9c4bf00 commit 7b4c0c5

File tree

2 files changed

+60
-14
lines changed

2 files changed

+60
-14
lines changed

internal/config/alias_config.go

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package config
22

33
import (
4+
"errors"
45
"fmt"
56

67
"gopkg.in/yaml.v3"
@@ -11,14 +12,6 @@ type AliasConfig struct {
1112
Parent Config
1213
}
1314

14-
// TODO at what point should the alias top level be added? Lazily on first write, I think;
15-
// but then we aren't initializing the default aliases that we want. also want to ensure that we
16-
// don't re-add the default aliases if people delete them.
17-
//
18-
// I think I'm not going to worry about it for now; config setup will add the aliases and existing
19-
// users can take some step later on to add them if they want them. we'll otherwise lazily create
20-
// the aliases: section on first write.
21-
2215
func (a *AliasConfig) Exists(alias string) bool {
2316
if a.Empty() {
2417
return false
@@ -36,18 +29,43 @@ func (a *AliasConfig) Get(alias string) string {
3629

3730
func (a *AliasConfig) Add(alias, expansion string) error {
3831
if a.Root == nil {
39-
// then we don't actually have an aliases stanza in the config yet.
40-
keyNode := &yaml.Node{
41-
Kind: yaml.ScalarNode,
42-
Value: "aliases",
32+
// TODO awful hack bad type conversion i'm sorry
33+
entry, err := a.Parent.(*fileConfig).FindEntryPrime("aliases")
34+
35+
var notFound *NotFoundError
36+
37+
if err != nil && !errors.As(err, &notFound) {
38+
return err
4339
}
4440
valueNode := &yaml.Node{
4541
Kind: yaml.MappingNode,
4642
Value: "",
4743
}
4844

4945
a.Root = valueNode
50-
a.Parent.Root().Content = append(a.Parent.Root().Content, keyNode, valueNode)
46+
if errors.As(err, &notFound) {
47+
// No aliases: key; just append new aliases key and empty map to end
48+
keyNode := &yaml.Node{
49+
Kind: yaml.ScalarNode,
50+
Value: "aliases",
51+
}
52+
a.Parent.Root().Content = append(a.Parent.Root().Content, keyNode, valueNode)
53+
} else {
54+
// Empty aliases; inject new value node after existing aliases key
55+
newContent := []*yaml.Node{}
56+
for i := 0; i < len(a.Parent.Root().Content); i++ {
57+
node := a.Parent.Root().Content[i]
58+
if i == entry.Index {
59+
}
60+
if i == entry.Index {
61+
newContent = append(newContent, entry.KeyNode, valueNode)
62+
i++
63+
} else {
64+
newContent = append(newContent, a.Parent.Root().Content[i])
65+
}
66+
}
67+
a.Parent.Root().Content = newContent
68+
}
5169
}
5270

5371
err := a.SetStringValue(alias, expansion)

internal/config/config_type.go

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,32 @@ func (cm *ConfigMap) SetStringValue(key, value string) error {
7373
return nil
7474
}
7575

76+
type ConfigEntry struct {
77+
KeyNode *yaml.Node
78+
ValueNode *yaml.Node
79+
Index int
80+
}
81+
82+
func (cm *ConfigMap) FindEntryPrime(key string) (ce *ConfigEntry, err error) {
83+
err = nil
84+
85+
ce = &ConfigEntry{}
86+
87+
topLevelKeys := cm.Root.Content
88+
for i, v := range topLevelKeys {
89+
if v.Value == key {
90+
ce.KeyNode = v
91+
ce.Index = i
92+
if i+1 < len(topLevelKeys) {
93+
ce.ValueNode = topLevelKeys[i+1]
94+
}
95+
return
96+
}
97+
}
98+
99+
return ce, &NotFoundError{errors.New("not found")}
100+
}
101+
76102
func (cm *ConfigMap) FindEntry(key string) (keyNode, valueNode *yaml.Node, err error) {
77103
err = nil
78104

@@ -195,7 +221,9 @@ func (c *fileConfig) Aliases() (*AliasConfig, error) {
195221

196222
func (c *fileConfig) parseAliasConfig(aliasesEntry *yaml.Node) (*AliasConfig, error) {
197223
if aliasesEntry.Kind != yaml.MappingNode {
198-
return nil, errors.New("expected aliases to be a map")
224+
return &AliasConfig{
225+
Parent: c,
226+
}, nil
199227
}
200228

201229
aliasConfig := AliasConfig{

0 commit comments

Comments
 (0)
X Tutup