feat(k3d): switch to map

This commit is contained in:
Kevin Franklin Kim 2024-01-25 17:50:28 +01:00
parent 1cf6500352
commit 875a26dfbd
No known key found for this signature in database
2 changed files with 16 additions and 19 deletions

View File

@ -50,7 +50,7 @@ k3d:
name: foomo-registry
port: 12345
clusters:
- name: local
local:
port: 9443
alias: foomo
enableTraefikRouter: false

View File

@ -5,13 +5,14 @@ import (
"github.com/foomo/posh/pkg/env"
"github.com/pkg/errors"
"github.com/samber/lo"
)
type (
Config struct {
Charts ConfigCharts `json:"charts" yaml:"charts"`
Registry ConfigRegistry `json:"registry" yaml:"registry"`
Clusters []ConfigCluster `json:"clusters" yaml:"clusters"`
Charts ConfigCharts `json:"charts" yaml:"charts"`
Registry ConfigRegistry `json:"registry" yaml:"registry"`
Clusters map[string]ConfigCluster `json:"clusters" yaml:"clusters"`
}
ConfigCharts struct {
Path string `json:"path" yaml:"path"`
@ -23,7 +24,7 @@ type (
}
ConfigCluster struct {
// Cluster name
Name string `json:"name" yaml:"name"`
Name string `json:"-" yaml:"-"`
// K3d cluster name
Alias string `json:"alias" yaml:"alias"`
// Docker image to use
@ -37,15 +38,6 @@ type (
}
)
func (c Config) Cluster(name string) (ConfigCluster, error) {
for _, v := range c.Clusters {
if v.Name == name {
return v, nil
}
}
return ConfigCluster{}, errors.Errorf("missing cluster config: %s", name)
}
func (c ConfigCharts) Names() ([]string, error) {
files, err := os.ReadDir(env.Path(c.Path))
if err != nil {
@ -62,12 +54,17 @@ func (c ConfigCharts) Names() ([]string, error) {
return ret, nil
}
func (c Config) ClusterNames() []string {
ret := make([]string, len(c.Clusters))
for i, v := range c.Clusters {
ret[i] = v.Name
func (c Config) Cluster(name string) (ConfigCluster, error) {
if value, ok := c.Clusters[name]; ok {
value.Name = name
return value, nil
} else {
return ConfigCluster{}, errors.Errorf("missing cluster config: %s", name)
}
return ret
}
func (c Config) ClusterNames() []string {
return lo.Keys(c.Clusters)
}
func (c ConfigCluster) AliasName() string {