1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-03-17 21:18:31 +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:
Stefan Haller 2023-06-08 09:22:42 +02:00
parent 6acabba417
commit 4461dc68b7
2 changed files with 27 additions and 5 deletions

View File

@ -64,12 +64,26 @@ func updateYamlNode(node *yaml.Node, path []string, value string) error {
}
// 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{
Kind: yaml.ScalarNode,
Value: key,
}, &yaml.Node{
Kind: yaml.ScalarNode,
Value: value,
})
return nil
}, newNode)
return updateYamlNode(newNode, path[1:], value)
}

View File

@ -56,6 +56,14 @@ func TestUpdateYamlValue(t *testing.T) {
expectedOut: "foo:\n bar: qux\n",
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
{