-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutil.go
More file actions
62 lines (51 loc) · 1.42 KB
/
util.go
File metadata and controls
62 lines (51 loc) · 1.42 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package main
import (
"bytes"
"crypto/rand"
"encoding/base32"
"strings"
)
// OutputCollector wraps an io.Writer and limits the number of bytes written to it.
// Once the limit is reached, subsequent writes are discarded (no-op).
type OutputCollector struct {
Buffer bytes.Buffer
MaxRemaining int64 // max bytes remaining
}
func (oc *OutputCollector) Write(p []byte) (n int, err error) {
if oc.MaxRemaining <= 0 {
// Limit reached, discard but report success
return len(p), nil
}
if int64(len(p)) > oc.MaxRemaining {
// Partial write up to the limit
n, err = oc.Buffer.Write(p[:oc.MaxRemaining])
oc.MaxRemaining -= int64(n)
// Report full length as written
return len(p), err
}
n, err = oc.Buffer.Write(p)
oc.MaxRemaining -= int64(n)
return n, err
}
func sanitizeEnvVarName(s string) string {
var result strings.Builder
result.Grow(len(s))
for _, r := range s {
if (r >= 'A' && r <= 'Z') || (r >= 'a' && r <= 'z') || (r >= '0' && r <= '9') || r == '_' {
result.WriteRune(r)
} else {
result.WriteRune('_')
}
}
return strings.ToUpper(result.String())
}
type TaskID = string
var taskIDEncoding = base32.NewEncoding("abcdefghijklmnopqrstuvwxyz234567").WithPadding(base32.NoPadding)
func newTaskID() TaskID {
// Generate 10 random bytes (80 bits of entropy)
b := make([]byte, 10)
if _, err := rand.Read(b); err != nil {
panic(err) // unreachable
}
return taskIDEncoding.EncodeToString(b)
}