Refactor code style for improved readability and consistency.
This commit is contained in:
1 parent
463c013056
commit
94bfc87e9d
4 files changed
+118
-156
No files matched your search
+57
-73
@@ -19,9 +19,9 @@ import (
|
||||
// Ranges for the [sandbox] limits.
|
||||
const (
|
||||
minSandboxTimeout = 100 * time.Millisecond
|
||||
maxSandboxTimeout = 30 * time.Second
|
||||
minSessionTimeout = 10 * time.Second
|
||||
maxSessionTimeout = 30 * time.Minute
|
||||
maxSandboxTimeout = 30 * time.Second
|
||||
minSessionTimeout = 10 * time.Second
|
||||
maxSessionTimeout = 30 * time.Minute
|
||||
minMemoryMB = 4
|
||||
maxMemoryMB = 1024
|
||||
minCallStack = 50
|
||||
@@ -50,7 +50,7 @@ var goTypeNames = map[string]string{
|
||||
// Error is one problem in the config file.
|
||||
type Error struct {
|
||||
File string
|
||||
Line int // 1-based; 0 if unknown
|
||||
Line int // 1-based; 0 if unknown
|
||||
Col int
|
||||
Key string // e.g. "item[1].interval"; empty for file-level problems
|
||||
Msg string
|
||||
@@ -59,9 +59,7 @@ type Error struct {
|
||||
func (e Error) Error() string {
|
||||
var b strings.Builder
|
||||
b.WriteString(e.File)
|
||||
if e.Line > 0 {
|
||||
_, _ = fmt.Fprintf(&b, ":%d:%d", e.Line, e.Col)
|
||||
}
|
||||
if e.Line > 0 { _, _ = fmt.Fprintf(&b, ":%d:%d", e.Line, e.Col) }
|
||||
|
||||
b.WriteString(": ")
|
||||
if e.Key != "" {
|
||||
@@ -78,9 +76,7 @@ type Errors []Error
|
||||
|
||||
func (es Errors) Error() string {
|
||||
lines := make([]string, len(es))
|
||||
for i, e := range es {
|
||||
lines[i] = e.Error()
|
||||
}
|
||||
for i, e := range es { lines[i] = e.Error() }
|
||||
|
||||
return strings.Join(lines, "\n")
|
||||
}
|
||||
@@ -93,9 +89,7 @@ type validator struct {
|
||||
|
||||
func (v *validator) errorf(key, format string, args ...any) {
|
||||
e := Error{File: v.file, Key: key, Msg: fmt.Sprintf(format, args...)}
|
||||
if p, ok := v.pos.find(key); ok {
|
||||
e.Line, e.Col = p.Line, p.Column
|
||||
}
|
||||
if p, ok := v.pos.find(key); ok { e.Line, e.Col = p.Line, p.Column }
|
||||
v.errs = append(v.errs, e)
|
||||
}
|
||||
|
||||
@@ -119,17 +113,11 @@ func (v *validator) decodeError(de *toml.DecodeError, msg string) {
|
||||
// types, into TOML terms.
|
||||
func friendlyDecodeMsg(msg string) string {
|
||||
m := typeErrRe.FindStringSubmatch(msg)
|
||||
if m == nil {
|
||||
return msg
|
||||
}
|
||||
if m == nil { return msg }
|
||||
|
||||
want, ok := goTypeNames[m[2]]
|
||||
if !ok {
|
||||
if strings.HasPrefix(m[2], "map[") {
|
||||
want = "a table"
|
||||
} else {
|
||||
return msg
|
||||
}
|
||||
if strings.HasPrefix(m[2], "map[") { want = "a table" } else { return msg }
|
||||
}
|
||||
|
||||
return fmt.Sprintf("expected %s, got a TOML %s", want, strings.ToLower(m[1]))
|
||||
@@ -137,9 +125,7 @@ func friendlyDecodeMsg(msg string) string {
|
||||
|
||||
func (v *validator) validate(cfg *Config, opts Options) {
|
||||
shops := map[string]bool{}
|
||||
for _, b := range opts.Builtins {
|
||||
shops[b] = true
|
||||
}
|
||||
for _, b := range opts.Builtins { shops[b] = true }
|
||||
|
||||
v.plugins(cfg, shops)
|
||||
v.items(cfg, shops, opts.Builtins)
|
||||
@@ -180,10 +166,10 @@ func (v *validator) plugins(cfg *Config, shops map[string]bool) {
|
||||
|
||||
info, err := os.Stat(path)
|
||||
switch {
|
||||
case err != nil:
|
||||
v.errorf(key, "cannot read plugin: %v", err)
|
||||
case !info.Mode().IsRegular():
|
||||
v.errorf(key, "%s is not a regular file", path)
|
||||
case err != nil:
|
||||
v.errorf(key, "cannot read plugin: %v", err)
|
||||
case !info.Mode().IsRegular():
|
||||
v.errorf(key, "%s is not a regular file", path)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -195,27 +181,27 @@ func (v *validator) items(cfg *Config, shops map[string]bool, builtins []string)
|
||||
}
|
||||
|
||||
names := map[string]int{}
|
||||
urls := map[string]string{}
|
||||
urls := map[string]string{}
|
||||
|
||||
for i := range cfg.Items {
|
||||
it := &cfg.Items[i]
|
||||
it := &cfg.Items[i]
|
||||
key := fmt.Sprintf("item[%d]", i)
|
||||
|
||||
switch {
|
||||
case it.Name == "":
|
||||
v.errorf(key+".name", "missing required key")
|
||||
case names[it.Name] > 0:
|
||||
v.errorf(key+".name", "duplicate item name %q (also item[%d])", it.Name, names[it.Name]-1)
|
||||
default:
|
||||
names[it.Name] = i + 1
|
||||
case it.Name == "":
|
||||
v.errorf(key+".name", "missing required key")
|
||||
case names[it.Name] > 0:
|
||||
v.errorf(key+".name", "duplicate item name %q (also item[%d])", it.Name, names[it.Name]-1)
|
||||
default:
|
||||
names[it.Name] = i + 1
|
||||
}
|
||||
|
||||
switch {
|
||||
case it.Shop == "":
|
||||
v.errorf(key+".shop", "missing required key")
|
||||
case !shops[it.Shop]:
|
||||
v.errorf(key+".shop", "unknown shop %q: built-in plugins are %s, others must be listed under [plugins]",
|
||||
it.Shop, strings.Join(builtins, ", "))
|
||||
case it.Shop == "":
|
||||
v.errorf(key+".shop", "missing required key")
|
||||
case !shops[it.Shop]:
|
||||
v.errorf(key+".shop", "unknown shop %q: built-in plugins are %s, others must be listed under [plugins]",
|
||||
it.Shop, strings.Join(builtins, ", "))
|
||||
}
|
||||
|
||||
if len(it.URLs) == 0 {
|
||||
@@ -252,26 +238,26 @@ func (v *validator) items(cfg *Config, shops map[string]bool, builtins []string)
|
||||
|
||||
if it.FXMargin != 0 {
|
||||
switch {
|
||||
case it.FXMargin < 0 || it.FXMargin > 1:
|
||||
v.errorf(key+".fx_margin", "must be between 0 and 1, e.g. 0.1 for 10%% (got %v)", it.FXMargin)
|
||||
case it.Currency == "" || it.MaxPrice == nil:
|
||||
v.errorf(key+".fx_margin", "needs both currency and max_price to have an effect")
|
||||
case it.FXMargin < 0 || it.FXMargin > 1:
|
||||
v.errorf(key+".fx_margin", "must be between 0 and 1, e.g. 0.1 for 10%% (got %v)", it.FXMargin)
|
||||
case it.Currency == "" || it.MaxPrice == nil:
|
||||
v.errorf(key+".fx_margin", "needs both currency and max_price to have an effect")
|
||||
}
|
||||
}
|
||||
|
||||
switch it.Seller {
|
||||
case "":
|
||||
it.Seller = SellerFirstParty
|
||||
case SellerFirstParty, SellerAny:
|
||||
default:
|
||||
v.errorf(key+".seller", "must be %q or %q (got %q)", SellerFirstParty, SellerAny, it.Seller)
|
||||
case "":
|
||||
it.Seller = SellerFirstParty
|
||||
case SellerFirstParty, SellerAny:
|
||||
default:
|
||||
v.errorf(key+".seller", "must be %q or %q (got %q)", SellerFirstParty, SellerAny, it.Seller)
|
||||
}
|
||||
|
||||
switch {
|
||||
case !it.Interval.IsSet():
|
||||
it.Interval.Duration = DefaultInterval
|
||||
case it.Interval.Duration < MinInterval:
|
||||
v.errorf(key+".interval", "must be at least %s (got %s)", MinInterval, it.Interval)
|
||||
case !it.Interval.IsSet():
|
||||
it.Interval.Duration = DefaultInterval
|
||||
case it.Interval.Duration < MinInterval:
|
||||
v.errorf(key+".interval", "must be at least %s (got %s)", MinInterval, it.Interval)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -286,11 +272,11 @@ func (v *validator) accounts(cfg *Config, shops map[string]bool, lookupEnv func(
|
||||
}
|
||||
|
||||
switch acc.CartMode {
|
||||
case "":
|
||||
acc.CartMode = CartModeDeeplink
|
||||
case CartModeDeeplink, CartModeSession:
|
||||
default:
|
||||
v.errorf(key+".cart_mode", "must be %q or %q (got %q)", CartModeDeeplink, CartModeSession, acc.CartMode)
|
||||
case "":
|
||||
acc.CartMode = CartModeDeeplink
|
||||
case CartModeDeeplink, CartModeSession:
|
||||
default:
|
||||
v.errorf(key+".cart_mode", "must be %q or %q (got %q)", CartModeDeeplink, CartModeSession, acc.CartMode)
|
||||
}
|
||||
|
||||
if acc.PasswordEnv != "" {
|
||||
@@ -300,9 +286,7 @@ func (v *validator) accounts(cfg *Config, shops map[string]bool, lookupEnv func(
|
||||
}
|
||||
|
||||
for _, f := range []struct{ name, raw string }{{"login_url", acc.LoginURL}, {"cart_url", acc.CartURL}} {
|
||||
if f.raw == "" {
|
||||
continue
|
||||
}
|
||||
if f.raw == "" { continue }
|
||||
|
||||
if msg := checkURL(f.raw); msg != "" {
|
||||
v.errorf(key+"."+f.name, "%s", msg)
|
||||
@@ -324,11 +308,11 @@ func (v *validator) notify(n *Notify) {
|
||||
}
|
||||
|
||||
switch wh.Format {
|
||||
case FormatDiscord, FormatNtfy, FormatJSON:
|
||||
case "":
|
||||
v.errorf(key+".format", "missing required key (%q, %q or %q)", FormatDiscord, FormatNtfy, FormatJSON)
|
||||
default:
|
||||
v.errorf(key+".format", "must be %q, %q or %q (got %q)", FormatDiscord, FormatNtfy, FormatJSON, wh.Format)
|
||||
case FormatDiscord, FormatNtfy, FormatJSON:
|
||||
case "":
|
||||
v.errorf(key+".format", "missing required key (%q, %q or %q)", FormatDiscord, FormatNtfy, FormatJSON)
|
||||
default:
|
||||
v.errorf(key+".format", "must be %q, %q or %q (got %q)", FormatDiscord, FormatNtfy, FormatJSON, wh.Format)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -360,12 +344,12 @@ func durationRange(v *validator, key string, d, lo, hi time.Duration) {
|
||||
func checkURL(raw string) string {
|
||||
u, err := url.Parse(raw)
|
||||
switch {
|
||||
case err != nil:
|
||||
return fmt.Sprintf("invalid URL: %v", err)
|
||||
case u.Scheme != "http" && u.Scheme != "https":
|
||||
return fmt.Sprintf("URL must start with http:// or https:// (got %q)", raw)
|
||||
case u.Host == "":
|
||||
return fmt.Sprintf("URL has no host (got %q)", raw)
|
||||
case err != nil:
|
||||
return fmt.Sprintf("invalid URL: %v", err)
|
||||
case u.Scheme != "http" && u.Scheme != "https":
|
||||
return fmt.Sprintf("URL must start with http:// or https:// (got %q)", raw)
|
||||
case u.Host == "":
|
||||
return fmt.Sprintf("URL has no host (got %q)", raw)
|
||||
}
|
||||
|
||||
return ""
|
||||
|
||||
Reference in new issue
Block a user