CLI: Refactor "dry-run" and "yes" command flags to use helper functions

Signed-off-by: Michael Mayer <michael@photoprism.app>
This commit is contained in:
Michael Mayer
2025-10-09 15:41:35 +02:00
parent f125bfd638
commit 9d968ab357
8 changed files with 124 additions and 36 deletions

View File

@@ -24,9 +24,12 @@ var ClusterNodesModCommand = &cli.Command{
Name: "mod",
Usage: "Updates node properties",
ArgsUsage: "<id|name>",
Flags: []cli.Flag{nodesModRoleFlag, nodesModInternal, nodesModLabel, &cli.BoolFlag{Name: "yes", Aliases: []string{"y"}, Usage: "runs the command non-interactively"}},
Hidden: true, // Required for cluster-management only.
Action: clusterNodesModAction,
Flags: []cli.Flag{nodesModRoleFlag, nodesModInternal, nodesModLabel,
DryRunFlag("preview updates without modifying the registry"),
YesFlag(),
},
Hidden: true, // Required for cluster-management only.
Action: clusterNodesModAction,
}
func clusterNodesModAction(ctx *cli.Context) error {
@@ -62,11 +65,15 @@ func clusterNodesModAction(ctx *cli.Context) error {
return cli.Exit(fmt.Errorf("node not found"), 3)
}
changes := make([]string, 0, 4)
if v := ctx.String("role"); v != "" {
n.Role = clean.TypeLowerDash(v)
changes = append(changes, fmt.Sprintf("role=%s", clean.Log(n.Role)))
}
if v := ctx.String("advertise-url"); v != "" {
n.AdvertiseUrl = v
changes = append(changes, fmt.Sprintf("advertise-url=%s", clean.Log(n.AdvertiseUrl)))
}
if labels := ctx.StringSlice("label"); len(labels) > 0 {
if n.Labels == nil {
@@ -77,6 +84,16 @@ func clusterNodesModAction(ctx *cli.Context) error {
n.Labels[k] = v
}
}
changes = append(changes, fmt.Sprintf("labels+=%s", clean.Log(strings.Join(labels, ","))))
}
if ctx.Bool("dry-run") {
if len(changes) == 0 {
log.Infof("dry-run: no updates to apply for node %s", clean.LogQuote(n.Name))
} else {
log.Infof("dry-run: would update node %s (%s)", clean.LogQuote(n.Name), strings.Join(changes, ", "))
}
return nil
}
confirmed := RunNonInteractively(ctx.Bool("yes"))