Skip to content

Commit

Permalink
feat: implement global storage path configuration and update command …
Browse files Browse the repository at this point in the history
…syntax

- Implemented global configuration for default storage path if not defined:
  - Uses `/home/.dabadee/Storage` if launched in a sub-directory of a non-root user
  - Uses `/opt/.dabadee/Storage` if launched as root from any other path
- Updated command syntax for `dedup` and `cp` commands to use `--storage` and `--workers` flags
  • Loading branch information
mirkobrombin committed Jul 8, 2024
1 parent 7578df5 commit e4aeadb
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 14 deletions.
19 changes: 13 additions & 6 deletions cmd/cp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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)
}
Expand Down
18 changes: 10 additions & 8 deletions cmd/dedup.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"log"
"os"
"strconv"

"github.com/mirkobrombin/dabadee/pkg/dabadee"
"github.com/mirkobrombin/dabadee/pkg/hash"
Expand All @@ -15,30 +14,33 @@ 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,
}

cmd.Flags().BoolP("with-metadata", "m", false, "Include file metadata in hash calculation")
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{
Expand Down
23 changes: 23 additions & 0 deletions cmd/utils.go
Original file line number Diff line number Diff line change
@@ -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")
}

0 comments on commit e4aeadb

Please sign in to comment.