Skip to content

Commit

Permalink
Add a command to reset the uploaded files tracker
Browse files Browse the repository at this point in the history
Signed-off-by: pacoorozco <[email protected]>
  • Loading branch information
pacoorozco committed Oct 21, 2023
1 parent 7cef355 commit 36792de
Show file tree
Hide file tree
Showing 9 changed files with 156 additions and 7 deletions.
2 changes: 2 additions & 0 deletions internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ type FileTracker interface {
IsUploaded(file string) bool
UnmarkAsUploaded(file string) error
Close() error

Destroy() error
}

// TokenManager represents a service to keep and read secrets (like passwords, tokens...)
Expand Down
2 changes: 2 additions & 0 deletions internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/gphotosuploader/gphotos-uploader-cli/internal/cli/flags"
"github.com/gphotosuploader/gphotos-uploader-cli/internal/cli/list"
"github.com/gphotosuploader/gphotos-uploader-cli/internal/cli/push"
"github.com/gphotosuploader/gphotos-uploader-cli/internal/cli/reset"
"github.com/gphotosuploader/gphotos-uploader-cli/internal/cli/version"
"github.com/gphotosuploader/gphotos-uploader-cli/internal/log"
"github.com/mgutz/ansi"
Expand Down Expand Up @@ -64,6 +65,7 @@ func createCliCommandTree(cmd *cobra.Command) {
cmd.AddCommand(push.NewCommand(globalFlags))
cmd.AddCommand(auth.NewCommand(globalFlags))
cmd.AddCommand(list.NewCommand(globalFlags))
cmd.AddCommand(reset.NewCommand(globalFlags))

// TODO: Set flags here instead of passing globalFlags to all commands.
// See: https://github.com/arduino/arduino-cli/blob/master/internal/cli/cli.go
Expand Down
17 changes: 17 additions & 0 deletions internal/cli/reset/reset.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package reset

import (
"github.com/gphotosuploader/gphotos-uploader-cli/internal/cli/flags"
"github.com/spf13/cobra"
)

func NewCommand(globalFlags *flags.GlobalFlags) *cobra.Command {
resetCommand := &cobra.Command{
Use: "reset",
Short: "Reset internal databases",
}

resetCommand.AddCommand(initFileTrackerCommand(globalFlags))

return resetCommand
}
71 changes: 71 additions & 0 deletions internal/cli/reset/reset_filetracker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package reset

import (
"context"
"github.com/gphotosuploader/gphotos-uploader-cli/internal/app"
"github.com/gphotosuploader/gphotos-uploader-cli/internal/cli/flags"
"github.com/gphotosuploader/gphotos-uploader-cli/internal/feedback"
"github.com/spf13/cobra"
)

// FileTrackerCommandOptions contains the input to the 'reset file-tracker' command.
type FileTrackerCommandOptions struct {
*flags.GlobalFlags

Force bool
}

func initFileTrackerCommand(globalFlags *flags.GlobalFlags) *cobra.Command {
o := &FileTrackerCommandOptions{
GlobalFlags: globalFlags,

Force: false,
}

command := &cobra.Command{
Use: "file-tracker",
Short: "Reset the already uploaded files database",
Long: `Reset the internal database which keep track of the already uploaded files.`,
Args: cobra.NoArgs,
RunE: o.Run,
}

command.Flags().BoolVar(&o.Force, "force", false, "Force the deletion without asking.")

return command
}

func (o *FileTrackerCommandOptions) Run(cobraCmd *cobra.Command, args []string) error {
ctx := context.Background()
cli, err := app.Start(ctx, o.CfgDir)
if err != nil {
return err
}
defer func() {
_ = cli.Stop()
}()

cli.Logger.Debug("Removing the File Tracker database...")

// If the force flag is not user, ask for user confirmation
if !o.Force {
userConfirmation, err := feedback.YesNoPrompt("Do you want to reset the already uploaded file tracker?", false)
if err != nil {
return err
}

if !userConfirmation {
cobraCmd.Println("User aborted the removal of the File Tracker")
return nil
}
}

err = cli.FileTracker.Destroy()
if err != nil {
return err
}

cobraCmd.Println("File Tracker was reset")

return nil
}
6 changes: 6 additions & 0 deletions internal/datastore/filetracker/filetracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type FileRepository interface {
Put(key string, item TrackedFile) error
Delete(key string) error
Close() error
Destroy() error
}

// New returns a FileTracker using specified repo.
Expand Down Expand Up @@ -110,3 +111,8 @@ func (ft FileTracker) UnmarkAsUploaded(file string) error {
func (ft FileTracker) Close() error {
return ft.repo.Close()
}

// Destroy completely remove an existing FileTracker database.
func (ft FileTracker) Destroy() error {
return ft.repo.Destroy()

Check warning on line 117 in internal/datastore/filetracker/filetracker.go

View check run for this annotation

Codecov / codecov/patch

internal/datastore/filetracker/filetracker.go#L116-L117

Added lines #L116 - L117 were not covered by tests
}
4 changes: 4 additions & 0 deletions internal/datastore/filetracker/filetracker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ func (m mockedRepository) Close() error {
return nil
}

func (m mockedRepository) Destroy() error {
return nil
}

type mockedHasher struct {
hash string
}
Expand Down
19 changes: 14 additions & 5 deletions internal/datastore/filetracker/leveldb_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package filetracker
import (
"github.com/syndtr/goleveldb/leveldb"
"github.com/syndtr/goleveldb/leveldb/opt"
"os"
)

// DB represents a LevelDB database.
Expand All @@ -15,19 +16,21 @@ type DB interface {

// LevelDBRepository implements a FileRepository using LevelDB.
type LevelDBRepository struct {
DB DB
DB DB
path string
}

// NewLevelDBRepository creates a repository using LevelDB package.
func NewLevelDBRepository(filename string) (*LevelDBRepository, error) {
ft, err := leveldb.OpenFile(filename, nil)
func NewLevelDBRepository(path string) (*LevelDBRepository, error) {
ft, err := leveldb.OpenFile(path, nil)

Check warning on line 25 in internal/datastore/filetracker/leveldb_repository.go

View check run for this annotation

Codecov / codecov/patch

internal/datastore/filetracker/leveldb_repository.go#L24-L25

Added lines #L24 - L25 were not covered by tests
return &LevelDBRepository{
DB: ft,
DB: ft,
path: path,

Check warning on line 28 in internal/datastore/filetracker/leveldb_repository.go

View check run for this annotation

Codecov / codecov/patch

internal/datastore/filetracker/leveldb_repository.go#L27-L28

Added lines #L27 - L28 were not covered by tests
}, err
}

// Get returns the item specified by key. It returns ErrItemNotFound if the
// DB does not contains the key.
// DB does not contain the key.
func (r LevelDBRepository) Get(key string) (TrackedFile, bool) {
val, err := r.DB.Get([]byte(key), nil)
if err != nil {
Expand All @@ -50,3 +53,9 @@ func (r LevelDBRepository) Delete(key string) error {
func (r LevelDBRepository) Close() error {
return r.DB.Close()
}

// Destroy completely remove an existing LevelDB database directory.
func (r LevelDBRepository) Destroy() error {
_ = r.DB.Close()
return os.RemoveAll(r.path)

Check warning on line 60 in internal/datastore/filetracker/leveldb_repository.go

View check run for this annotation

Codecov / codecov/patch

internal/datastore/filetracker/leveldb_repository.go#L58-L60

Added lines #L58 - L60 were not covered by tests
}
15 changes: 13 additions & 2 deletions internal/datastore/upload_tracker/leveldb.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package upload_tracker

import (
"github.com/syndtr/goleveldb/leveldb"
"os"
)

type LevelDBStore struct {
db *leveldb.DB
db *leveldb.DB
path string
}

// NewStore create a new Store implemented by LevelDB
Expand All @@ -16,7 +18,10 @@ func NewStore(path string) (*LevelDBStore, error) {
return nil, err
}

s := &LevelDBStore{db: db}
s := &LevelDBStore{
db: db,
path: path,
}
return s, err
}

Expand All @@ -42,3 +47,9 @@ func (s *LevelDBStore) Delete(key string) {
func (s *LevelDBStore) Close() {
_ = s.db.Close()
}

// Destroy completely remove an existing LevelDB database directory.
func (s *LevelDBStore) Destroy() error {
_ = s.db.Close()
return os.RemoveAll(s.path)

Check warning on line 54 in internal/datastore/upload_tracker/leveldb.go

View check run for this annotation

Codecov / codecov/patch

internal/datastore/upload_tracker/leveldb.go#L52-L54

Added lines #L52 - L54 were not covered by tests
}
27 changes: 27 additions & 0 deletions internal/feedback/terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"golang.org/x/term"
"os"
"strings"
)

func isTerminal() bool {
Expand All @@ -32,3 +33,29 @@ func InputUserField(prompt string, secret bool) (string, error) {
sc.Scan()
return sc.Text(), sc.Err()
}

func YesNoPrompt(prompt string, def bool) (bool, error) {
choices := "Y/n"
if !def {
choices = "y/N"
}

Check warning on line 41 in internal/feedback/terminal.go

View check run for this annotation

Codecov / codecov/patch

internal/feedback/terminal.go#L37-L41

Added lines #L37 - L41 were not covered by tests

prompt = fmt.Sprintf("%s (%s)", prompt, choices)

for {
s, err := InputUserField(prompt, false)
if err != nil {
return def, err
}
if s == "" {
return def, nil
}
s = strings.ToLower(s)
if s == "y" || s == "yes" {
return true, nil
}
if s == "n" || s == "no" {
return false, nil
}

Check warning on line 59 in internal/feedback/terminal.go

View check run for this annotation

Codecov / codecov/patch

internal/feedback/terminal.go#L43-L59

Added lines #L43 - L59 were not covered by tests
}
}

0 comments on commit 36792de

Please sign in to comment.