Add extension normalization command
This commit is contained in:
92
cmd/extension.go
Normal file
92
cmd/extension.go
Normal file
@@ -0,0 +1,92 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/rogeecn/renamer/internal/extension"
|
||||
"github.com/rogeecn/renamer/internal/listing"
|
||||
)
|
||||
|
||||
// NewExtensionCommand constructs the extension CLI command; exported for testing.
|
||||
func NewExtensionCommand() *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: "extension <source-ext...> <target-ext>",
|
||||
Short: "Normalize multiple file extensions to a single target extension",
|
||||
Args: cobra.MinimumNArgs(2),
|
||||
SilenceUsage: true,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
scope, err := listing.ScopeFromCmd(cmd)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
req := extension.NewRequest(scope)
|
||||
|
||||
dryRun, err := getBool(cmd, "dry-run")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
autoApply, err := getBool(cmd, "yes")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if dryRun && autoApply {
|
||||
return errors.New("--dry-run cannot be combined with --yes; remove one of them")
|
||||
}
|
||||
req.SetExecutionMode(dryRun, autoApply)
|
||||
|
||||
parsed, err := extension.ParseArgs(args)
|
||||
if err != nil {
|
||||
return fmt.Errorf("invalid extension arguments: %w", err)
|
||||
}
|
||||
|
||||
req.SetExtensions(parsed.SourcesCanonical, parsed.SourcesDisplay, parsed.Target)
|
||||
req.SetWarnings(parsed.Duplicates, parsed.NoOps)
|
||||
|
||||
summary, planned, err := extension.Preview(cmd.Context(), req, cmd.OutOrStdout())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if summary.HasConflicts() {
|
||||
return errors.New("conflicts detected; resolve them before applying")
|
||||
}
|
||||
|
||||
if dryRun || !autoApply {
|
||||
if !autoApply {
|
||||
fmt.Fprintln(cmd.OutOrStdout(), "Preview complete. Re-run with --yes to apply.")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
if len(planned) == 0 {
|
||||
if summary.TotalCandidates == 0 {
|
||||
fmt.Fprintln(cmd.OutOrStdout(), "No candidates found.")
|
||||
} else {
|
||||
fmt.Fprintln(cmd.OutOrStdout(), "Nothing to apply; extensions already normalized.")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
entry, err := extension.Apply(cmd.Context(), req, planned, summary)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Fprintf(cmd.OutOrStdout(), "Applied %d extension updates. Ledger updated.\n", len(entry.Operations))
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
cmd.Example = ` renamer extension .jpeg .JPG .jpg --dry-run
|
||||
renamer extension .yaml .yml .yml --yes --recursive`
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(NewExtensionCommand())
|
||||
}
|
||||
@@ -47,6 +47,7 @@ func NewRootCommand() *cobra.Command {
|
||||
cmd.AddCommand(newListCommand())
|
||||
cmd.AddCommand(NewReplaceCommand())
|
||||
cmd.AddCommand(NewRemoveCommand())
|
||||
cmd.AddCommand(NewExtensionCommand())
|
||||
cmd.AddCommand(newUndoCommand())
|
||||
|
||||
return cmd
|
||||
|
||||
14
cmd/undo.go
14
cmd/undo.go
@@ -4,6 +4,7 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
@@ -25,7 +26,18 @@ func newUndoCommand() *cobra.Command {
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Fprintf(cmd.OutOrStdout(), "Undo applied: %d operations reversed\n", len(entry.Operations))
|
||||
out := cmd.OutOrStdout()
|
||||
fmt.Fprintf(out, "Undo applied: %d operations reversed\n", len(entry.Operations))
|
||||
|
||||
if entry.Command == "extension" && entry.Metadata != nil {
|
||||
if target, ok := entry.Metadata["targetExtension"].(string); ok && target != "" {
|
||||
fmt.Fprintf(out, "Restored extensions to %s\n", target)
|
||||
}
|
||||
if sources, ok := entry.Metadata["sourceExtensions"].([]string); ok && len(sources) > 0 {
|
||||
fmt.Fprintf(out, "Previous sources: %s\n", strings.Join(sources, ", "))
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user