2022-07-18 10:10:24 -05:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2022-07-25 10:13:36 -05:00
|
|
|
"context"
|
2022-07-18 10:10:24 -05:00
|
|
|
"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"
|
2022-07-26 15:22:38 -05:00
|
|
|
log "github.com/sirupsen/logrus"
|
2022-07-18 10:10:24 -05:00
|
|
|
"golang.org/x/exp/slices"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2022-07-19 01:37:47 -05:00
|
|
|
CONFIG_FILE_DEFAULT = "/etc/ccl.toml"
|
2022-07-18 10:10:24 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type command string
|
|
|
|
|
|
|
|
var (
|
2022-07-27 21:48:47 -05:00
|
|
|
ConfigFile = CONFIG_FILE_DEFAULT
|
|
|
|
Networks = []network.Network{}
|
|
|
|
Containers = []container.Container{}
|
2022-07-18 10:10:24 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func Categories() []string {
|
2022-07-26 15:22:38 -05:00
|
|
|
categories := []string{"all"}
|
2022-07-18 10:10:24 -05:00
|
|
|
gs := hashset.New()
|
2022-07-27 01:01:24 -05:00
|
|
|
for _, c := range Containers {
|
2022-07-18 10:10:24 -05:00
|
|
|
gs.Add(c.Category)
|
|
|
|
}
|
|
|
|
for _, c := range gs.Values() {
|
2022-07-26 15:22:38 -05:00
|
|
|
categories = append(categories, c.(string))
|
2022-07-18 10:10:24 -05:00
|
|
|
}
|
2022-07-27 21:48:47 -05:00
|
|
|
slices.Sort(categories)
|
2022-07-26 15:22:38 -05:00
|
|
|
return categories
|
2022-07-18 10:10:24 -05:00
|
|
|
}
|
|
|
|
|
2022-07-27 21:48:47 -05:00
|
|
|
func Union(ids []string, catMask []string) (conts []container.Container) {
|
2022-07-18 10:10:24 -05:00
|
|
|
if len(ids) == 0 {
|
2022-07-27 21:48:47 -05:00
|
|
|
ids = []string{"all"}
|
2022-07-18 10:10:24 -05:00
|
|
|
}
|
|
|
|
h := hashset.New()
|
|
|
|
for _, id := range ids {
|
2022-07-27 21:48:47 -05:00
|
|
|
if j := slices.Index(catMask, id); j >= 0 {
|
|
|
|
catMask = slices.Delete(catMask, j, j+1)
|
|
|
|
}
|
2022-07-27 01:01:24 -05:00
|
|
|
for _, c := range Containers {
|
2022-07-27 21:48:47 -05:00
|
|
|
if (id == "all" || c.Category == id) && !slices.Contains(catMask, c.Category) {
|
|
|
|
h.Add(c.Name)
|
|
|
|
}
|
|
|
|
if c.Name == id {
|
2022-07-18 10:10:24 -05:00
|
|
|
h.Add(c.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, c := range h.Values() {
|
|
|
|
name := c.(string)
|
2022-07-27 01:01:24 -05:00
|
|
|
match := slices.IndexFunc(Containers, func(c container.Container) bool { return c.Name == name })
|
|
|
|
conts = append(conts, Containers[match])
|
2022-07-18 10:10:24 -05:00
|
|
|
}
|
2022-07-26 15:22:38 -05:00
|
|
|
if len(conts) == 0 {
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"ids": ids,
|
2022-07-27 21:48:47 -05:00
|
|
|
}).Warnln("No matching containers. If disabled, try adding -a")
|
2022-07-26 15:22:38 -05:00
|
|
|
}
|
2022-07-27 21:48:47 -05:00
|
|
|
slices.SortFunc(conts, func(a, b container.Container) bool {
|
|
|
|
if a.Category < b.Category {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if a.Category > b.Category {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return a.Name <= b.Name
|
|
|
|
})
|
2022-07-18 10:10:24 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-07-25 10:13:36 -05:00
|
|
|
func Init(conn context.Context) error {
|
2022-07-27 01:01:24 -05:00
|
|
|
// A parsing convenience
|
|
|
|
type parse struct {
|
|
|
|
Networks []network.Network
|
|
|
|
Containers []container.Container
|
|
|
|
}
|
|
|
|
|
2022-07-18 10:10:24 -05:00
|
|
|
f, err := os.ReadFile(ConfigFile)
|
|
|
|
if err != nil {
|
2022-07-18 19:47:05 -05:00
|
|
|
return err
|
2022-07-18 10:10:24 -05:00
|
|
|
}
|
|
|
|
p := parse{}
|
|
|
|
err = toml.Unmarshal(f, &p)
|
|
|
|
if err != nil {
|
2022-07-18 19:47:05 -05:00
|
|
|
return err
|
2022-07-18 10:10:24 -05:00
|
|
|
}
|
2022-07-27 01:01:24 -05:00
|
|
|
Containers, Networks = p.Containers, p.Networks
|
2022-07-18 10:10:24 -05:00
|
|
|
for i := range p.Containers {
|
2022-07-27 01:01:24 -05:00
|
|
|
p.Containers[i].Init(conn, Networks)
|
2022-07-18 10:10:24 -05:00
|
|
|
}
|
2022-07-27 21:48:47 -05:00
|
|
|
slices.SortFunc(Containers, func(a, b container.Container) bool {
|
|
|
|
return a.Name < b.Name
|
|
|
|
})
|
2022-07-18 19:47:05 -05:00
|
|
|
return nil
|
2022-07-18 10:10:24 -05:00
|
|
|
}
|