Files
slacker-bot/commands.go
T
2026-11-01 11:47:41 -05:00

146 lines
3.4 KiB
Go

package main
import (
"errors"
"fmt"
"math"
"regexp"
"strconv"
"strings"
"time"
)
var errNotCommand = errors.New("not a command")
var unitSpace = regexp.MustCompile(`(\d)\s+(tib|tb|t|gib|gb|g|mib|mb|m|cpus|cpu|cores|core)\b`)
var sizeToken = regexp.MustCompile(`^([+-]?)(\d+(?:\.\d+)?)(g|gb|gib|m|mb|mib|cpu|cpus|core|cores)$`)
func parseScale(args string, cur Size) (Size, error) {
args = unitSpace.ReplaceAllString(strings.ToLower(args), "$1$2")
words := strings.Fields(args)
if len(words) == 0 {
return Size{cur.MemoryMB * 2, cur.CPUs * 2}, nil
}
if len(words) == 1 {
switch words[0] {
case "up", "out":
return Size{cur.MemoryMB * 2, cur.CPUs * 2}, nil
case "down", "in":
return Size{cur.MemoryMB / 2, cur.CPUs / 2}, nil
}
}
next := cur
var memDone, cpuDone bool
for i, w := range words {
parts := sizeToken.FindStringSubmatch(w)
if parts == nil {
if i == 0 {
return cur, errNotCommand
}
return cur, fmt.Errorf("I didn't understand `%s`", w)
}
sign, num, unit := parts[1], parts[2], parts[3]
n, _ := strconv.ParseFloat(num, 64)
if unit == "cpu" || unit == "cpus" || unit == "core" || unit == "cores" {
if cpuDone {
return cur, errors.New("CPU was given twice")
}
cpuDone = true
next.CPUs = apply(sign, cur.CPUs, n)
continue
}
if memDone {
return cur, errors.New("memory was given twice")
}
memDone = true
mb := n
if strings.HasPrefix(unit, "g") {
mb = n * 1024
}
next.MemoryMB = int(math.Round(apply(sign, float64(cur.MemoryMB), mb)))
}
return next, nil
}
func apply(sign string, cur, n float64) float64 {
switch sign {
case "+":
return cur + n
case "-":
return cur - n
}
return n
}
type keepSetting struct {
show bool
reset bool
indefinite bool
limit time.Duration
}
var dayToken = regexp.MustCompile(`(\d+(?:\.\d+)?)([dw])`)
var wordUnit = regexp.MustCompile(`(\d+(?:\.\d+)?)\s*(weeks?|wks?|days?|hours?|hrs?|minutes?|mins?)\b`)
func parseKeep(args string) (keepSetting, error) {
a := strings.ToLower(strings.TrimSpace(args))
switch a {
case "":
return keepSetting{show: true}, nil
case "default", "reset", "normal":
return keepSetting{reset: true}, nil
case "indefinite", "indefinitely", "forever", "alive", "running", "on":
return keepSetting{indefinite: true}, nil
}
d, err := parseDuration(a)
if err != nil {
return keepSetting{}, errNotCommand
}
if d <= 0 {
return keepSetting{}, errors.New("the time has to be more than zero")
}
return keepSetting{limit: d}, nil
}
func parseDuration(s string) (time.Duration, error) {
s = wordUnit.ReplaceAllStringFunc(s, func(m string) string {
p := wordUnit.FindStringSubmatch(m)
return p[1] + map[byte]string{'w': "w", 'd': "d", 'h': "h", 'm': "m"}[p[2][0]]
})
s = strings.ReplaceAll(s, " ", "")
s = dayToken.ReplaceAllStringFunc(s, func(m string) string {
p := dayToken.FindStringSubmatch(m)
n, _ := strconv.ParseFloat(p[1], 64)
hours := n * 24
if p[2] == "w" {
hours = n * 24 * 7
}
return strconv.FormatFloat(hours, 'f', -1, 64) + "h"
})
return time.ParseDuration(s)
}
func human(d time.Duration) string {
d = d.Round(time.Minute)
days := d / (24 * time.Hour)
d -= days * 24 * time.Hour
out := ""
if days > 0 {
out = fmt.Sprintf("%dd", days)
}
if d > 0 {
r := strings.TrimSuffix(d.String(), "0s")
if strings.HasSuffix(r, "h0m") {
r = strings.TrimSuffix(r, "0m")
}
out += r
}
if out == "" {
out = "0m"
}
return out
}