@@ -23,6 +23,8 @@ import (
23
23
"gopkg.in/yaml.v3"
24
24
)
25
25
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.
26
28
func updateOrAddYamlRootEntry (path * paths.Path , key , newValue string ) error {
27
29
var srcYaml []string
28
30
if path .Exist () {
@@ -46,10 +48,16 @@ func updateOrAddYamlRootEntry(path *paths.Path, key, newValue string) error {
46
48
updatedLine := key + ": " + strings .TrimSpace (string (v ))
47
49
48
50
// Update or add the key/value pair into the original yaml
49
- addMissing := true
51
+ addMissing := ( newValue != "" )
50
52
for i , line := range srcYaml {
51
53
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
+ }
53
61
addMissing = false
54
62
break
55
63
}
@@ -69,7 +77,12 @@ func updateOrAddYamlRootEntry(path *paths.Path, key, newValue string) error {
69
77
if err := yaml .Unmarshal (dstYaml , & dst ); err != nil {
70
78
return fmt .Errorf ("%s: %w" , tr ("could not update sketch project file" ), err )
71
79
}
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 ) {
73
86
return fmt .Errorf (tr ("could not update sketch project file" ))
74
87
}
75
88
0 commit comments