Files
magpie/internal/provider/rename.go
T

128 lines
3.7 KiB
Go

package provider
import (
"errors"
"fmt"
"os"
"slices"
"strings"
"github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/settings"
)
// Rename gives a provider another id, the one its models are picked by
// (<id>/<model>). What magpie keeps that names it by id follows: the
// routing groups it is in and their rules and classifiers, the other
// providers' fallbacks, and which agents are shown it. The old id stays
// with it (Was), so an agent still running on a config that names it the
// old way reaches it, and its usage so far is counted with it; the agents'
// own files are the agent package's to rewrite (agent.RenameProvider).
// A signed-in account keeps the id of its agent.
func Rename(from, to string) error {
from = strings.ToLower(strings.TrimSpace(from))
to = strings.ToLower(strings.TrimSpace(to))
if to == "" || to != Slug(to) {
return fmt.Errorf("a provider's id must be lowercase letters, digits and dashes, not %q", to)
}
if to == from {
return nil
}
if _, ok := find(Accounts(), from); ok || slices.Contains(accountIDs, from) {
return fmt.Errorf("%s is a subscription: its id is its agent's", from)
}
switch {
case to == "magpie":
return errors.New(`"magpie" is what agents call the gateway itself; pick another id`)
case to == strings.TrimSuffix(GroupPrefix, "/"):
return errors.New(`"group" starts the ids of routing groups; pick another id`)
case slices.Contains(accountIDs, to):
return fmt.Errorf("%q is the id of the %s subscription; pick another", to, to)
}
f := load()
i := slices.IndexFunc(f.Providers, func(p Provider) bool { return p.ID == from })
if i < 0 {
return fmt.Errorf("no provider %q", from)
}
if slices.ContainsFunc(f.Providers, func(p Provider) bool { return p.ID == to }) {
return fmt.Errorf("there is a provider %q already", to)
}
// an id another provider once had is this one's now
for j := range f.Providers {
f.Providers[j].Was = slices.DeleteFunc(f.Providers[j].Was, func(w string) bool { return w == to })
}
p := &f.Providers[i]
p.ID = to
if !slices.Contains(p.Was, from) {
p.Was = append(p.Was, from)
}
for j := range f.Providers {
for k, m := range f.Providers[j].Fallback {
f.Providers[j].Fallback[k] = renamedRef(m, from, to)
}
}
for j := range f.Groups {
g := &f.Groups[j]
for k, m := range g.Members {
g.Members[k] = renamedRef(m, from, to)
}
for k, r := range g.Rules {
g.Rules[k].Use = renamedRef(r.Use, from, to)
}
g.Classifier = renamedRef(g.Classifier, from, to)
}
// the vendor's list last fetched goes with it
os.Rename(catalog.LivePath(from), catalog.LivePath(to))
if err := store(f); err != nil {
return err
}
s := settings.Load()
changed := renameModelPrefs(&s, from, to)
for a, names := range s.Visible {
for k, n := range names {
if strings.EqualFold(n, from) {
s.Visible[a][k], changed = to, true
}
}
}
if changed {
return settings.Save(s)
}
return nil
}
// renamedRef is a model id ("provider/model", "magpie/provider/model")
// with the provider from named to instead.
func renamedRef(ref, from, to string) string {
pre, rest := "", ref
if r, ok := strings.CutPrefix(ref, "magpie/"); ok {
pre, rest = "magpie/", r
}
if m, ok := strings.CutPrefix(rest, from+"/"); ok {
return pre + to + "/" + m
}
return ref
}
// RenamedRef is ref with the provider it names by an id the provider had
// named by the id it has now.
func RenamedRef(ref string) string {
for old, now := range Renamed() {
if r := renamedRef(ref, old, now); r != ref {
return r
}
}
return ref
}
// Renamed maps the ids providers had to the ones they have now.
func Renamed() map[string]string {
out := map[string]string{}
for _, p := range load().Providers {
for _, w := range p.Was {
out[w] = p.ID
}
}
return out
}