| Giorgi Lekveishvili | 7fb28bf | 2023-06-24 19:51:16 +0400 | [diff] [blame] | 1 | package installer |
| 2 | |
| 3 | import ( |
| 4 | "crypto/rand" |
| 5 | "fmt" |
| 6 | ) |
| 7 | |
| Giorgi Lekveishvili | 6e81318 | 2023-06-30 13:45:30 +0400 | [diff] [blame] | 8 | type SuffixGenerator interface { |
| 9 | Generate() (string, error) |
| 10 | } |
| 11 | |
| 12 | type emptySuffixGenerator struct { |
| 13 | } |
| 14 | |
| 15 | func NewEmptySuffixGenerator() SuffixGenerator { |
| 16 | return &emptySuffixGenerator{} |
| 17 | } |
| 18 | |
| 19 | func (g *emptySuffixGenerator) Generate() (string, error) { |
| 20 | return "", nil |
| 21 | } |
| 22 | |
| Giorgi Lekveishvili | a1e7790 | 2023-11-06 14:48:27 +0400 | [diff] [blame] | 23 | type suffixGenerator struct { |
| 24 | suffix string |
| 25 | } |
| 26 | |
| 27 | func NewSuffixGenerator(suffix string) SuffixGenerator { |
| 28 | return &suffixGenerator{suffix} |
| 29 | } |
| 30 | |
| 31 | func (g *suffixGenerator) Generate() (string, error) { |
| 32 | return g.suffix, nil |
| 33 | } |
| 34 | |
| Giorgi Lekveishvili | 6e81318 | 2023-06-30 13:45:30 +0400 | [diff] [blame] | 35 | type fixedLengthRandomSuffixGenerator struct { |
| 36 | len int |
| 37 | } |
| 38 | |
| 39 | func NewFixedLengthRandomSuffixGenerator(len int) SuffixGenerator { |
| 40 | return &fixedLengthRandomSuffixGenerator{len} |
| 41 | } |
| 42 | |
| 43 | var letters = []rune("abcdefghijklmnopqrstuvwxyz") |
| 44 | |
| 45 | func (g *fixedLengthRandomSuffixGenerator) Generate() (string, error) { |
| 46 | r := make([]byte, g.len) |
| 47 | if _, err := rand.Read(r); err != nil { |
| 48 | return "", err |
| 49 | } |
| 50 | ret := make([]rune, g.len) |
| 51 | for i, v := range r { |
| 52 | ret[i] += letters[v%26] |
| 53 | } |
| 54 | return fmt.Sprintf("-%s", string(ret)), nil |
| 55 | } |
| 56 | |
| Giorgi Lekveishvili | 7fb28bf | 2023-06-24 19:51:16 +0400 | [diff] [blame] | 57 | type NamespaceGenerator interface { |
| 58 | Generate(name string) (string, error) |
| 59 | } |
| 60 | |
| 61 | type prefixGenerator struct { |
| 62 | prefix string |
| 63 | } |
| 64 | |
| 65 | func NewPrefixGenerator(prefix string) NamespaceGenerator { |
| 66 | return &prefixGenerator{prefix} |
| 67 | } |
| 68 | |
| 69 | func (g *prefixGenerator) Generate(name string) (string, error) { |
| 70 | return g.prefix + name, nil |
| 71 | } |