Skip to content

Commit 77dd4b1

Browse files
committed
Do not write empty entries in sketch.yaml
1 parent 96c16b2 commit 77dd4b1

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

Diff for: arduino/sketch/yaml.go

+16-3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import (
2323
"gopkg.in/yaml.v3"
2424
)
2525

26+
// updateOrAddYamlRootEntry updates or adds a new entry to the root of the yaml file.
27+
// If the value is empty the entry is removed.
2628
func updateOrAddYamlRootEntry(path *paths.Path, key, newValue string) error {
2729
var srcYaml []string
2830
if path.Exist() {
@@ -46,10 +48,16 @@ func updateOrAddYamlRootEntry(path *paths.Path, key, newValue string) error {
4648
updatedLine := key + ": " + strings.TrimSpace(string(v))
4749

4850
// Update or add the key/value pair into the original yaml
49-
addMissing := true
51+
addMissing := (newValue != "")
5052
for i, line := range srcYaml {
5153
if strings.HasPrefix(line, key+": ") {
52-
srcYaml[i] = updatedLine
54+
if newValue == "" {
55+
// Remove the key/value pair
56+
srcYaml = append(srcYaml[:i], srcYaml[i+1:]...)
57+
} else {
58+
// Update the key/value pair
59+
srcYaml[i] = updatedLine
60+
}
5361
addMissing = false
5462
break
5563
}
@@ -69,7 +77,12 @@ func updateOrAddYamlRootEntry(path *paths.Path, key, newValue string) error {
6977
if err := yaml.Unmarshal(dstYaml, &dst); err != nil {
7078
return fmt.Errorf("%s: %w", tr("could not update sketch project file"), err)
7179
}
72-
if dstMap, ok := dst.(map[string]interface{}); !ok || dstMap[key] != newValue {
80+
dstMap, ok := dst.(map[string]interface{})
81+
if !ok {
82+
return fmt.Errorf(tr("could not update sketch project file"))
83+
}
84+
writtenValue, notRemoved := dstMap[key]
85+
if (newValue == "" && notRemoved) || (newValue != "" && newValue != writtenValue) {
7386
return fmt.Errorf(tr("could not update sketch project file"))
7487
}
7588

0 commit comments

Comments
 (0)