| 1 | |
| 2 | |
| 3 | package vmname |
| 4 | |
| 5 | import ( |
| 6 | "crypto/rand" |
| 7 | "crypto/sha256" |
| 8 | "encoding/hex" |
| 9 | "errors" |
| 10 | "fmt" |
| 11 | "os" |
| 12 | "path/filepath" |
| 13 | "regexp" |
| 14 | "strings" |
| 15 | |
| 16 | "github.com/sylvinus/agent-vm/internal/paths" |
| 17 | ) |
| 18 | |
| 19 | |
| 20 | |
| 21 | const OldPrefix = "agent-vm-" |
| 22 | |
| 23 | |
| 24 | |
| 25 | const Template = "base" |
| 26 | |
| 27 | |
| 28 | var scratchRe = regexp.MustCompile(`^[A-Za-z0-9-]+-scratch-[0-9a-f]{8}$`) |
| 29 | |
| 30 | |
| 31 | |
| 32 | type Limit int |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | func Name(dir string) string { return Limit(0).Name(dir) } |
| 41 | |
| 42 | func (limit Limit) Name(dir string) string { |
| 43 | dir = paths.Guest(dir) |
| 44 | sum := sha256.Sum256([]byte(dir)) |
| 45 | b := base(dir, 0) |
| 46 | if b == "" { |
| 47 | b = "project" |
| 48 | } |
| 49 | return limit.fit(b, "-"+hex.EncodeToString(sum[:])[:8]) |
| 50 | } |
| 51 | |
| 52 | |
| 53 | func (limit Limit) fit(b, suffix string) string { |
| 54 | if limit == 0 { |
| 55 | limit = 76 |
| 56 | } |
| 57 | if n := int(limit) - len(suffix); len(b) > n { |
| 58 | b = strings.TrimRight(b[:max(n, 1)], "-") |
| 59 | if b == "" { |
| 60 | b = "p" |
| 61 | } |
| 62 | } |
| 63 | return b + suffix |
| 64 | } |
| 65 | |
| 66 | |
| 67 | |
| 68 | func FromOld(old string) string { return Limit(0).FromOld(old) } |
| 69 | |
| 70 | func (limit Limit) FromOld(old string) string { |
| 71 | if old == OldPrefix+"base" { |
| 72 | return Template |
| 73 | } |
| 74 | n := strings.TrimPrefix(old, OldPrefix) |
| 75 | if i := strings.LastIndexByte(n, '-'); i > 0 { |
| 76 | return limit.fit(n[:i], n[i:]) |
| 77 | } |
| 78 | return n |
| 79 | } |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | func Scratch(dir string) (string, error) { return Limit(0).Scratch(dir) } |
| 86 | |
| 87 | func (limit Limit) Scratch(dir string) (string, error) { |
| 88 | var b [4]byte |
| 89 | if _, err := rand.Read(b[:]); err != nil { |
| 90 | return "", fmt.Errorf("cannot read random bytes to name the VM: %w", err) |
| 91 | } |
| 92 | name := base(dir, 20) |
| 93 | if name == "" { |
| 94 | name = "project" |
| 95 | } |
| 96 | return limit.fit(name, "-scratch-"+hex.EncodeToString(b[:])), nil |
| 97 | } |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | func IsScratch(name string) bool { |
| 104 | return scratchRe.MatchString(name) |
| 105 | } |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | func base(dir string, max int) string { |
| 112 | b := basename(dir) + "\n" |
| 113 | var out []byte |
| 114 | for i := 0; i < len(b); i++ { |
| 115 | c := b[i] |
| 116 | if c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9' { |
| 117 | out = append(out, c) |
| 118 | } else if len(out) == 0 || out[len(out)-1] != '-' { |
| 119 | out = append(out, '-') |
| 120 | } |
| 121 | } |
| 122 | if max > 0 && len(out) > max { |
| 123 | out = out[:max] |
| 124 | } |
| 125 | s := string(out) |
| 126 | if max > 0 { |
| 127 | |
| 128 | return strings.Trim(s, "-") |
| 129 | } |
| 130 | s = strings.TrimPrefix(s, "-") |
| 131 | return strings.TrimSuffix(s, "-") |
| 132 | } |
| 133 | |
| 134 | |
| 135 | |
| 136 | func basename(p string) string { |
| 137 | if p == "" { |
| 138 | return "" |
| 139 | } |
| 140 | t := strings.TrimRight(p, "/") |
| 141 | if t == "" { |
| 142 | return "/" |
| 143 | } |
| 144 | return t[strings.LastIndexByte(t, '/')+1:] |
| 145 | } |
| 146 | |
| 147 | |
| 148 | var ErrNoDir = errors.New("no such directory") |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | func AbsDir(dir string) (string, error) { |
| 156 | if dir == "" || !filepath.IsAbs(dir) { |
| 157 | wd, err := os.Getwd() |
| 158 | if err != nil { |
| 159 | return "", err |
| 160 | } |
| 161 | dir = filepath.Join(wd, dir) |
| 162 | } |
| 163 | dir = filepath.Clean(dir) |
| 164 | if st, err := os.Stat(dir); err != nil || !st.IsDir() { |
| 165 | return "", fmt.Errorf("%w: %s", ErrNoDir, dir) |
| 166 | } |
| 167 | for _, r := range dir { |
| 168 | if r < 0x20 || r == 0x7f { |
| 169 | return "", errors.New("the directory name contains a control character") |
| 170 | } |
| 171 | } |
| 172 | return paths.Host(dir), nil |
| 173 | } |