build out cli and add size and gather functionality
This commit is contained in:
69
cmd/cli/gather.go
Normal file
69
cmd/cli/gather.go
Normal file
@ -0,0 +1,69 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/dustin/go-humanize"
|
||||
|
||||
"forever-files/db"
|
||||
"forever-files/source"
|
||||
"forever-files/types"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
func gather() *cli.Command {
|
||||
return &cli.Command{
|
||||
Name: "gather",
|
||||
Aliases: []string{"g"},
|
||||
Usage: "Collects the files to be backed up and stores their info in the database",
|
||||
Flags: []cli.Flag{
|
||||
&cli.BoolFlag{
|
||||
Name: "reset",
|
||||
Usage: "Reset the database before gathering info",
|
||||
Aliases: []string{"r"},
|
||||
Action: func(c *cli.Context, reset bool) error {
|
||||
if reset {
|
||||
err := db.DeleteDB(types.AppName)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error deleting db: %w", err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
},
|
||||
},
|
||||
baseDirFlag(),
|
||||
},
|
||||
Action: func(c *cli.Context) error {
|
||||
|
||||
store, err := db.NewDB(types.AppName)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("error creating db: %w", err))
|
||||
}
|
||||
defer store.Close()
|
||||
|
||||
err = store.Migrate()
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("error migrating db: %w", err))
|
||||
}
|
||||
|
||||
baseDir := c.String("baseDir")
|
||||
|
||||
err = source.GatherInfo(baseDir, store)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error gathering info: %w", err)
|
||||
}
|
||||
|
||||
fileCount, err := store.GetFileCount()
|
||||
if err != nil {
|
||||
return fmt.Errorf("error getting file count: %w", err)
|
||||
}
|
||||
totalSize, err := store.GetTotalSize()
|
||||
if err != nil {
|
||||
return fmt.Errorf("error getting total size: %w", err)
|
||||
}
|
||||
fmt.Printf("Total Files: %v\n", fileCount)
|
||||
fmt.Printf("Total Size: %v\n", humanize.Bytes(uint64(totalSize)))
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user