Skip to content

Commit

Permalink
Add terminal interface to the 'del' option
Browse files Browse the repository at this point in the history
  • Loading branch information
jotadrilo committed Jun 25, 2019
1 parent ece4629 commit 5b28656
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 18 deletions.
24 changes: 15 additions & 9 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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)
Expand Down
5 changes: 1 addition & 4 deletions dotfiles/.rubbi.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 7 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"flag"
"fmt"
"os"
"strconv"
"time"

"github.com/juju/errors"
Expand All @@ -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")
)

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 5b28656

Please sign in to comment.