X Tutup
Skip to content

Commit 54183f4

Browse files
committed
remove a gross type hack
1 parent 4804c8b commit 54183f4

File tree

2 files changed

+41
-59
lines changed

2 files changed

+41
-59
lines changed

internal/config/alias_config.go

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
package config
22

33
import (
4-
"errors"
54
"fmt"
6-
7-
"gopkg.in/yaml.v3"
85
)
96

107
type AliasConfig struct {
@@ -28,44 +25,6 @@ func (a *AliasConfig) Get(alias string) string {
2825
}
2926

3027
func (a *AliasConfig) Add(alias, expansion string) error {
31-
if a.Root == nil {
32-
// TODO awful hack bad type conversion i'm sorry. this is to support an empty aliases key and
33-
// ought to be generalized into ConfigMap or fileConfig (via Config interface) itself.
34-
entry, err := a.Parent.(*fileConfig).FindEntry("aliases")
35-
36-
var notFound *NotFoundError
37-
38-
if err != nil && !errors.As(err, &notFound) {
39-
return err
40-
}
41-
valueNode := &yaml.Node{
42-
Kind: yaml.MappingNode,
43-
Value: "",
44-
}
45-
46-
a.Root = valueNode
47-
if errors.As(err, &notFound) {
48-
// No aliases: key; just append new aliases key and empty map to end
49-
keyNode := &yaml.Node{
50-
Kind: yaml.ScalarNode,
51-
Value: "aliases",
52-
}
53-
a.Parent.Root().Content = append(a.Parent.Root().Content, keyNode, valueNode)
54-
} else {
55-
// Empty aliases; inject new value node after existing aliases key
56-
newContent := []*yaml.Node{}
57-
for i := 0; i < len(a.Parent.Root().Content); i++ {
58-
if i == entry.Index {
59-
newContent = append(newContent, entry.KeyNode, valueNode)
60-
i++
61-
} else {
62-
newContent = append(newContent, a.Parent.Root().Content[i])
63-
}
64-
}
65-
a.Parent.Root().Content = newContent
66-
}
67-
}
68-
6928
err := a.SetStringValue(alias, expansion)
7029
if err != nil {
7130
return fmt.Errorf("failed to update config: %w", err)

internal/config/config_type.go

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -191,34 +191,57 @@ func (c *fileConfig) Write() error {
191191
}
192192

193193
func (c *fileConfig) Aliases() (*AliasConfig, error) {
194+
// The complexity here is for dealing with either a missing or empty aliases key. It's something
195+
// we'll likely want for other config sections at some point.
194196
entry, err := c.FindEntry("aliases")
195197
var nfe *NotFoundError
196-
if err != nil && errors.As(err, &nfe) {
197-
// TODO does this make sense at all? want to simulate existing but empty key.
198-
return &AliasConfig{Parent: c}, nil
199-
}
200-
201-
aliasConfig, err := c.parseAliasConfig(entry.ValueNode)
202-
if err != nil {
198+
notFound := errors.As(err, &nfe)
199+
if err != nil && !notFound {
203200
return nil, err
204201
}
205202

206-
return aliasConfig, nil
207-
}
203+
toInsert := []*yaml.Node{}
208204

209-
func (c *fileConfig) parseAliasConfig(aliasesEntry *yaml.Node) (*AliasConfig, error) {
210-
if aliasesEntry.Kind != yaml.MappingNode {
211-
return &AliasConfig{
212-
Parent: c,
213-
}, nil
205+
keyNode := entry.KeyNode
206+
valueNode := entry.ValueNode
207+
208+
if keyNode == nil {
209+
keyNode = &yaml.Node{
210+
Kind: yaml.ScalarNode,
211+
Value: "aliases",
212+
}
213+
toInsert = append(toInsert, keyNode)
214214
}
215215

216-
aliasConfig := AliasConfig{
217-
Parent: c,
218-
ConfigMap: ConfigMap{Root: aliasesEntry},
216+
if valueNode == nil || valueNode.Kind != yaml.MappingNode {
217+
valueNode = &yaml.Node{
218+
Kind: yaml.MappingNode,
219+
Value: "",
220+
}
221+
toInsert = append(toInsert, valueNode)
222+
}
223+
224+
if len(toInsert) > 0 {
225+
newContent := []*yaml.Node{}
226+
if notFound {
227+
newContent = append(c.Root().Content, keyNode, valueNode)
228+
} else {
229+
for i := 0; i < len(c.Root().Content); i++ {
230+
if i == entry.Index {
231+
newContent = append(newContent, keyNode, valueNode)
232+
i++
233+
} else {
234+
newContent = append(newContent, c.Root().Content[i])
235+
}
236+
}
237+
}
238+
c.Root().Content = newContent
219239
}
220240

221-
return &aliasConfig, nil
241+
return &AliasConfig{
242+
Parent: c,
243+
ConfigMap: ConfigMap{Root: valueNode},
244+
}, nil
222245
}
223246

224247
func (c *fileConfig) Hosts() ([]*HostConfig, error) {

0 commit comments

Comments
 (0)
X Tutup