示例#1
0
文件: values.go 项目: Konubinix/tmsu
func listValuesForTag(store *storage.Storage, tagName string, showCount, onePerLine bool) error {
	tag, err := store.TagByName(tagName)
	if err != nil {
		return fmt.Errorf("could not retrieve tag '%v': %v", tagName, err)
	}
	if tag == nil {
		return fmt.Errorf("no such tag, '%v'.", tagName)
	}

	log.Infof(2, "retrieving values for tag '%v'.", tagName)

	values, err := store.ValuesByTag(tag.Id)
	if err != nil {
		return fmt.Errorf("could not retrieve values for tag '%v': %v", tagName, err)
	}

	if showCount {
		fmt.Println(len(values))
	} else {
		if onePerLine {
			for _, value := range values {
				fmt.Println(value.Name)
			}
		} else {
			valueNames := make([]string, len(values))
			for index, value := range values {
				valueNames[index] = value.Name
			}

			format.Columns(valueNames, terminalWidth())
		}
	}

	return nil
}
示例#2
0
文件: files.go 项目: Konubinix/tmsu
func listFiles(files entities.Files, dirOnly, fileOnly, topOnly, leafOnly, recursive, print0, showCount, onePerLine bool) error {
	tree := path.NewTree()
	for _, file := range files {
		tree.Add(file.Path(), file.IsDir)
	}

	if topOnly {
		tree = tree.TopLevel()
	} else {
		if recursive {
			fsFiles, err := path.Enumerate(tree.TopLevel().Paths())
			if err != nil {
				return err
			}

			for _, fsFile := range fsFiles {
				tree.Add(fsFile.Path, fsFile.IsDir)
			}
		}
	}

	if leafOnly {
		tree = tree.Leaves()
	}

	if fileOnly {
		tree = tree.Files()
	}

	if dirOnly {
		tree = tree.Directories()
	}

	absPaths := tree.Paths()

	if showCount {
		fmt.Println(len(absPaths))
	} else {
		relPaths := make([]string, len(absPaths))
		for index, absPath := range absPaths {
			relPaths[index] = path.Rel(absPath)
		}
		sort.Strings(relPaths)

		if onePerLine || print0 {
			for _, relPath := range relPaths {
				if print0 {
					fmt.Printf("%v\000", relPath)
				} else {
					fmt.Println(relPath)
				}
			}
		} else {
			format.Columns(relPaths, terminalWidth())
		}
	}

	return nil
}
示例#3
0
文件: tags.go 项目: Konubinix/tmsu
func listTagsForPath(store *storage.Storage, path string, showCount, onePerLine bool) error {
	log.Infof(2, "%v: retrieving tags.", path)

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

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

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

	if showCount {
		fmt.Println(len(tagNames))
	} else {
		if onePerLine {
			for _, tagName := range tagNames {
				fmt.Println(tagName)
			}
		} else {
			format.Columns(tagNames, terminalWidth())
		}
	}

	return nil
}
示例#4
0
文件: tags.go 项目: Konubinix/tmsu
func listAllTags(showCount, onePerLine bool) error {
	store, err := storage.Open()
	if err != nil {
		return fmt.Errorf("could not open storage: %v", err)
	}
	defer store.Close()

	log.Info(2, "retrieving all tags.")

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

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

		if onePerLine {
			for _, tag := range tags {
				fmt.Println(tag.Name)
			}
		} else {
			tagNames := make([]string, len(tags))
			for index, tag := range tags {
				tagNames[index] = tag.Name
			}

			format.Columns(tagNames, terminalWidth())
		}
	}

	return nil
}
示例#5
0
文件: values.go 项目: Konubinix/tmsu
func listAllValues(showCount, onePerLine bool) error {
	store, err := storage.Open()
	if err != nil {
		return fmt.Errorf("could not open storage: %v", err)
	}
	defer store.Close()

	log.Info(2, "retrieving all values.")

	if showCount {
		count, err := store.ValueCount()
		if err != nil {
			return fmt.Errorf("could not retrieve value count: %v", err)
		}

		fmt.Println(count)
	} else {
		values, err := store.Values()
		if err != nil {
			return fmt.Errorf("could not retrieve values: %v", err)
		}

		if onePerLine {
			for _, value := range values {
				fmt.Println(value.Name)
			}
		} else {
			valueNames := make([]string, len(values))
			for index, value := range values {
				valueNames[index] = value.Name
			}

			format.Columns(valueNames, terminalWidth())
		}
	}

	return nil
}