Example #1
0
func (command TagsCommand) listAllTags() error {
	store, err := storage.Open()
	if err != nil {
		return fmt.Errorf("could not open storage: %v", err)
	}
	defer store.Close()

	if command.verbose {
		log.Info("retrieving all tags.")
	}

	if command.count {
		count, err := store.TagCount()
		if err != nil {
			return fmt.Errorf("could not retrieve tag count: %v", err)
		}

		log.Print(count)
	} else {
		tags, err := store.Tags()
		if err != nil {
			return fmt.Errorf("could not retrieve tags: %v", err)
		}

		for _, tag := range tags {
			log.Print(tag.Name)
		}
	}

	return nil
}
Example #2
0
func (command TagsCommand) listTagsForPath(store *storage.Storage, path string) error {
	if command.verbose {
		log.Infof("%v: retrieving tags.", path)
	}

	var tags, err = store.TagsForPath(path)
	if err != nil {
		return fmt.Errorf("%v: could not retrieve tags: %v", path, err)
	}

	if len(tags) == 0 {
		_, err := os.Stat(path)
		if err != nil {
			switch {
			case os.IsPermission(err):
				log.Warnf("%v: permission denied", path)
			case os.IsNotExist(err):
				return fmt.Errorf("%v: file not found", path)
			default:
				return fmt.Errorf("%v: could not stat file: %v", path, err)
			}
		}
	}

	if command.count {
		log.Print(len(tags))
	} else {
		for _, tag := range tags {
			log.Print(tag.Name)
		}
	}

	return nil
}
Example #3
0
func (command DupesCommand) findDuplicatesOf(paths []string) error {
	store, err := storage.Open()
	if err != nil {
		return fmt.Errorf("could not open storage: %v", err)
	}
	defer store.Close()

	first := true
	for _, path := range paths {
		if command.verbose {
			log.Infof("%v: identifying duplicate files.\n", path)
		}

		fp, err := fingerprint.Create(path)
		if err != nil {
			return fmt.Errorf("%v: could not create fingerprint: %v", path, err)
		}

		if fp == fingerprint.Fingerprint("") {
			return nil
		}

		files, err := store.FilesByFingerprint(fp)
		if err != nil {
			return fmt.Errorf("%v: could not retrieve files matching fingerprint '%v': %v", path, fp, err)
		}

		absPath, err := filepath.Abs(path)
		if err != nil {
			return fmt.Errorf("%v: could not determine absolute path: %v", path, err)
		}

		// filter out the file we're searching on
		dupes := files.Where(func(file *database.File) bool { return file.Path() != absPath })

		if len(paths) > 1 && len(dupes) > 0 {
			if first {
				first = false
			} else {
				log.Print()
			}

			log.Printf("%v duplicates of %v:", len(dupes), path)

			for _, dupe := range dupes {
				relPath := _path.Rel(dupe.Path())
				log.Printf("  %v", relPath)
			}
		} else {
			for _, dupe := range dupes {
				relPath := _path.Rel(dupe.Path())
				log.Print(relPath)
			}
		}
	}

	return nil
}
Example #4
0
func (VersionCommand) Exec(options cli.Options, args []string) error {
	log.Print("TMSU", common.Version)
	log.Print()
	log.Print(`Copyright © 2011-2013 Paul Ruane.

This program comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it under certain conditions.
See the accompanying COPYING file for further details.`)

	return nil
}
Example #5
0
func (command *FilesCommand) listFiles(files database.Files) error {
	tree := path.NewTree()
	for _, file := range files {
		tree.Add(file.Path(), file.IsDir)
	}

	if command.top {
		tree = tree.TopLevel()
	} else {
		if command.recursive {
			fsFiles, err := command.filesystemFiles(tree.TopLevel().Paths())
			if err != nil {
				return err
			}

			for _, fsFile := range fsFiles {
				tree.Add(fsFile.path, fsFile.isDir)
			}
		}
	}

	if command.leaf {
		tree = tree.Leaves()
	}

	if command.file {
		tree = tree.Files()
	}

	if command.directory {
		tree = tree.Directories()
	}

	if command.count {
		log.Print(len(tree.Paths()))
	} else {
		for _, absPath := range tree.Paths() {
			relPath := path.Rel(absPath)

			if command.print0 {
				log.Print0(relPath)
			} else {
				log.Print(relPath)
			}
		}
	}

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

	if command.verbose {
		log.Info("identifying duplicate files.")
	}

	fileSets, err := store.DuplicateFiles()
	if err != nil {
		return fmt.Errorf("could not identify duplicate files: %v", err)
	}

	if command.verbose {
		log.Infof("found %v sets of duplicate files.", len(fileSets))
	}

	for index, fileSet := range fileSets {
		if index > 0 {
			log.Print()
		}

		log.Printf("Set of %v duplicates:", len(fileSet))

		for _, file := range fileSet {
			relPath := _path.Rel(file.Path())
			log.Printf("  %v", relPath)
		}
	}

	return nil
}
Example #7
0
func (command TagsCommand) listTagsForPaths(store *storage.Storage, paths []string) error {
	for _, path := range paths {
		if command.verbose {
			log.Infof("%v: retrieving tags.", path)
		}

		var tags, err = store.TagsForPath(path)
		if err != nil {
			log.Warn(err.Error())
			continue
		}

		if command.count {
			log.Print(path + ": " + strconv.Itoa(len(tags)))
		} else {
			log.Print(path + ": " + tagLine(tags))
		}
	}

	return nil
}
Example #8
0
func (command TagsCommand) listTagsForWorkingDirectory(store *storage.Storage) 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 {
		if command.verbose {
			log.Infof("%v: retrieving tags.", dirName)
		}

		var tags, err = store.TagsForPath(dirName)

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

		if len(tags) == 0 {
			continue
		}

		if command.count {
			log.Print(dirName + ": " + strconv.Itoa(len(tags)))
		} else {
			log.Print(dirName + ": " + tagLine(tags))
		}
	}

	return nil
}