Skip to content

Commit

Permalink
feat: verbose flag
Browse files Browse the repository at this point in the history
  • Loading branch information
mirkobrombin committed Feb 27, 2024
1 parent c5935d2 commit 37a182f
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 11 deletions.
4 changes: 3 additions & 1 deletion cmd/cp.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ func NewCpCommand() *cobra.Command {
}

cmd.Flags().BoolP("with-metadata", "m", false, "Include file metadata in hash calculation")
cmd.Flags().BoolP("verbose", "v", false, "Verbose output")

return cmd
}

func cpCommand(cmd *cobra.Command, args []string) {
source, dest, storagePath := args[0], args[1], args[2]
withMetadata, _ := cmd.Flags().GetBool("with-metadata")
verbose, _ := cmd.Flags().GetBool("verbose")

// Create storage
storageOpts := storage.StorageOptions{
Expand All @@ -45,7 +47,7 @@ func cpCommand(cmd *cobra.Command, args []string) {

// Run the processor
log.Printf("Copying %s to %s..", source, dest)
d := dabadee.NewDaBaDee(processor)
d := dabadee.NewDaBaDee(processor, verbose)
if err := d.Run(); err != nil {
log.Fatalf("Error during copy and link: %v", err)
}
Expand Down
4 changes: 3 additions & 1 deletion cmd/dedup.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ func NewDedupCommand() *cobra.Command {
}

cmd.Flags().BoolP("with-metadata", "m", false, "Include file metadata in hash calculation")
cmd.Flags().BoolP("verbose", "v", false, "Verbose output")

return cmd
}

func dedupCommand(cmd *cobra.Command, args []string) {
source, storagePath, workersStr := args[0], args[1], args[2]
withMetadata, _ := cmd.Flags().GetBool("with-metadata")
verbose, _ := cmd.Flags().GetBool("verbose")
workers, err := strconv.Atoi(workersStr)
if err != nil {
log.Fatalf("Invalid number of workers: %v", err)
Expand All @@ -50,7 +52,7 @@ func dedupCommand(cmd *cobra.Command, args []string) {

// Run the processor
log.Printf("Deduplicating %s..", source)
d := dabadee.NewDaBaDee(processor)
d := dabadee.NewDaBaDee(processor, verbose)
if err := d.Run(); err != nil {
log.Fatalf("Error during deduplication: %v", err)
}
Expand Down
10 changes: 7 additions & 3 deletions pkg/dabadee/dabadee.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@ import "github.com/mirkobrombin/dabadee/pkg/processor"

type DaBaDee struct {
Processor processor.Processor
Verbose bool
}

// NewDaBaDee creates a new DaBaDee orchestrator with the given processor
func NewDaBaDee(p processor.Processor) *DaBaDee {
return &DaBaDee{Processor: p}
func NewDaBaDee(p processor.Processor, verbose bool) *DaBaDee {
return &DaBaDee{
Processor: p,
Verbose: verbose,
}
}

// Run starts the given processor
func (d *DaBaDee) Run() error {
return d.Processor.Process()
return d.Processor.Process(d.Verbose)
}
2 changes: 1 addition & 1 deletion pkg/processor/cp.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func NewCpProcessor(sourceFile, destFile string, storage *storage.Storage, hashG
}

// Process processes the file and creates a link at the destination
func (p *CpProcessor) Process() (err error) {
func (p *CpProcessor) Process(verbose bool) (err error) {
// Compute file hash
var finalHash string

Expand Down
6 changes: 4 additions & 2 deletions pkg/processor/dedup.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func finishProcessing(hash string) {
}

// Process processes the files in the source directory
func (p *DedupProcessor) Process() error {
func (p *DedupProcessor) Process(verbose bool) error {
jobs := make(chan string, p.Workers)
var wg sync.WaitGroup

Expand All @@ -91,7 +91,9 @@ func (p *DedupProcessor) Process() error {
for path := range jobs {
err := p.processFile(path)
if err != nil {
log.Printf("Error processing file %s: %v", path, err)
if verbose {
log.Printf("Error processing file %s: %v", path, err)
}
}
}
}()
Expand Down
2 changes: 1 addition & 1 deletion pkg/processor/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ package processor
// Processor defines the core processing logic, implement this to add new
// functionalities that can be orchestrated by DaBaDee
type Processor interface {
Process() error
Process(verbose bool) error
}
2 changes: 1 addition & 1 deletion tests/cp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func TestCpCommand(t *testing.T) {
h,
)

d := dabadee.NewDaBaDee(cpProcessor)
d := dabadee.NewDaBaDee(cpProcessor, true)

// Run the command
err = d.Run()
Expand Down
2 changes: 1 addition & 1 deletion tests/dedup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestDedupCommand(t *testing.T) {

processor := processor.NewDedupProcessor(testPath, s, h, 1)

d := dabadee.NewDaBaDee(processor)
d := dabadee.NewDaBaDee(processor, true)

// Run the deduplication
err = d.Run()
Expand Down

0 comments on commit 37a182f

Please sign in to comment.