コード例 #1
0
ファイル: tags.go プロジェクト: Konubinix/tmsu
func listTagsForWorkingDirectory(store *storage.Storage, showCount, onePerLine bool) error {
	file, err := os.Open(".")
	if err != nil {
		return fmt.Errorf("could not open working directory: %v", err)
	}
	defer file.Close()

	dirNames, err := file.Readdirnames(0)
	if err != nil {
		return fmt.Errorf("could not list working directory contents: %v", err)
	}

	sort.Strings(dirNames)

	for _, dirName := range dirNames {
		log.Infof(2, "%v: retrieving tags.", dirName)

		file, err := store.FileByPath(dirName)
		if err != nil {
			log.Warn(err.Error())
			continue
		}
		if file == nil {
			continue
		}

		fileTags, err := store.FileTagsByFileId(file.Id)
		if err != nil {
			return fmt.Errorf("could not retrieve file-tags: %v", err)
		}

		tagNames, err := lookupTagNames(store, fileTags)
		if err != nil {
			return err
		}

		if showCount {
			fmt.Println(dirName + ": " + strconv.Itoa(len(tagNames)))
		} else {
			if onePerLine {
				fmt.Println(dirName)
				for _, tagName := range tagNames {
					fmt.Println(tagName)
				}
				fmt.Println()
			} else {
				fmt.Println(dirName + ": " + strings.Join(tagNames, " "))
			}
		}
	}

	return nil
}
コード例 #2
0
ファイル: cli.go プロジェクト: Konubinix/tmsu
func Run() {
	helpCommands = commands

	parser := NewOptionParser(globalOptions, commands)
	commandName, options, arguments, err := parser.Parse(os.Args[1:])
	if err != nil {
		log.Fatal(err)
	}

	switch {
	case options.HasOption("--version"):
		commandName = "version"
	case options.HasOption("--help"), commandName == "":
		commandName = "help"
	}

	log.Verbosity = options.Count("--verbose") + 1

	if dbOption := options.Get("--database"); dbOption != nil && dbOption.Argument != "" {
		database.Path = dbOption.Argument
	}

	command := commands[commandName]
	if command == nil {
		log.Fatalf("Invalid command '%v'.", commandName)
	}

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

		os.Exit(1)
	}
}
コード例 #3
0
ファイル: tags.go プロジェクト: Konubinix/tmsu
func listTagsForPaths(store *storage.Storage, paths []string, showCount, onePerLine bool) error {
	wereErrors := false
	for _, path := range paths {
		log.Infof(2, "%v: retrieving tags.", path)

		file, err := store.FileByPath(path)
		if err != nil {
			log.Warn(err.Error())
			continue
		}

		var tagNames []string
		if file != nil {
			fileTags, err := store.FileTagsByFileId(file.Id)
			if err != nil {
				return err
			}

			tagNames, err = lookupTagNames(store, fileTags)
			if err != nil {
				return err
			}
		} else {
			_, err := os.Stat(path)
			if err != nil {
				switch {
				case os.IsPermission(err):
					log.Warnf("%v: permission denied", path)
					wereErrors = true
					continue
				case os.IsNotExist(err):
					log.Warnf("%v: no such file", path)
					wereErrors = true
					continue
				default:
					return fmt.Errorf("%v: could not stat file: %v", path, err)
				}
			}
		}

		if showCount {
			fmt.Println(path + ": " + strconv.Itoa(len(tagNames)))
		} else {
			if onePerLine {
				fmt.Println(path)
				for _, tagName := range tagNames {
					fmt.Println(tagName)
				}
				fmt.Println()
			} else {
				fmt.Println(path + ": " + strings.Join(tagNames, " "))
			}
		}
	}

	if wereErrors {
		return blankError
	}

	return nil
}