mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-08-10 22:42:00 +02:00
Use indentation of 2 when rewriting auto-migrated config file
This seems to be what most people use when indenting yaml files, and it seems to make more sense than the default of 4. We also use it the example config in Config.md.
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package yaml_utils
|
package yaml_utils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
@@ -34,7 +35,7 @@ func UpdateYamlValue(yamlBytes []byte, path []string, value string) ([]byte, err
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Convert the updated YAML node back to YAML bytes.
|
// Convert the updated YAML node back to YAML bytes.
|
||||||
updatedYAMLBytes, err := yaml.Marshal(body)
|
updatedYAMLBytes, err := yamlMarshal(body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to convert YAML node to bytes: %w", err)
|
return nil, fmt.Errorf("failed to convert YAML node to bytes: %w", err)
|
||||||
}
|
}
|
||||||
@@ -126,7 +127,7 @@ func TransformNode(yamlBytes []byte, path []string, transform func(node *yaml.No
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Convert the updated YAML node back to YAML bytes.
|
// Convert the updated YAML node back to YAML bytes.
|
||||||
updatedYAMLBytes, err := yaml.Marshal(body)
|
updatedYAMLBytes, err := yamlMarshal(body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to convert YAML node to bytes: %w", err)
|
return nil, fmt.Errorf("failed to convert YAML node to bytes: %w", err)
|
||||||
}
|
}
|
||||||
@@ -170,7 +171,7 @@ func RenameYamlKey(yamlBytes []byte, path []string, newKey string) ([]byte, erro
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Convert the updated YAML node back to YAML bytes.
|
// Convert the updated YAML node back to YAML bytes.
|
||||||
updatedYAMLBytes, err := yaml.Marshal(body)
|
updatedYAMLBytes, err := yamlMarshal(body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to convert YAML node to bytes: %w", err)
|
return nil, fmt.Errorf("failed to convert YAML node to bytes: %w", err)
|
||||||
}
|
}
|
||||||
@@ -227,7 +228,7 @@ func Walk(yamlBytes []byte, callback func(node *yaml.Node, path string) bool) ([
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Convert the updated YAML node back to YAML bytes.
|
// Convert the updated YAML node back to YAML bytes.
|
||||||
updatedYAMLBytes, err := yaml.Marshal(body)
|
updatedYAMLBytes, err := yamlMarshal(body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to convert YAML node to bytes: %w", err)
|
return nil, fmt.Errorf("failed to convert YAML node to bytes: %w", err)
|
||||||
}
|
}
|
||||||
@@ -273,3 +274,12 @@ func walk(node *yaml.Node, path string, callback func(*yaml.Node, string) bool)
|
|||||||
|
|
||||||
return didChange, nil
|
return didChange, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func yamlMarshal(node *yaml.Node) ([]byte, error) {
|
||||||
|
var buffer bytes.Buffer
|
||||||
|
encoder := yaml.NewEncoder(&buffer)
|
||||||
|
encoder.SetIndent(2)
|
||||||
|
|
||||||
|
err := encoder.Encode(node)
|
||||||
|
return buffer.Bytes(), err
|
||||||
|
}
|
||||||
|
@@ -50,12 +50,11 @@ func TestUpdateYamlValue(t *testing.T) {
|
|||||||
expectedErr: "",
|
expectedErr: "",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "nested update",
|
name: "nested update",
|
||||||
in: "foo:\n bar: baz\n",
|
in: "foo:\n bar: baz\n",
|
||||||
path: []string{"foo", "bar"},
|
path: []string{"foo", "bar"},
|
||||||
value: "qux",
|
value: "qux",
|
||||||
// indentation is not preserved. See https://github.com/go-yaml/yaml/issues/899
|
expectedOut: "foo:\n bar: qux\n",
|
||||||
expectedOut: "foo:\n bar: qux\n",
|
|
||||||
expectedErr: "",
|
expectedErr: "",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -63,7 +62,7 @@ func TestUpdateYamlValue(t *testing.T) {
|
|||||||
in: "",
|
in: "",
|
||||||
path: []string{"foo", "bar", "baz"},
|
path: []string{"foo", "bar", "baz"},
|
||||||
value: "qux",
|
value: "qux",
|
||||||
expectedOut: "foo:\n bar:\n baz: qux\n",
|
expectedOut: "foo:\n bar:\n baz: qux\n",
|
||||||
expectedErr: "",
|
expectedErr: "",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -134,21 +133,19 @@ func TestRenameYamlKey(t *testing.T) {
|
|||||||
expectedErr: "",
|
expectedErr: "",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "rename key, nested",
|
name: "rename key, nested",
|
||||||
in: "foo:\n bar: 5\n",
|
in: "foo:\n bar: 5\n",
|
||||||
path: []string{"foo", "bar"},
|
path: []string{"foo", "bar"},
|
||||||
newKey: "baz",
|
newKey: "baz",
|
||||||
// indentation is not preserved. See https://github.com/go-yaml/yaml/issues/899
|
expectedOut: "foo:\n baz: 5\n",
|
||||||
expectedOut: "foo:\n baz: 5\n",
|
|
||||||
expectedErr: "",
|
expectedErr: "",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "rename non-scalar key",
|
name: "rename non-scalar key",
|
||||||
in: "foo:\n bar: 5\n",
|
in: "foo:\n bar: 5\n",
|
||||||
path: []string{"foo"},
|
path: []string{"foo"},
|
||||||
newKey: "qux",
|
newKey: "qux",
|
||||||
// indentation is not preserved. See https://github.com/go-yaml/yaml/issues/899
|
expectedOut: "qux:\n bar: 5\n",
|
||||||
expectedOut: "qux:\n bar: 5\n",
|
|
||||||
expectedErr: "",
|
expectedErr: "",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -288,8 +285,7 @@ func TestWalk_inPlaceChanges(t *testing.T) {
|
|||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
},
|
},
|
||||||
// indentation is not preserved. See https://github.com/go-yaml/yaml/issues/899
|
expectedOut: "x:\n y: 7\n",
|
||||||
expectedOut: "x:\n y: 7\n",
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "change array value",
|
name: "change array value",
|
||||||
@@ -301,8 +297,7 @@ func TestWalk_inPlaceChanges(t *testing.T) {
|
|||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
},
|
},
|
||||||
// indentation is not preserved. See https://github.com/go-yaml/yaml/issues/899
|
expectedOut: "x:\n - y: 7\n",
|
||||||
expectedOut: "x:\n - y: 7\n",
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -366,8 +361,8 @@ foo:
|
|||||||
path: []string{"foo", "bar"},
|
path: []string{"foo", "bar"},
|
||||||
transform: transformIntValueToString,
|
transform: transformIntValueToString,
|
||||||
expectedOut: `foo:
|
expectedOut: `foo:
|
||||||
bar: "2"
|
bar: "2"
|
||||||
`, // Note the indentiation change and newlines because of how it re-marshalls
|
`, // Note the trailing newline changes because of how it re-marshalls
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Does nothing when already transformed",
|
name: "Does nothing when already transformed",
|
||||||
|
Reference in New Issue
Block a user