From 5b28656625cbbe15f9f9d373f4a066751b6e7437 Mon Sep 17 00:00:00 2001 From: Joseda Rios Date: Wed, 26 Jun 2019 00:59:24 +0200 Subject: [PATCH] Add terminal interface to the 'del' option --- config.go | 24 +++++++++++++++--------- dotfiles/.rubbi.sh | 5 +---- main.go | 12 +++++++----- 3 files changed, 23 insertions(+), 18 deletions(-) diff --git a/config.go b/config.go index 11d4c23..25aa556 100644 --- a/config.go +++ b/config.go @@ -195,17 +195,10 @@ func (config *Config) Use(fn int) error { // RemoveFolder removes a folder from the configuration and the filesystem // If the folder is marked as latest, the last folder entry becomes latest. func (config *Config) RemoveFolder(fn int) error { - targetFolder := config.Folders[fn] - if err := os.RemoveAll(targetFolder.Path); err != nil { + if err := os.RemoveAll(config.Folders[fn].Path); err != nil { return errors.Errorf("failed to remove the folder: %+v", err) } - config.Folders = remove(config.Folders, fn) - sortFolders(config.Folders) - - // If we are removing the latest folder, point to the last folder in the list - if config.Latest.Path == targetFolder.Path { - config.updateLatest(config.Folders[len(config.Folders)-1]) - } + config.Flush() return nil } @@ -217,6 +210,19 @@ func (config *Config) Flush() (errs error) { folders = append(folders, fol) } } + sortFolders(folders) + + // If the latest folder was removed, point to the last folder in the list + var shouldUpdateLatest = true + for _, fol := range folders { + if fol.Path == config.Latest.Path { + shouldUpdateLatest = false + break + } + } + if shouldUpdateLatest { + config.updateLatest(folders[len(folders)-1]) + } config.Folders = folders return errors.Trace(errs) diff --git a/dotfiles/.rubbi.sh b/dotfiles/.rubbi.sh index ea33335..9de124b 100644 --- a/dotfiles/.rubbi.sh +++ b/dotfiles/.rubbi.sh @@ -11,14 +11,11 @@ alias rubbish='rbsh' alias rubcd='rbsh' function rubdel { - rubbi-sh -del "${1}" + rubbi-sh -del } function rubadd { rubbi-sh -add "${1}" } -function rubuse { - rubbi-sh -use "${1}" -} function rubsel { rubbi-sh -sel rubcd diff --git a/main.go b/main.go index aabaa7e..db1d731 100644 --- a/main.go +++ b/main.go @@ -4,7 +4,6 @@ import ( "flag" "fmt" "os" - "strconv" "time" "github.com/juju/errors" @@ -17,10 +16,10 @@ var ( clean = flag.Bool("clean", false, "if true, the rubbish folder is removed") sel = flag.Bool("sel", false, "if true, prompts the folders list and outputs the choosen one") + del = flag.Bool("del", false, "if true, prompts the folders list and removes the choosen one") show = flag.Bool("show", false, "if true, outputs the current rubbish folders") ver = flag.Bool("ver", false, "if true, the rubbish version will be shown") add = flag.String("add", "", "folder name to add") - del = flag.String("del", "", "folder number to delete") root = flag.String("root", "/tmp", "temporary location for the rubbish folder") ) @@ -98,10 +97,13 @@ func run() error { } } - if *del != "" { - fn, err := strconv.Atoi(*del) + if *del { + if len(config.Folders) <= 1 { + return errors.Errorf("unable to remove folders when there is only one folder or none.") + } + fn, err := SelectFolder(config) if err != nil { - return errors.Errorf("failed to parse folder number to delete: %+v", err) + return err } if err := config.RemoveFolder(fn); err != nil { return err