Skip to content

Commit d44a2cb

Browse files
committed
Adjust YAML encoding and style handling
Stop using yaml.FlowStyle and normalize sequence node styles when creating notifications nodes; ensure notifications nodes are explicit SequenceNodes and reset Style to zero where needed. Rely on yaml.Encoder to emit document separators instead of manually inserting "---" between encoded docs, and strip only the leading "---\n" to match CrowdSec's expected multi-document format. Also set encoder indentation to 1 and add comments explaining encoder behavior to prevent duplicate separators and maintain correct profiles.yaml formatting.
1 parent 330a67e commit d44a2cb

1 file changed

Lines changed: 9 additions & 11 deletions

File tree

internal/api/handlers/notifications_yaml.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -200,13 +200,12 @@ func updateProfilesYaml(path string, enable bool) error {
200200
if notificationsNode == nil {
201201
keyNode := &yaml.Node{Kind: yaml.ScalarNode, Value: "notifications"}
202202
notificationsNode = &yaml.Node{
203-
Kind: yaml.SequenceNode,
204-
Style: yaml.FlowStyle,
203+
Kind: yaml.SequenceNode,
205204
}
206205
profileNode.Content = append(profileNode.Content, keyNode, notificationsNode)
207206
} else if notificationsNode.Kind != yaml.SequenceNode {
208207
notificationsNode.Kind = yaml.SequenceNode
209-
notificationsNode.Style = yaml.FlowStyle
208+
notificationsNode.Style = 0
210209
notificationsNode.Content = []*yaml.Node{}
211210
profileNode.Content[notifIdx] = notificationsNode
212211
}
@@ -295,7 +294,7 @@ func updateProfilesYaml(path string, enable bool) error {
295294
}},
296295

297296
{Kind: yaml.ScalarNode, Value: "notifications"},
298-
{Kind: yaml.SequenceNode, Style: yaml.FlowStyle, Content: []*yaml.Node{
297+
{Kind: yaml.SequenceNode, Content: []*yaml.Node{
299298
{Kind: yaml.ScalarNode, Value: "discord"},
300299
}},
301300

@@ -311,25 +310,24 @@ func updateProfilesYaml(path string, enable bool) error {
311310
documents = append(documents, newDoc)
312311
}
313312

314-
// Write all documents back to file
313+
// Write all documents back to file, preserving multi-document format.
314+
// yaml.Encoder adds "---\n" before each document automatically,
315+
// so we just encode sequentially and clean up the leading separator.
315316
var buf bytes.Buffer
316317
encoder := yaml.NewEncoder(&buf)
317-
encoder.SetIndent(2)
318+
encoder.SetIndent(1)
318319

319320
for i, doc := range documents {
320-
if i > 0 {
321-
buf.WriteString("\n---\n")
322-
}
323321
if err := encoder.Encode(doc); err != nil {
324322
return fmt.Errorf("failed to marshal profiles.yaml document %d: %v", i, err)
325323
}
326324
}
327325
encoder.Close()
328326

329-
// Clean up the output - remove the extra "---" that encoder adds at the start
327+
// The encoder outputs "---\n<content>\n" per document.
328+
// Strip only the very first "---\n" to match CrowdSec's expected format.
330329
output := buf.Bytes()
331330
output = bytes.TrimPrefix(output, []byte("---\n"))
332331

333332
return os.WriteFile(path, output, 0644)
334333
}
335-

0 commit comments

Comments
 (0)