From 01e9a34952bd6ddd383680b0ca2312e476ad07a6 Mon Sep 17 00:00:00 2001 From: horus_arch Date: Sun, 19 Apr 2015 22:09:52 +0200 Subject: Initial commit. --- imagestore/namepathmapper.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 imagestore/namepathmapper.go (limited to 'imagestore/namepathmapper.go') diff --git a/imagestore/namepathmapper.go b/imagestore/namepathmapper.go new file mode 100644 index 0000000..919007b --- /dev/null +++ b/imagestore/namepathmapper.go @@ -0,0 +1,34 @@ +package imagestore + +import ( + "regexp" + "strings" +) + +type NamePathMapper struct { + regex *regexp.Regexp + replace string +} + +func NewNamePathMapper(expr string, mapping string) *NamePathMapper { + var r *regexp.Regexp + if len(expr) > 0 { + r = regexp.MustCompile(expr) + } + + return &NamePathMapper{ + r, + mapping, + } +} + +func (this *NamePathMapper) mapToPath(obj *StoreObject) string { + repl := strings.Replace(this.replace, "${ImageName}", obj.Name, -1) + repl = strings.Replace(repl, "${ImageSize}", obj.Type, -1) + + if this.regex != nil { + return this.regex.ReplaceAllString(obj.Name, repl) + } + + return repl +} -- cgit v1.2.3