diff --git a/cmd/cp.go b/cmd/cp.go index 77911ab..3ae3321 100644 --- a/cmd/cp.go +++ b/cmd/cp.go @@ -12,24 +12,31 @@ import ( func NewCpCommand() *cobra.Command { cmd := &cobra.Command{ - Use: "cp [source] [dest] [storage]", - Short: "Copy a file or directory and deduplicate it in storage", - Args: cobra.ExactArgs(3), + Use: "cp [source] [dest]", + Short: "Copy a file and deduplicate it in storage", + Args: cobra.ExactArgs(2), Run: cpCommand, } cmd.Flags().BoolP("with-metadata", "m", false, "Include file metadata in hash calculation") cmd.Flags().BoolP("verbose", "v", false, "Verbose output") - cmd.Flags().BoolP("append", "a", false, "Append directory contents to destination (same as dedup -d)") + cmd.Flags().BoolP("append", "a", false, "Append directory contents to destination") + cmd.Flags().String("storage", "", "Storage directory for deduplicated files") + cmd.Flags().Int("workers", 1, "Number of workers to use") return cmd } func cpCommand(cmd *cobra.Command, args []string) { - source, dest, storagePath := args[0], args[1], args[2] + source, dest := args[0], args[1] + storagePath, _ := cmd.Flags().GetString("storage") + if storagePath == "" { + storagePath = GetDefaultStoragePath() + } withMetadata, _ := cmd.Flags().GetBool("with-metadata") verbose, _ := cmd.Flags().GetBool("verbose") appendFlag, _ := cmd.Flags().GetBool("append") + workers, _ := cmd.Flags().GetInt("workers") // Create storage storageOpts := storage.StorageOptions{ @@ -47,7 +54,7 @@ func cpCommand(cmd *cobra.Command, args []string) { // Create processor based on the append flag var proc processor.Processor if appendFlag { - proc = processor.NewDedupProcessor(source, dest, s, h, 10) + proc = processor.NewDedupProcessor(source, dest, s, h, workers) } else { proc = processor.NewCpProcessor(source, dest, s, h) } diff --git a/cmd/dedup.go b/cmd/dedup.go index 4e6c6a6..245eee2 100644 --- a/cmd/dedup.go +++ b/cmd/dedup.go @@ -4,7 +4,6 @@ import ( "encoding/json" "log" "os" - "strconv" "github.com/mirkobrombin/dabadee/pkg/dabadee" "github.com/mirkobrombin/dabadee/pkg/hash" @@ -15,9 +14,9 @@ import ( func NewDedupCommand() *cobra.Command { cmd := &cobra.Command{ - Use: "dedup [source] [storage] [workers]", + Use: "dedup [source]", Short: "Deduplicate files in a directory", - Args: cobra.ExactArgs(3), + Args: cobra.ExactArgs(1), Run: dedupCommand, } @@ -25,20 +24,23 @@ func NewDedupCommand() *cobra.Command { cmd.Flags().BoolP("verbose", "v", false, "Verbose output") cmd.Flags().String("manifest-output", "", "Output manifest file to the given path") cmd.Flags().String("dest", "", "Destination directory for copying deduplicated files") + cmd.Flags().String("storage", "", "Storage directory for deduplicated files") + cmd.Flags().Int("workers", 1, "Number of workers to use") return cmd } func dedupCommand(cmd *cobra.Command, args []string) { - source, storagePath, workersStr := args[0], args[1], args[2] + source := args[0] + storagePath, _ := cmd.Flags().GetString("storage") + if storagePath == "" { + storagePath = GetDefaultStoragePath() + } withMetadata, _ := cmd.Flags().GetBool("with-metadata") verbose, _ := cmd.Flags().GetBool("verbose") outputManifest, _ := cmd.Flags().GetString("manifest-output") destDir, _ := cmd.Flags().GetString("dest") - workers, err := strconv.Atoi(workersStr) - if err != nil { - log.Fatalf("Invalid number of workers: %v", err) - } + workers, _ := cmd.Flags().GetInt("workers") // Create storage storageOpts := storage.StorageOptions{ diff --git a/cmd/utils.go b/cmd/utils.go new file mode 100644 index 0000000..f2176fa --- /dev/null +++ b/cmd/utils.go @@ -0,0 +1,23 @@ +package cmd + +import ( + "os/user" + "path/filepath" +) + +// GetDefaultStoragePath determines the default storage path based on user +// and execution context +func GetDefaultStoragePath() string { + currentUser, err := user.Current() + if err != nil { + return "/opt/.dabadee/Storage" + } + + if currentUser.Uid == "0" { + // Running as root + return "/opt/dabadee/Storage" + } + + // Running as non-root user + return filepath.Join(currentUser.HomeDir, ".dabadee/Storage") +}