| 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 | |
| 23 | type fixedLengthRandomSuffixGenerator struct { |
| 24 | len int |
| 25 | } |
| 26 | |
| 27 | func NewFixedLengthRandomSuffixGenerator(len int) SuffixGenerator { |
| 28 | return &fixedLengthRandomSuffixGenerator{len} |
| 29 | } |
| 30 | |
| 31 | var letters = []rune("abcdefghijklmnopqrstuvwxyz") |
| 32 | |
| 33 | func (g *fixedLengthRandomSuffixGenerator) Generate() (string, error) { |
| 34 | r := make([]byte, g.len) |
| 35 | if _, err := rand.Read(r); err != nil { |
| 36 | return "", err |
| 37 | } |
| 38 | ret := make([]rune, g.len) |
| 39 | for i, v := range r { |
| 40 | ret[i] += letters[v%26] |
| 41 | } |
| 42 | return fmt.Sprintf("-%s", string(ret)), nil |
| 43 | } |
| 44 | |
| Giorgi Lekveishvili | 7fb28bf | 2023-06-24 19:51:16 +0400 | [diff] [blame] | 45 | type NamespaceGenerator interface { |
| 46 | Generate(name string) (string, error) |
| 47 | } |
| 48 | |
| 49 | type prefixGenerator struct { |
| 50 | prefix string |
| 51 | } |
| 52 | |
| 53 | func NewPrefixGenerator(prefix string) NamespaceGenerator { |
| 54 | return &prefixGenerator{prefix} |
| 55 | } |
| 56 | |
| 57 | func (g *prefixGenerator) Generate(name string) (string, error) { |
| 58 | return g.prefix + name, nil |
| 59 | } |