Skip to content

Commit

Permalink
Allow deleting without going through trash. Made deletion of multiple…
Browse files Browse the repository at this point in the history
… files slightly faster.
  • Loading branch information
Laurent Cozic committed Apr 29, 2014
1 parent 559bba6 commit d1862bf
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 13 deletions.
2 changes: 1 addition & 1 deletion install/install.osx.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env bash
if [ -f "massren.osx.tar.gz" ]; then mv -f "massren.osx.tar.gz" "massren.osx.tar.gz.old" ; fi
wget "https://github.com/laurent22/massren/releases/download/v1.2.0/massren.osx.tar.gz"
wget "https://github.com/laurent22/massren/releases/download/v1.2.1/massren.osx.tar.gz"
tar xvzf massren.osx.tar.gz
chmod 755 massren
mv massren /usr/bin
75 changes: 64 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"runtime"
"sort"
"strings"
"sync"
"time"

"github.com/jessevdk/go-flags"
Expand All @@ -33,7 +34,7 @@ const (
type CommandLineOptions struct {
DryRun bool `short:"n" long:"dry-run" description:"Don't rename anything but show the operation that would have been performed."`
Verbose bool `short:"v" long:"verbose" description:"Enable verbose output."`
Config bool `short:"c" long:"config" description:"Set a configuration value. eg. massren --config <name> [value]"`
Config bool `short:"c" long:"config" description:"Set or list a configuration values. For more info: massren --config --help"`
Undo bool `short:"u" long:"undo" description:"Undo a rename operation. Currently delete operations cannot be undone (though files can be recovered from the trash in OSX and Windows). eg. massren --undo [path]"`
Version bool `short:"V" long:"version" description:"Displays version information."`
}
Expand Down Expand Up @@ -253,10 +254,13 @@ func filePathsFromListFile(filePath string) ([]string, error) {
return filePathsFromString(string(contentB)), nil
}

func printHelp() {
flagParser_.WriteHelp(os.Stdout)

examples := `
func printHelp(subMenu string) {
var info string

if subMenu == "" {
flagParser_.WriteHelp(os.Stdout)

info = `
Examples:
Process all the files in the current directory:
Expand All @@ -274,7 +278,35 @@ Examples:
List config values:
% APPNAME --config
`
fmt.Println(strings.Replace(examples, "APPNAME", APPNAME, -1))
} else if subMenu == "config" {
info = `
Config commands:
Set a value:
% APPNAME --config <name> <value>
List all the values:
% APPNAME --config
Delete a value:
% APPNAME --config <name>
Possible key/values:
editor: The editor to use when editing the list of files. Default: auto-detected.
use_trash: Whether files should be moved to the trash/recycle bin after deletion. Possible values: 0 or 1. Default: 1.
Examples:
Set Sublime as the default text editor:
% APPNAME --config editor "subl -n -w"
Don't move files to trash:
% APPNAME --config use_trash 0
`
}

fmt.Println(strings.Replace(info, "APPNAME", APPNAME, -1))
}

func fileActions(originalFilePaths []string, changedContent string) ([]*FileAction, error) {
Expand Down Expand Up @@ -362,6 +394,10 @@ func processFileActions(fileActions []*FileAction, dryRun bool) error {
logError("Could not save history items: %s", err)
}
}()

var deleteWaitGroup sync.WaitGroup
var deleteChannel = make(chan int, 100)
useTrash := config_.Bool("use_trash")

for _, action := range fileActions {
switch action.kind {
Expand All @@ -386,10 +422,21 @@ func processFileActions(fileActions []*FileAction, dryRun bool) error {
logInfo("\"%s\" => <Deleted>", filePath)
} else {
logDebug("\"%s\" => <Deleted>", filePath)
_, err := trash.MoveToTrash(filePath)
if err != nil {
return err
}
deleteWaitGroup.Add(1)
go func(filePath string, deleteChannel chan int, useTrash bool) {
var err error
deleteChannel <- 1
defer deleteWaitGroup.Done()
if useTrash {
_, err = trash.MoveToTrash(filePath)
} else {
err = os.RemoveAll(filePath)
}
if err != nil {
logError("%s", err)
}
<- deleteChannel
}(filePath, deleteChannel, useTrash)
}
break

Expand All @@ -402,6 +449,8 @@ func processFileActions(fileActions []*FileAction, dryRun bool) error {

doneActions = append(doneActions, action)
}

deleteWaitGroup.Wait()

return nil
}
Expand Down Expand Up @@ -440,7 +489,11 @@ func main() {
if err != nil {
t := err.(*flags.Error).Type
if t == flags.ErrHelp {
printHelp()
subMenu := ""
if opts.Config {
subMenu = "config"
}
printHelp(subMenu)
return
} else {
criticalError(err)
Expand Down
2 changes: 1 addition & 1 deletion version.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package main

import "fmt"

const VERSION = "1.2.0"
const VERSION = "1.2.1"

func handleVersionCommand(opts *CommandLineOptions, args []string) error {
fmt.Println(VERSION)
Expand Down

0 comments on commit d1862bf

Please sign in to comment.