mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-08-10 22:42:00 +02:00
Create missing path elements
This fixes a serious error: trying to change a value on gui.someOption would add a someOption key at root if gui doesn't exist.
This commit is contained in:
@@ -64,12 +64,26 @@ func updateYamlNode(node *yaml.Node, path []string, value string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// if the key doesn't exist, we'll add it
|
// if the key doesn't exist, we'll add it
|
||||||
|
|
||||||
|
// at end of path: add the new key, done
|
||||||
|
if len(path) == 1 {
|
||||||
|
node.Content = append(node.Content, &yaml.Node{
|
||||||
|
Kind: yaml.ScalarNode,
|
||||||
|
Value: key,
|
||||||
|
}, &yaml.Node{
|
||||||
|
Kind: yaml.ScalarNode,
|
||||||
|
Value: value,
|
||||||
|
})
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// otherwise, create the missing intermediate node and continue
|
||||||
|
newNode := &yaml.Node{
|
||||||
|
Kind: yaml.MappingNode,
|
||||||
|
}
|
||||||
node.Content = append(node.Content, &yaml.Node{
|
node.Content = append(node.Content, &yaml.Node{
|
||||||
Kind: yaml.ScalarNode,
|
Kind: yaml.ScalarNode,
|
||||||
Value: key,
|
Value: key,
|
||||||
}, &yaml.Node{
|
}, newNode)
|
||||||
Kind: yaml.ScalarNode,
|
return updateYamlNode(newNode, path[1:], value)
|
||||||
Value: value,
|
|
||||||
})
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
@@ -56,6 +56,14 @@ func TestUpdateYamlValue(t *testing.T) {
|
|||||||
expectedOut: "foo:\n bar: qux\n",
|
expectedOut: "foo:\n bar: qux\n",
|
||||||
expectedErr: "",
|
expectedErr: "",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "nested where parents doesn't exist yet",
|
||||||
|
in: "",
|
||||||
|
path: []string{"foo", "bar", "baz"},
|
||||||
|
value: "qux",
|
||||||
|
expectedOut: "foo:\n bar:\n baz: qux\n",
|
||||||
|
expectedErr: "",
|
||||||
|
},
|
||||||
|
|
||||||
// Error cases
|
// Error cases
|
||||||
{
|
{
|
||||||
|
Reference in New Issue
Block a user