Example #1
0
func untagExec(options Options, args []string, databasePath string) (error, warnings) {
	if len(args) < 1 {
		return fmt.Errorf("too few arguments"), nil
	}

	recursive := options.HasOption("--recursive")
	followSymlinks := !options.HasOption("--no-dereference")

	store, err := openDatabase(databasePath)
	if err != nil {
		return err, nil
	}
	defer store.Close()

	tx, err := store.Begin()
	if err != nil {
		return err, nil
	}
	defer tx.Commit()

	if options.HasOption("--all") {
		if len(args) < 1 {
			return fmt.Errorf("files to untag must be specified"), nil
		}

		paths := args

		return untagPathsAll(store, tx, paths, recursive, followSymlinks)
	} else if options.HasOption("--tags") {
		tagArgs := text.Tokenize(options.Get("--tags").Argument)
		if len(tagArgs) == 0 {
			return fmt.Errorf("set of tags to apply must be specified"), nil
		}

		paths := args
		if len(paths) < 1 {
			return fmt.Errorf("at least one file to untag must be specified"), nil
		}

		return untagPaths(store, tx, paths, tagArgs, recursive, followSymlinks)
	} else {
		if len(args) < 2 {
			return fmt.Errorf("tags to remove and files to untag must be specified"), nil
		}

		paths := args[0:1]
		tagArgs := args[1:]

		return untagPaths(store, tx, paths, tagArgs, recursive, followSymlinks)
	}
}
Example #2
0
File: tag.go Project: logtcn/TMSU
func readStandardInput(store *storage.Storage, tx *storage.Tx, recursive, explicit, force bool) (error, warnings) {
	reader := bufio.NewReader(os.Stdin)

	warnings := make(warnings, 0, 10)

	for {
		line, err := reader.ReadString('\n')
		if err != nil {
			if err == io.EOF {
				break
			}

			return err, warnings
		}

		words := text.Tokenize(line[0 : len(line)-1])

		if len(words) < 2 {
			warnings = append(warnings, fmt.Sprintf("too few arguments"))
			continue
		}

		path := words[0]
		tagArgs := words[1:]

		err, commandWarnings := tagPaths(store, tx, tagArgs, []string{path}, explicit, recursive, force)
		if err != nil {
			warnings = append(warnings, err.Error())
		}
		if commandWarnings != nil {
			warnings = append(warnings, commandWarnings...)
		}
	}

	return nil, warnings
}
Example #3
0
File: tag.go Project: logtcn/TMSU
func tagExec(options Options, args []string, databasePath string) (error, warnings) {
	recursive := options.HasOption("--recursive")
	explicit := options.HasOption("--explicit")
	force := options.HasOption("--force")

	store, err := openDatabase(databasePath)
	if err != nil {
		return err, nil
	}
	defer store.Close()

	tx, err := store.Begin()
	if err != nil {
		return err, nil
	}
	defer tx.Commit()

	switch {
	case options.HasOption("--create"):
		if len(args) == 0 {
			return fmt.Errorf("too few arguments"), nil
		}

		return createTagsValues(store, tx, args)
	case options.HasOption("--tags"):
		if len(args) < 1 {
			return fmt.Errorf("too few arguments"), nil
		}

		tagArgs := text.Tokenize(options.Get("--tags").Argument)
		if len(tagArgs) == 0 {
			return fmt.Errorf("too few arguments"), nil
		}

		paths := args
		if len(paths) < 1 {
			return fmt.Errorf("too few arguments"), nil
		}

		return tagPaths(store, tx, tagArgs, paths, explicit, recursive, force)
	case options.HasOption("--from"):
		if len(args) < 1 {
			return fmt.Errorf("too few arguments"), nil
		}

		fromPath, err := filepath.Abs(options.Get("--from").Argument)
		if err != nil {
			return fmt.Errorf("%v: could not get absolute path: %v", fromPath, err), nil
		}

		paths := args

		return tagFrom(store, tx, fromPath, paths, explicit, recursive, force)
	case len(args) == 1 && args[0] == "-":
		return readStandardInput(store, tx, recursive, explicit, force)
	default:
		if len(args) < 2 {
			return fmt.Errorf("too few arguments"), nil
		}

		paths := args[0:1]
		tagArgs := args[1:]

		return tagPaths(store, tx, tagArgs, paths, explicit, recursive, force)
	}
}