mirror of
https://github.com/rclone/rclone.git
synced 2025-07-23 23:05:54 +02:00
lib/transform adds the transform library, supporting advanced path name transformations for converting and renaming files and directories by applying prefixes, suffixes, and other alterations. It also adds the --name-transform flag for use with sync, copy, and move. Multiple transformations can be used in sequence, applied in the order they are specified on the command line. By default --name-transform will only apply to file names. The means only the leaf file name will be transformed. However some of the transforms would be better applied to the whole path or just directories. To choose which which part of the file path is affected some tags can be added to the --name-transform: file Only transform the leaf name of files (DEFAULT) dir Only transform name of directories - these may appear anywhere in the path all Transform the entire path for files and directories Example syntax: --name-transform file,prefix=ABC --name-transform dir,prefix=DEF
86 lines
1.9 KiB
Go
86 lines
1.9 KiB
Go
package bilib
|
|
|
|
import (
|
|
"bytes"
|
|
"os"
|
|
"sort"
|
|
"strconv"
|
|
)
|
|
|
|
// Names comprises a set of file names
|
|
type Names map[string]any
|
|
|
|
// ToNames converts string slice to a set of names
|
|
func ToNames(list []string) Names {
|
|
ns := Names{}
|
|
for _, f := range list {
|
|
ns.Add(f)
|
|
}
|
|
return ns
|
|
}
|
|
|
|
// Add adds new file name to the set
|
|
func (ns Names) Add(name string) {
|
|
ns[name] = nil
|
|
}
|
|
|
|
// Has checks whether given name is present in the set
|
|
func (ns Names) Has(name string) bool {
|
|
_, ok := ns[name]
|
|
return ok
|
|
}
|
|
|
|
// NotEmpty checks whether set is not empty
|
|
func (ns Names) NotEmpty() bool {
|
|
return len(ns) > 0
|
|
}
|
|
|
|
// ToList converts name set to string slice
|
|
func (ns Names) ToList() []string {
|
|
list := []string{}
|
|
for file := range ns {
|
|
list = append(list, file)
|
|
}
|
|
sort.Strings(list)
|
|
return list
|
|
}
|
|
|
|
// Save saves name set in a text file
|
|
func (ns Names) Save(path string) error {
|
|
return SaveList(ns.ToList(), path)
|
|
}
|
|
|
|
// SaveList saves file name list in a text file
|
|
func SaveList(list []string, path string) error {
|
|
buf := &bytes.Buffer{}
|
|
for _, s := range list {
|
|
_, _ = buf.WriteString(strconv.Quote(s))
|
|
_ = buf.WriteByte('\n')
|
|
}
|
|
return os.WriteFile(path, buf.Bytes(), PermSecure)
|
|
}
|
|
|
|
// AliasMap comprises a pair of names that are not equal but treated as equal for comparison purposes
|
|
// For example, when normalizing unicode and casing
|
|
// This helps reduce repeated normalization functions, which really slow things down
|
|
type AliasMap map[string]string
|
|
|
|
// Add adds new pair to the set, in both directions
|
|
func (am AliasMap) Add(name1, name2 string) {
|
|
if name1 != name2 {
|
|
am[name1] = name2
|
|
am[name2] = name1
|
|
}
|
|
}
|
|
|
|
// Alias returns the alternate version, if any, else the original.
|
|
func (am AliasMap) Alias(name1 string) string {
|
|
// note: we don't need to check normalization settings, because we already did it in March.
|
|
// the AliasMap will only exist if March paired up two unequal filenames.
|
|
name2, ok := am[name1]
|
|
if ok {
|
|
return name2
|
|
}
|
|
return name1
|
|
}
|