Skip to content

Commit

Permalink
Issue #6: read subcommands from standard input
Browse files Browse the repository at this point in the history
  • Loading branch information
oniony committed Dec 17, 2014
1 parent 7c69d8a commit df2c065
Show file tree
Hide file tree
Showing 38 changed files with 246 additions and 346 deletions.
100 changes: 83 additions & 17 deletions src/tmsu/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,20 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
package cli

import (
"bufio"
"io"
"os"
"strings"
"tmsu/common/log"
"tmsu/storage"
"tmsu/storage/database"
)

var globalOptions = Options{Option{"--verbose", "-v", "show verbose messages", false, ""},
Option{"--help", "-h", "show help and exit", false, ""},
Option{"--version", "-V", "show version information and exit", false, ""},
Option{"--database", "-D", "use the specified database", true, ""},
Option{"--color", "", "colorize the output (auto/always/never)", true, ""},
}

func Run() {
helpCommands = commands

parser := NewOptionParser(globalOptions, commands)
commandName, options, arguments, err := parser.Parse(os.Args[1:])
commandName, options, arguments, err := parser.Parse(os.Args[1:]...)
if err != nil {
log.Fatal(err)
}
Expand All @@ -52,22 +49,91 @@ func Run() {
database.Path = dbOption.Argument
}

store, err := storage.Open()
if err != nil {
log.Fatalf("could not open storage: %v", err)
}

if err := store.Begin(); err != nil {
log.Fatalf("could not begin transaction: %v", err)
}

if commandName == "-" {
err = readCommandsFromStdin(store)
} else {
err = processCommand(store, commandName, options, arguments)
}

store.Commit()
store.Close()

if err != nil {
if err != errBlank {
log.Warn(err.Error())
}

os.Exit(1)
}
}

// unexported

var globalOptions = Options{Option{"--verbose", "-v", "show verbose messages", false, ""},
Option{"--help", "-h", "show help and exit", false, ""},
Option{"--version", "-V", "show version information and exit", false, ""},
Option{"--database", "-D", "use the specified database", true, ""},
Option{"--color", "", "colorize the output (auto/always/never)", true, ""},
}

func readCommandsFromStdin(store *storage.Storage) error {
reader := bufio.NewReader(os.Stdin)

wereErrors := false
for {
line, _, err := reader.ReadLine()
if err != nil {
if err == io.EOF {
if wereErrors {
return errBlank
} else {
return nil
}
}

log.Fatal(err)
}

parser := NewOptionParser(globalOptions, commands)
words := strings.SplitN(string(line), " ", -1)
commandName, options, arguments, err := parser.Parse(words...)
if err != nil {
log.Fatal(err)
}

if err := processCommand(store, commandName, options, arguments); err != nil {
if err != nil {
if err == errBlank {
wereErrors = true
} else {
return err
}
}
}
}
}

func processCommand(store *storage.Storage, commandName string, options Options, arguments []string) error {
command := findCommand(commands, commandName)
if command == nil {
log.Fatalf("invalid command '%v'.", commandName)
}

err = command.Exec(options, arguments)
if err != nil {
if err != errBlank {
log.Warn(err.Error())
}

os.Exit(1)
if err := command.Exec(store, options, arguments); err != nil {
return err
}
}

// unexported
return nil
}

func findCommand(commands map[string]*Command, commandName string) *Command {
command := commands[commandName]
Expand Down
25 changes: 16 additions & 9 deletions src/tmsu/cli/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.

package cli

import (
"tmsu/storage"
)

type Command struct {
Name string
Aliases []string
Expand All @@ -25,25 +29,28 @@ type Command struct {
Description string
Examples []string
Options Options
Exec func(options Options, args []string) error
Exec func(*storage.Storage, Options, []string) error
Hidden bool
}

var commands = map[string]*Command{
"copy": &CopyCommand,
"delete": &DeleteCommand,
"dupes": &DupesCommand,
"files": &FilesCommand,
"help": &HelpCommand,
"imply": &ImplyCommand,
"merge": &MergeCommand,
"copy": &CopyCommand,
"delete": &DeleteCommand,
"dupes": &DupesCommand,
"files": &FilesCommand,
"help": &HelpCommand,
"imply": &ImplyCommand,
"merge": &MergeCommand,
"mount": &MountCommand,
"rename": &RenameCommand,
"repair": &RepairCommand,
"stats": &StatsCommand,
"status": &StatusCommand,
"tag": &TagCommand,
"tags": &TagsCommand,
"unmount": &UnmountCommand,
"untag": &UntagCommand,
"untagged": &UntaggedCommand,
"values": &ValuesCommand,
"version": &VersionCommand}
"version": &VersionCommand,
"vfs": &VfsCommand}
6 changes: 3 additions & 3 deletions src/tmsu/cli/commands_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
package cli

func init() {
commands["mount"] = &MountCommand
commands["unmount"] = &UnmountCommand
commands["vfs"] = &VfsCommand
delete(commands, "mount")
delete(commands, "unmount")
delete(commands, "vfs")
}
13 changes: 1 addition & 12 deletions src/tmsu/cli/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,7 @@ var CopyCommand = Command{
Exec: copyExec,
}

func copyExec(options Options, args []string) error {
store, err := storage.Open()
if err != nil {
return fmt.Errorf("could not open storage: %v", err)
}
defer store.Close()

if err := store.Begin(); err != nil {
return fmt.Errorf("could not begin transaction: %v", err)
}
defer store.Commit()

func copyExec(store *storage.Storage, options Options, args []string) error {
sourceTagName := args[0]
destTagNames := args[1:]

Expand Down
4 changes: 2 additions & 2 deletions src/tmsu/cli/copy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func TestCopySuccessful(test *testing.T) {

// test

if err := CopyCommand.Exec(Options{}, []string{"source", "dest"}); err != nil {
if err := CopyCommand.Exec(store, Options{}, []string{"source", "dest"}); err != nil {
test.Fatal(err)
}

Expand All @@ -88,7 +88,7 @@ func TestCopyNonExistentSourceTag(test *testing.T) {

// test

err := CopyCommand.Exec(Options{}, []string{"source", "dest"})
err := CopyCommand.Exec(store, Options{}, []string{"source", "dest"})

// validate

Expand Down
13 changes: 1 addition & 12 deletions src/tmsu/cli/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,11 @@ var DeleteCommand = Command{
Exec: deleteExec,
}

func deleteExec(options Options, args []string) error {
func deleteExec(store *storage.Storage, options Options, args []string) error {
if len(args) == 0 {
return fmt.Errorf("no tags to delete specified")
}

store, err := storage.Open()
if err != nil {
return fmt.Errorf("could not open storage: %v", err)
}
defer store.Close()

if err := store.Begin(); err != nil {
return fmt.Errorf("could not begin transaction: %v", err)
}
defer store.Commit()

wereErrors := false
for _, tagName := range args {
tag, err := store.TagByName(tagName)
Expand Down
6 changes: 3 additions & 3 deletions src/tmsu/cli/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestDeleteUnappliedTag(test *testing.T) {

// test

if err := DeleteCommand.Exec(Options{}, []string{"beetroot"}); err != nil {
if err := DeleteCommand.Exec(store, Options{}, []string{"beetroot"}); err != nil {
test.Fatal(err)
}

Expand Down Expand Up @@ -113,7 +113,7 @@ func TestDeleteAppliedTag(test *testing.T) {

// test

if err := DeleteCommand.Exec(Options{}, []string{"deathrow"}); err != nil {
if err := DeleteCommand.Exec(store, Options{}, []string{"deathrow"}); err != nil {
test.Fatal(err)
}

Expand Down Expand Up @@ -166,7 +166,7 @@ func TestDeleteNonExistentTag(test *testing.T) {

// test

err := DeleteCommand.Exec(Options{}, []string{"deleteme"})
err := DeleteCommand.Exec(store, Options{}, []string{"deleteme"})

// validate

Expand Down
16 changes: 5 additions & 11 deletions src/tmsu/cli/dupes.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,26 +40,20 @@ var DupesCommand = Command{
Exec: dupesExec,
}

func dupesExec(options Options, args []string) error {
func dupesExec(store *storage.Storage, options Options, args []string) error {
recursive := options.HasOption("--recursive")

switch len(args) {
case 0:
findDuplicatesInDb()
findDuplicatesInDb(store)
default:
return findDuplicatesOf(args, recursive)
return findDuplicatesOf(store, args, recursive)
}

return nil
}

func findDuplicatesInDb() error {
store, err := storage.Open()
if err != nil {
return fmt.Errorf("could not open storage: %v", err)
}
defer store.Close()

func findDuplicatesInDb(store *storage.Storage) error {
log.Info(2, "identifying duplicate files.")

fileSets, err := store.DuplicateFiles()
Expand All @@ -85,7 +79,7 @@ func findDuplicatesInDb() error {
return nil
}

func findDuplicatesOf(paths []string, recursive bool) error {
func findDuplicatesOf(store *storage.Storage, paths []string, recursive bool) error {
store, err := storage.Open()
if err != nil {
return fmt.Errorf("could not open storage: %v", err)
Expand Down
12 changes: 6 additions & 6 deletions src/tmsu/cli/dupes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func TestDupesSingle(test *testing.T) {

// test

if err := DupesCommand.Exec(Options{}, []string{}); err != nil {
if err := DupesCommand.Exec(store, Options{}, []string{}); err != nil {
test.Fatal(err)
}

Expand Down Expand Up @@ -110,7 +110,7 @@ func TestDupesMultiple(test *testing.T) {

// test

if err := DupesCommand.Exec(Options{}, []string{}); err != nil {
if err := DupesCommand.Exec(store, Options{}, []string{}); err != nil {
test.Fatal(err)
}

Expand Down Expand Up @@ -162,7 +162,7 @@ func TestDupesNone(test *testing.T) {

// test

if err := DupesCommand.Exec(Options{}, []string{}); err != nil {
if err := DupesCommand.Exec(store, Options{}, []string{}); err != nil {
test.Fatal(err)
}

Expand Down Expand Up @@ -221,7 +221,7 @@ func TestDupesSingleUntaggedFile(test *testing.T) {

// test

if err := DupesCommand.Exec(Options{}, []string{path}); err != nil {
if err := DupesCommand.Exec(store, Options{}, []string{path}); err != nil {
test.Fatal(err)
}

Expand Down Expand Up @@ -280,7 +280,7 @@ func TestDupesMultipleUntaggedFile(test *testing.T) {

// test

if err := DupesCommand.Exec(Options{}, []string{path}); err != nil {
if err := DupesCommand.Exec(store, Options{}, []string{path}); err != nil {
test.Fatal(err)
}

Expand Down Expand Up @@ -339,7 +339,7 @@ func TestDupesNoneUntaggedFile(test *testing.T) {

// test

if err := DupesCommand.Exec(Options{}, []string{path}); err != nil {
if err := DupesCommand.Exec(store, Options{}, []string{path}); err != nil {
test.Fatal(err)
}

Expand Down

0 comments on commit df2c065

Please sign in to comment.