-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Provide better logs and ask user input when needed
- Loading branch information
1 parent
5c6209c
commit f2b39ac
Showing
7 changed files
with
274 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package cmd | ||
|
||
import ( | ||
"log" | ||
|
||
"github.com/mirkobrombin/dabadee/pkg/dabadee" | ||
"github.com/mirkobrombin/dabadee/pkg/hash" | ||
"github.com/mirkobrombin/dabadee/pkg/processor" | ||
"github.com/mirkobrombin/dabadee/pkg/storage" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func NewCpCommand() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "cp [source] [dest] [storage]", | ||
Short: "Copy a file and deduplicate it in storage", | ||
Args: cobra.ExactArgs(3), | ||
Run: cpCommand, | ||
} | ||
|
||
cmd.Flags().BoolP("with-metadata", "m", false, "Include file metadata in hash calculation") | ||
|
||
return cmd | ||
} | ||
|
||
func cpCommand(cmd *cobra.Command, args []string) { | ||
source, dest, storagePath := args[0], args[1], args[2] | ||
withMetadata, _ := cmd.Flags().GetBool("with-metadata") | ||
|
||
// Create storage | ||
storageOpts := storage.StorageOptions{ | ||
Root: storagePath, | ||
WithMetadata: withMetadata, | ||
} | ||
s, err := storage.NewStorage(storageOpts) | ||
if err != nil { | ||
log.Fatalf("Error creating storage: %v", err) | ||
} | ||
|
||
// Create hash generator | ||
h := hash.NewSHA256Generator() | ||
|
||
// Create processor | ||
processor := processor.NewCpProcessor(source, dest, s, h, withMetadata) | ||
|
||
// Run the processor | ||
log.Printf("Copying %s to %s..", source, dest) | ||
d := dabadee.NewDaBaDee(processor) | ||
if err := d.Run(); err != nil { | ||
log.Fatalf("Error during copy and link: %v", err) | ||
} | ||
|
||
log.Print("Done") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package cmd | ||
|
||
import ( | ||
"log" | ||
"strconv" | ||
|
||
"github.com/mirkobrombin/dabadee/pkg/dabadee" | ||
"github.com/mirkobrombin/dabadee/pkg/hash" | ||
"github.com/mirkobrombin/dabadee/pkg/processor" | ||
"github.com/mirkobrombin/dabadee/pkg/storage" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func NewDedupCommand() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "dedup [source] [storage] [workers]", | ||
Short: "Deduplicate files in a directory", | ||
Args: cobra.ExactArgs(3), | ||
Run: dedupCommand, | ||
} | ||
|
||
cmd.Flags().BoolP("with-metadata", "m", false, "Include file metadata in hash calculation") | ||
|
||
return cmd | ||
} | ||
|
||
func dedupCommand(cmd *cobra.Command, args []string) { | ||
source, storagePath, workersStr := args[0], args[1], args[2] | ||
withMetadata, _ := cmd.Flags().GetBool("with-metadata") | ||
workers, err := strconv.Atoi(workersStr) | ||
if err != nil { | ||
log.Fatalf("Invalid number of workers: %v", err) | ||
} | ||
|
||
// Create storage | ||
storageOpts := storage.StorageOptions{ | ||
Root: storagePath, | ||
WithMetadata: withMetadata, | ||
} | ||
s, err := storage.NewStorage(storageOpts) | ||
if err != nil { | ||
log.Fatalf("Error creating storage: %v", err) | ||
} | ||
|
||
// Create hash generator | ||
h := hash.NewSHA256Generator() | ||
|
||
// Create processor | ||
processor := processor.NewDedupProcessor(source, s, h, workers, withMetadata) | ||
|
||
// Run the processor | ||
log.Printf("Deduplicating %s..", source) | ||
d := dabadee.NewDaBaDee(processor) | ||
if err := d.Run(); err != nil { | ||
log.Fatalf("Error during deduplication: %v", err) | ||
} | ||
|
||
log.Print("Done") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"github.com/mirkobrombin/dabadee/pkg/storage" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func NewFindLinksCommand() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "find-links <source> <storage>", | ||
Short: "Find all hard links to the specified file", | ||
Args: cobra.ExactArgs(2), | ||
Run: findLinksCommand, | ||
} | ||
|
||
cmd.Flags().StringSliceP("additional-paths", "p", []string{}, "Additional paths to search for links") | ||
|
||
return cmd | ||
} | ||
|
||
func findLinksCommand(cmd *cobra.Command, args []string) { | ||
path, storagePath := args[0], args[1] | ||
additionalPaths, _ := cmd.Flags().GetStringSlice("additional-paths") | ||
|
||
// Create storage | ||
s, err := storage.NewStorage(storage.StorageOptions{Root: storagePath}) | ||
if err != nil { | ||
log.Fatalf("Error creating storage: %v", err) | ||
} | ||
|
||
// Find links | ||
log.Printf("Finding links to %s..", path) | ||
links, err := s.FindLinks(path, additionalPaths) | ||
if err != nil { | ||
log.Fatalf("Error finding links: %v", err) | ||
} | ||
|
||
// Print links | ||
for _, link := range links { | ||
fmt.Printf("- %s\n", link) | ||
} | ||
|
||
log.Print("Done") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"strings" | ||
|
||
"github.com/mirkobrombin/dabadee/pkg/storage" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func NewRmOrphansCommand() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "rm-orphans <storage>", | ||
Short: "Remove all orphaned files from the storage", | ||
Args: cobra.ExactArgs(1), | ||
Run: rmOrphansCommand, | ||
} | ||
|
||
cmd.Flags().BoolP("yes", "y", false, "Assume yes; do not prompt") | ||
|
||
return cmd | ||
} | ||
|
||
func rmOrphansCommand(cmd *cobra.Command, args []string) { | ||
storagePath := args[0] | ||
assumeYes, _ := cmd.Flags().GetBool("yes") | ||
|
||
// Create storage | ||
s, err := storage.NewStorage(storage.StorageOptions{Root: storagePath}) | ||
if err != nil { | ||
log.Fatalf("Error creating storage: %v", err) | ||
} | ||
|
||
// Prompt | ||
if !assumeYes { | ||
fmt.Print("This will remove all orphaned files from the storage. Continue? [y/N] ") | ||
var response string | ||
fmt.Scanln(&response) | ||
if strings.ToLower(response) != "y" { | ||
log.Print("Aborting") | ||
return | ||
} | ||
} | ||
|
||
// Remove orphans | ||
log.Print("Removing orphans..") | ||
err = s.RemoveOrphans() | ||
if err != nil { | ||
log.Fatalf("Error removing orphans: %v", err) | ||
} | ||
|
||
log.Print("Done") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"strings" | ||
|
||
"github.com/mirkobrombin/dabadee/pkg/storage" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func NewRmCommand() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "rm <source> <storage-path>", | ||
Short: "Remove a file from the storage", | ||
Args: cobra.ExactArgs(2), | ||
Run: rmCommand, | ||
} | ||
|
||
cmd.Flags().BoolP("yes", "y", false, "Assume yes; do not prompt") | ||
|
||
return cmd | ||
} | ||
|
||
func rmCommand(cmd *cobra.Command, args []string) { | ||
source, storagePath := args[0], args[1] | ||
assumeYes, _ := cmd.Flags().GetBool("yes") | ||
|
||
// Create storage | ||
s, err := storage.NewStorage(storage.StorageOptions{Root: storagePath}) | ||
if err != nil { | ||
log.Fatalf("Error creating storage: %v", err) | ||
} | ||
|
||
// Prompt | ||
if !assumeYes { | ||
fmt.Printf("This will remove %s and its link from the storage. Continue? [y/N] ", source) | ||
var response string | ||
fmt.Scanln(&response) | ||
if strings.ToLower(response) != "y" { | ||
log.Print("Aborting") | ||
return | ||
} | ||
} | ||
|
||
// Remove file | ||
err = s.RemoveFile(source) | ||
if err != nil { | ||
log.Fatalf("Error removing file: %v", err) | ||
} | ||
|
||
log.Print("Done") | ||
} |
Oops, something went wrong.