pgcli/internal/config/config.go

63 lines
1.2 KiB
Go

package config
import (
"bytes"
"os"
"path/filepath"
"gopkg.in/yaml.v3"
)
type Config struct {
Kube KubeConfig `yaml:"kube"`
App AppConfig `yaml:"app"`
}
type KubeConfig struct {
PodDB KubeSecretDBConfig `yaml:"db"`
Secret KubeSecretDBConfig `yaml:"secret"`
}
type KubePodDBConfig struct {
Substring string `yaml:"pod-substring"`
}
type KubeSecretDBConfig struct {
Substring string `yaml:"pod-substring"`
DataFields struct {
Password string `yaml:"password"`
Username string `yaml:"username"`
DBName string `yaml:"dbname"`
Port string `yaml:"port"`
} `yaml:"data-fields"`
}
type AppConfig struct {
PortForwardOnly struct {
Enabled bool `yaml:"enabled"`
LocalHost string `yaml:"local-host"`
LocalPort int `yaml:"local-port"`
} `yaml:"port-forward-only"`
UseApp struct {
PgCLI bool `yaml:"pgcli"`
Psql bool `yaml:"psql"`
} `yaml:"use-app"`
}
func InitConfig(config string) (*Config, error) {
f, err := os.ReadFile(filepath.Clean(config))
if err != nil {
return nil, err
}
buf := bytes.NewReader(f)
var cfg Config
err = yaml.NewDecoder(buf).Decode(&cfg)
if err != nil {
return nil, err
}
return &cfg, nil
}