package config import ( "context" "os" "gitea.elkins.co/Networking/ccl/internal/pkg/container" "gitea.elkins.co/Networking/ccl/internal/pkg/network" "github.com/emirpasic/gods/sets/hashset" toml "github.com/pelletier/go-toml" log "github.com/sirupsen/logrus" "golang.org/x/exp/slices" ) const ( CONFIG_FILE_DEFAULT = "/etc/ccl.toml" ) type command string var ( ConfigFile string = CONFIG_FILE_DEFAULT Networks = []network.Network{} Containers = []container.Container{} ) func Categories() []string { categories := []string{"all"} gs := hashset.New() for _, c := range Containers { gs.Add(c.Category) } for _, c := range gs.Values() { categories = append(categories, c.(string)) } return categories } func Union(ids []string) (conts []container.Container) { if len(ids) == 0 { return Containers } h := hashset.New() for _, id := range ids { for _, c := range Containers { if id == "all" || c.Name == id || c.Category == id { h.Add(c.Name) } } } for _, c := range h.Values() { name := c.(string) match := slices.IndexFunc(Containers, func(c container.Container) bool { return c.Name == name }) conts = append(conts, Containers[match]) } if len(conts) == 0 { log.WithFields(log.Fields{ "ids": ids, }).Warnln("No matching containers") } return } func Init(conn context.Context) error { // A parsing convenience type parse struct { Networks []network.Network Containers []container.Container } f, err := os.ReadFile(ConfigFile) if err != nil { return err } p := parse{} err = toml.Unmarshal(f, &p) if err != nil { return err } Containers, Networks = p.Containers, p.Networks for i := range p.Containers { p.Containers[i].Init(conn, Networks) } return nil }