Skip to content

Commit

Permalink
feat: Provide better logs and ask user input when needed
Browse files Browse the repository at this point in the history
  • Loading branch information
mirkobrombin committed Feb 26, 2024
1 parent 5c6209c commit f2b39ac
Show file tree
Hide file tree
Showing 7 changed files with 274 additions and 145 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ was listening to when I started the project. So... I'm blue, da ba dee da ba daa

- [x] Add tests
- [ ] Add a progress bar
- [ ] Provide better logs and ask user input when needed
- [x] Provide better logs and ask user input when needed
- [ ] Make access to storage more robust, with a lock file
- [x] Add a way to remove files from the storage reflecting the changes in the
original files
Expand Down
54 changes: 54 additions & 0 deletions cmd/cp.go
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")
}
59 changes: 59 additions & 0 deletions cmd/dedup.go
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")
}
47 changes: 47 additions & 0 deletions cmd/find-links.go
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")
}
54 changes: 54 additions & 0 deletions cmd/rm-orphans.go
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")
}
53 changes: 53 additions & 0 deletions cmd/rm.go
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")
}
Loading

0 comments on commit f2b39ac

Please sign in to comment.