refactor: let updaters define the files they want to run on (#233)

This change reverses the responsibility for which files the updaters are
run on. Now each updater can specify the list of files and wether the
files should be created when they do not exist yet. This simplifies the
handling of each update in releaserpleaser.go, as we can just iterate
over all updaters and call it for each file of that updater.

Also update the flags to allow users to easily define which updaters
should run.
This commit is contained in:
Julian Tölle 2025-08-23 22:14:34 +02:00 committed by GitHub
parent 1e9e0aa5d9
commit f1aa1a2ef4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
20 changed files with 307 additions and 151 deletions

View file

@ -2,6 +2,7 @@ package cmd
import (
"fmt"
"slices"
"strings"
"github.com/spf13/cobra"
@ -21,12 +22,12 @@ var runCmd = &cobra.Command{
}
var (
flagForge string
flagBranch string
flagOwner string
flagRepo string
flagExtraFiles string
flagUpdatePackageJson bool
flagForge string
flagBranch string
flagOwner string
flagRepo string
flagExtraFiles string
flagUpdaters []string
)
func init() {
@ -36,7 +37,7 @@ func init() {
runCmd.PersistentFlags().StringVar(&flagOwner, "owner", "", "")
runCmd.PersistentFlags().StringVar(&flagRepo, "repo", "", "")
runCmd.PersistentFlags().StringVar(&flagExtraFiles, "extra-files", "", "")
runCmd.PersistentFlags().BoolVar(&flagUpdatePackageJson, "update-package-json", false, "")
runCmd.PersistentFlags().StringSliceVar(&flagUpdaters, "updaters", []string{}, "")
}
func run(cmd *cobra.Command, _ []string) error {
@ -49,7 +50,6 @@ func run(cmd *cobra.Command, _ []string) error {
"branch", flagBranch,
"owner", flagOwner,
"repo", flagRepo,
"update-package-json", flagUpdatePackageJson,
)
var f forge.Forge
@ -83,11 +83,19 @@ func run(cmd *cobra.Command, _ []string) error {
extraFiles := parseExtraFiles(flagExtraFiles)
updaters := []updater.NewUpdater{updater.Generic}
if flagUpdatePackageJson {
logger.DebugContext(ctx, "package.json updater enabled")
updaters = append(updaters, updater.PackageJson)
updaterNames := parseUpdaters(flagUpdaters)
updaters := []updater.Updater{}
for _, name := range updaterNames {
switch name {
case "generic":
updaters = append(updaters, updater.Generic(extraFiles))
case "changelog":
updaters = append(updaters, updater.Changelog())
case "packagejson":
updaters = append(updaters, updater.PackageJson())
default:
return fmt.Errorf("unknown updater: %s", name)
}
}
releaserPleaser := rp.New(
@ -122,3 +130,22 @@ func parseExtraFiles(input string) []string {
return extraFiles
}
func parseUpdaters(input []string) []string {
names := []string{"changelog", "generic"}
for _, u := range input {
if strings.HasPrefix(u, "-") {
name := u[1:]
names = slices.DeleteFunc(names, func(existingName string) bool { return existingName == name })
} else {
names = append(names, u)
}
}
// Make sure we only have unique updaters
slices.Sort(names)
names = slices.Compact(names)
return names
}

View file

@ -57,3 +57,43 @@ dir/Chart.yaml"`,
})
}
}
func Test_parseUpdaters(t *testing.T) {
tests := []struct {
name string
input []string
want []string
}{
{
name: "empty",
input: []string{},
want: []string{"changelog", "generic"},
},
{
name: "remove defaults",
input: []string{"-changelog", "-generic"},
want: []string{},
},
{
name: "remove unknown is ignored",
input: []string{"-fooo"},
want: []string{"changelog", "generic"},
},
{
name: "add new entry",
input: []string{"bar"},
want: []string{"bar", "changelog", "generic"},
},
{
name: "duplicates are removed",
input: []string{"bar", "bar", "changelog"},
want: []string{"bar", "changelog", "generic"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := parseUpdaters(tt.input)
assert.Equal(t, tt.want, got)
})
}
}