mirror of
https://github.com/skoelle/kctl-tui.git
synced 2026-09-17 20:10:24 +00:00
Add internal/kctl and internal/config packages with unit tests
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
// Package config loads the user-specific, non-versioned kctl-tui
|
||||
// configuration (context pairs, team label key) from a YAML file.
|
||||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
|
||||
"github.com/skoelle/kctl-tui/internal/kctl"
|
||||
)
|
||||
|
||||
// Config is the root structure of ~/.kctl-tui/config.yaml
|
||||
type Config struct {
|
||||
ContextPairs []kctl.ContextPair `yaml:"context_pairs"`
|
||||
TeamLabelKey string `yaml:"team_label_key"`
|
||||
}
|
||||
|
||||
// DefaultPath returns the default config file location: ~/.kctl-tui/config.yaml
|
||||
func DefaultPath() (string, error) {
|
||||
home, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return filepath.Join(home, ".kctl-tui", "config.yaml"), nil
|
||||
}
|
||||
|
||||
// Load reads and parses the config file at path. If the file does not
|
||||
// exist, it returns a zero-value Config (no pairs, empty label key) and no
|
||||
// error, so the tool can run with sane defaults before the user has set up
|
||||
// a config file.
|
||||
func Load(path string) (Config, error) {
|
||||
cfg := Config{}
|
||||
|
||||
data, err := os.ReadFile(path)
|
||||
if os.IsNotExist(err) {
|
||||
return cfg, nil
|
||||
}
|
||||
if err != nil {
|
||||
return cfg, err
|
||||
}
|
||||
|
||||
if err := yaml.Unmarshal(data, &cfg); err != nil {
|
||||
return cfg, err
|
||||
}
|
||||
return cfg, nil
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestLoad_MissingFileReturnsDefaults(t *testing.T) {
|
||||
cfg, err := Load(filepath.Join(t.TempDir(), "does-not-exist.yaml"))
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if len(cfg.ContextPairs) != 0 {
|
||||
t.Fatalf("expected no context pairs, got %v", cfg.ContextPairs)
|
||||
}
|
||||
if cfg.TeamLabelKey != "" {
|
||||
t.Fatalf("expected empty team label key, got %q", cfg.TeamLabelKey)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoad_ValidFile(t *testing.T) {
|
||||
content := []byte(`
|
||||
context_pairs:
|
||||
- name: "env-pair-1"
|
||||
contexts: ["ctx-a", "ctx-b"]
|
||||
team_label_key: "example.org/team"
|
||||
`)
|
||||
path := filepath.Join(t.TempDir(), "config.yaml")
|
||||
if err := os.WriteFile(path, content, 0o600); err != nil {
|
||||
t.Fatalf("failed to write test config: %v", err)
|
||||
}
|
||||
|
||||
cfg, err := Load(path)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if cfg.TeamLabelKey != "example.org/team" {
|
||||
t.Fatalf("unexpected team label key: %q", cfg.TeamLabelKey)
|
||||
}
|
||||
if len(cfg.ContextPairs) != 1 || cfg.ContextPairs[0].Name != "env-pair-1" {
|
||||
t.Fatalf("unexpected context pairs: %v", cfg.ContextPairs)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user