package config import ( "fmt" "maps" "slices" "strconv" "strings" ) // Diff summarizes what changed from old to cfg, one line per change, for // the reload message. func Diff(old, cfg *Config) []string { var out []string oldItems := map[string]Item{} for _, it := range old.Items { oldItems[it.Name] = it } for _, it := range cfg.Items { prev, ok := oldItems[it.Name] if !ok { out = append(out, fmt.Sprintf("+ item %s (%s)", it.Name, countURLs(len(it.URLs)))) continue } delete(oldItems, it.Name) if changes := itemChanges(prev, it); len(changes) > 0 { out = append(out, fmt.Sprintf("~ item %s: %s", it.Name, strings.Join(changes, ", "))) } } for _, it := range old.Items { if _, gone := oldItems[it.Name]; gone { out = append(out, fmt.Sprintf("- item %s", it.Name)) } } if changes := notifyChanges(old.Notify, cfg.Notify); len(changes) > 0 { out = append(out, "~ notify: "+strings.Join(changes, ", ")) } if !maps.Equal(old.Plugins, cfg.Plugins) { out = append(out, "~ plugins") } if !maps.Equal(old.Accounts, cfg.Accounts) { out = append(out, "~ accounts") } if !sameSandbox(old.Sandbox, cfg.Sandbox) { out = append(out, "~ sandbox") } return out } func itemChanges(a, b Item) []string { var out []string change := func(key, from, to string) { if from != to { out = append(out, fmt.Sprintf("%s %s → %s", key, from, to)) } } change("shop", a.Shop, b.Shop) change("max_price", optPrice(a.MaxPrice), optPrice(b.MaxPrice)) change("currency", orNone(a.Currency), orNone(b.Currency)) change("fx_margin", strconv.FormatFloat(a.FXMargin, 'f', -1, 64), strconv.FormatFloat(b.FXMargin, 'f', -1, 64)) change("seller", a.Seller, b.Seller) change("interval", a.Interval.String(), b.Interval.String()) added, removed := 0, 0 for _, u := range b.URLs { if !slices.Contains(a.URLs, u) { added++ } } for _, u := range a.URLs { if !slices.Contains(b.URLs, u) { removed++ } } if added > 0 { out = append(out, "+"+countURLs(added)) } if removed > 0 { out = append(out, "-"+countURLs(removed)) } return out } func notifyChanges(a, b Notify) []string { var out []string if !slices.Equal(a.Webhooks, b.Webhooks) { out = append(out, "webhooks") } if a.Desktop != b.Desktop { out = append(out, fmt.Sprintf("desktop %v → %v", a.Desktop, b.Desktop)) } if a.Cooldown.Duration != b.Cooldown.Duration { out = append(out, fmt.Sprintf("cooldown %s → %s", a.Cooldown, b.Cooldown)) } return out } // sameSandbox compares the limits, not whether they were written out. func sameSandbox(a, b Sandbox) bool { return a.Timeout.Duration == b.Timeout.Duration && a.SessionTimeout.Duration == b.SessionTimeout.Duration && a.MemoryMB == b.MemoryMB && a.CallStack == b.CallStack } func countURLs(n int) string { if n == 1 { return "1 URL" } return strconv.Itoa(n) + " URLs" } func optPrice(p *float64) string { if p == nil { return "none" } return strconv.FormatFloat(*p, 'f', -1, 64) } func orNone(s string) string { if s == "" { return "none" } return s }