diff --git a/internal/config/config.go b/internal/config/config.go index 8f17825b..34d4cc34 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -162,8 +162,13 @@ func setInFile(path, key string, value any) error { content := string(data) re := regexp.MustCompile(`(?m)^\s*` + regexp.QuoteMeta(field) + `\s*=.*$`) + secRe := regexp.MustCompile(`(?m)^\[` + regexp.QuoteMeta(section) + `\][^\n]*$`) if re.MatchString(content) { content = re.ReplaceAllString(content, assignment) + } else if secRe.MatchString(content) { + content = secRe.ReplaceAllStringFunc(content, func(header string) string { + return header + "\n" + assignment + }) } else { content = strings.TrimRight(content, "\n") + "\n\n[" + section + "]\n" + assignment + "\n" } diff --git a/internal/config/config_test.go b/internal/config/config_test.go index 4fedc013..82c8e8d7 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -60,6 +60,36 @@ update_skipped_version = "v1.0.0" require.NoError(t, toml.Unmarshal(got, &parsed)) } +func TestSetInFileAddsSecondFieldToExistingSection(t *testing.T) { + path := filepath.Join(t.TempDir(), "config.toml") + original := `# Keep this +[[containers]] +type = "aws" + +[cli] +update_skipped_version = "v1.0.0" +` + require.NoError(t, os.WriteFile(path, []byte(original), 0644)) + + require.NoError(t, setInFile(path, "cli.notify_cadence", "minor")) + + got, err := os.ReadFile(path) + require.NoError(t, err) + result := string(got) + + var parsed struct { + CLI struct { + UpdateSkippedVersion string `toml:"update_skipped_version"` + NotifyCadence string `toml:"notify_cadence"` + } `toml:"cli"` + } + require.NoError(t, toml.Unmarshal(got, &parsed)) + assert.Equal(t, "v1.0.0", parsed.CLI.UpdateSkippedVersion) + assert.Equal(t, "minor", parsed.CLI.NotifyCadence) + + assert.Equal(t, 1, strings.Count(result, "[cli]"), "expected a single [cli] section header") +} + func TestSetInFilePreservesCommentsAndFormatting(t *testing.T) { path := filepath.Join(t.TempDir(), "config.toml") original := `# lstk configuration file