Esempio n. 1
0
File: merge.go Progetto: logtcn/TMSU
func mergeValues(store *storage.Storage, tx *storage.Tx, sourceValueNames []string, destValueName string) (error, warnings) {
	destValue, err := store.ValueByName(tx, destValueName)
	if err != nil {
		return fmt.Errorf("could not retrieve value '%v': %v", destValueName, err), nil
	}
	if destValue == nil {
		return fmt.Errorf("no such value '%v'", destValueName), nil
	}

	warnings := make(warnings, 0, 10)

	for _, sourceValueName := range sourceValueNames {
		if sourceValueName == destValueName {
			warnings = append(warnings, fmt.Sprintf("cannot merge value '%v' into itself", sourceValueName))
			continue
		}

		sourceValue, err := store.ValueByName(tx, sourceValueName)
		if err != nil {
			return fmt.Errorf("could not retrieve value '%v': %v", sourceValueName, err), warnings
		}
		if sourceValue == nil {
			warnings = append(warnings, fmt.Sprintf("no such value '%v'", sourceValueName))
			continue
		}

		log.Infof(2, "finding files tagged with value '%v'.", sourceValueName)

		fileTags, err := store.FileTagsByValueId(tx, sourceValue.Id)
		if err != nil {
			return fmt.Errorf("could not retrieve files for value '%v': %v", sourceValueName, err), warnings
		}

		log.Infof(2, "applying value '%v' to these files.", destValueName)

		for _, fileTag := range fileTags {
			if _, err = store.AddFileTag(tx, fileTag.FileId, fileTag.TagId, destValue.Id); err != nil {
				return fmt.Errorf("could not apply value '%v' to file #%v: %v", destValueName, fileTag.FileId, err), warnings
			}
		}

		log.Infof(2, "deleting value '%v'.", sourceValueName)

		if err = store.DeleteValue(tx, sourceValue.Id); err != nil {
			return fmt.Errorf("could not delete value '%v': %v", sourceValueName, err), warnings
		}
	}

	return nil, warnings
}
Esempio n. 2
0
func deleteValue(store *storage.Storage, tx *storage.Tx, valueArgs []string) (error, warnings) {
	warnings := make(warnings, 0, 10)

	for _, valueArg := range valueArgs {
		valueName := parseTagOrValueName(valueArg)

		value, err := store.ValueByName(tx, valueName)
		if err != nil {
			return fmt.Errorf("could not retrieve value '%v': %v", valueName, err), warnings
		}
		if value == nil {
			warnings = append(warnings, fmt.Sprintf("no such value '%v'", valueName))
			continue
		}

		if err = store.DeleteValue(tx, value.Id); err != nil {
			return fmt.Errorf("could not delete value '%v': %v", valueName, err), warnings
		}
	}

	return nil, warnings
}