summaryrefslogtreecommitdiff
path: root/imagestore/namepathmapper.go
blob: 919007b63f5398648782b54607f23062d83ee3a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
}