refactor: switch config format from YAML to TOML

- Replace gopkg.in/yaml.v3 with github.com/BurntSushi/toml
- Update struct tags from yaml to toml
- Update config tests to use TOML syntax
- Add testdata/config.toml, update Helm configmap template
This commit is contained in:
2026-08-10 17:19:09 +08:00
parent 4e467e1136
commit 321d78a017
7 changed files with 129 additions and 67 deletions
+19 -19
View File
@@ -6,39 +6,39 @@ import (
"regexp"
"strings"
"gopkg.in/yaml.v3"
"github.com/BurntSushi/toml"
)
type Config struct {
Server ServerConfig `yaml:"server"`
Git GitConfig `yaml:"git"`
Cluster ClusterConfig `yaml:"cluster"`
ToolchainRegistry string `yaml:"toolchain_registry"`
TargetRepoTemplate string `yaml:"target_repo_template"`
Plugins []PluginConfig `yaml:"plugins"`
Server ServerConfig `toml:"server"`
Git GitConfig `toml:"git"`
Cluster ClusterConfig `toml:"cluster"`
ToolchainRegistry string `toml:"toolchain_registry"`
TargetRepoTemplate string `toml:"target_repo_template"`
Plugins []PluginConfig `toml:"plugins"`
}
type ServerConfig struct {
Addr string `yaml:"addr"`
PublicURL string `yaml:"public_url"`
Addr string `toml:"addr"`
PublicURL string `toml:"public_url"`
}
type GitConfig struct {
Type string `yaml:"type"`
BaseURL string `yaml:"base_url"`
Token string `yaml:"token"`
Type string `toml:"type"`
BaseURL string `toml:"base_url"`
Token string `toml:"token"`
}
type ClusterConfig struct {
Kubeconfig string `yaml:"kubeconfig"`
Namespace string `yaml:"namespace"`
JobTimeout int `yaml:"job_timeout"`
MaxConcurrent int `yaml:"max_concurrent"`
Kubeconfig string `toml:"kubeconfig"`
Namespace string `toml:"namespace"`
JobTimeout int `toml:"job_timeout"`
MaxConcurrent int `toml:"max_concurrent"`
}
type PluginConfig struct {
Language string `yaml:"language"`
Image string `yaml:"image"`
Language string `toml:"language"`
Image string `toml:"image"`
}
var envVarRegex = regexp.MustCompile(`\$\{(\w+)\}`)
@@ -52,7 +52,7 @@ func Load(path string) (*Config, error) {
expanded := expandEnvVars(string(data))
var cfg Config
if err := yaml.Unmarshal([]byte(expanded), &cfg); err != nil {
if _, err := toml.Decode(expanded, &cfg); err != nil {
return nil, fmt.Errorf("parsing config: %w", err)
}
+34 -28
View File
@@ -7,20 +7,22 @@ import (
)
func TestLoad_MissingFile(t *testing.T) {
_, err := Load("/nonexistent/config.yaml")
_, err := Load("/nonexistent/config.toml")
if err == nil {
t.Fatal("expected error for missing file")
}
}
func TestLoad_MissingGitToken(t *testing.T) {
cfg := `server:
addr: ":8080"
git:
type: gitea
base_url: "https://git.example.com"
cluster:
namespace: proto-gen`
cfg := `[server]
addr = ":8080"
[git]
type = "gitea"
base_url = "https://git.example.com"
[cluster]
namespace = "proto-gen"`
path := writeTempConfig(t, cfg)
os.Setenv("GIT_TOKEN", "")
_, err := Load(path)
@@ -33,19 +35,22 @@ func TestLoad_ValidConfig(t *testing.T) {
os.Setenv("GIT_TOKEN", "test-token")
defer os.Unsetenv("GIT_TOKEN")
cfg := `server:
addr: ":9090"
git:
type: gitea
base_url: "https://git.example.com"
token: ${GIT_TOKEN}
cluster:
namespace: proto-gen
job_timeout: 300
max_concurrent: 3
plugins:
- language: go
image: "proto-gen-go:v1.0"`
cfg := `[server]
addr = ":9090"
[git]
type = "gitea"
base_url = "https://git.example.com"
token = "${GIT_TOKEN}"
[cluster]
namespace = "proto-gen"
job_timeout = 300
max_concurrent = 3
[[plugins]]
language = "go"
image = "proto-gen-go:v1.0"`
path := writeTempConfig(t, cfg)
got, err := Load(path)
@@ -73,12 +78,13 @@ func TestLoad_DefaultValues(t *testing.T) {
os.Setenv("GIT_TOKEN", "test-token")
defer os.Unsetenv("GIT_TOKEN")
cfg := `git:
type: gitea
base_url: "https://git.example.com"
token: ${GIT_TOKEN}
cluster:
namespace: proto-gen`
cfg := `[git]
type = "gitea"
base_url = "https://git.example.com"
token = "${GIT_TOKEN}"
[cluster]
namespace = "proto-gen"`
path := writeTempConfig(t, cfg)
got, err := Load(path)
@@ -119,7 +125,7 @@ func TestGetPluginByLanguage(t *testing.T) {
func writeTempConfig(t *testing.T, content string) string {
t.Helper()
dir := t.TempDir()
path := filepath.Join(dir, "config.yaml")
path := filepath.Join(dir, "config.toml")
if err := os.WriteFile(path, []byte(content), 0644); err != nil {
t.Fatalf("writing temp config: %v", err)
}