From 321d78a01785b3d0874b78cef5e12dbb0d8f87c2 Mon Sep 17 00:00:00 2001 From: XingfenD Date: Mon, 10 Aug 2026 17:19:09 +0800 Subject: [PATCH] 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 --- config.toml | 24 +++++++ .../helm/proto-gen/templates/configmap.yaml | 42 +++++++------ go.mod | 3 +- go.sum | 2 + internal/config/config.go | 38 ++++++------ internal/config/config_test.go | 62 ++++++++++--------- testdata/config.toml | 25 ++++++++ 7 files changed, 129 insertions(+), 67 deletions(-) create mode 100644 config.toml create mode 100644 testdata/config.toml diff --git a/config.toml b/config.toml new file mode 100644 index 0000000..6980d7b --- /dev/null +++ b/config.toml @@ -0,0 +1,24 @@ +[server] +addr = ":8080" + +[git] +type = "gitea" +base_url = "https://git.example.com" +token = "test-token" + +[cluster] +kubeconfig = "/dev/null" +namespace = "proto-gen" +job_timeout = 600 +max_concurrent = 2 + +toolchain_registry = "registry.example.com/proto-gen" +target_repo_template = "{proto_repo}-gen-{lang}" + +[[plugins]] +language = "go" +image = "proto-gen-go:v1.34.2" + +[[plugins]] +language = "nodejs" +image = "proto-gen-ts:v0.9.0" diff --git a/deploy/helm/proto-gen/templates/configmap.yaml b/deploy/helm/proto-gen/templates/configmap.yaml index 717bc9f..2951d9a 100644 --- a/deploy/helm/proto-gen/templates/configmap.yaml +++ b/deploy/helm/proto-gen/templates/configmap.yaml @@ -3,22 +3,26 @@ kind: ConfigMap metadata: name: {{ .Release.Name }}-config data: - config.yaml: | - server: - addr: ":8080" - git: - type: gitea - base_url: "{{ .Values.git.baseUrl }}" - token: "${GIT_TOKEN}" - cluster: - kubeconfig: /etc/proto-gen/kubeconfig - namespace: {{ .Values.cluster.namespace }} - job_timeout: {{ .Values.cluster.jobTimeout }} - max_concurrent: {{ .Values.cluster.maxConcurrent }} - toolchain_registry: "{{ .Values.toolchainRegistry }}" - target_repo_template: "{{ .Values.targetRepoTemplate }}" - plugins: - {{- range .Values.plugins }} - - language: {{ .language }} - image: "{{ .image }}" - {{- end }} + config.toml: | + [server] + addr = ":8080" + + [git] + type = "gitea" + base_url = "{{ .Values.git.baseUrl }}" + token = "${GIT_TOKEN}" + + [cluster] + kubeconfig = "/etc/proto-gen/kubeconfig" + namespace = "{{ .Values.cluster.namespace }}" + job_timeout = {{ .Values.cluster.jobTimeout }} + max_concurrent = {{ .Values.cluster.maxConcurrent }} + + toolchain_registry = "{{ .Values.toolchainRegistry }}" + target_repo_template = "{{ .Values.targetRepoTemplate }}" + + {{- range .Values.plugins }} + [[plugins]] + language = "{{ .language }}" + image = "{{ .image }}" + {{- end }} diff --git a/go.mod b/go.mod index fe55624..a82d8fc 100644 --- a/go.mod +++ b/go.mod @@ -3,9 +3,9 @@ module proto-gen go 1.26.5 require ( + github.com/BurntSushi/toml v1.6.0 github.com/go-chi/chi/v5 v5.3.1 github.com/google/uuid v1.6.0 - gopkg.in/yaml.v3 v3.0.1 k8s.io/api v0.36.3 k8s.io/apimachinery v0.36.3 k8s.io/client-go v0.36.3 @@ -44,6 +44,7 @@ require ( google.golang.org/protobuf v1.36.12-0.20260120151049-f2248ac996af // indirect gopkg.in/evanphx/json-patch.v4 v4.13.0 // indirect gopkg.in/inf.v0 v0.9.1 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect k8s.io/klog/v2 v2.140.0 // indirect k8s.io/kube-openapi v0.0.0-20260317180543-43fb72c5454a // indirect k8s.io/utils v0.0.0-20260210185600-b8788abfbbc2 // indirect diff --git a/go.sum b/go.sum index 3ab31da..7ec4400 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +github.com/BurntSushi/toml v1.6.0 h1:dRaEfpa2VI55EwlIW72hMRHdWouJeRF7TPYhI+AUQjk= +github.com/BurntSushi/toml v1.6.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= diff --git a/internal/config/config.go b/internal/config/config.go index 33ee319..6d4954a 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -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) } diff --git a/internal/config/config_test.go b/internal/config/config_test.go index fd7bf94..acd6bf1 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -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) } diff --git a/testdata/config.toml b/testdata/config.toml new file mode 100644 index 0000000..20af964 --- /dev/null +++ b/testdata/config.toml @@ -0,0 +1,25 @@ +[server] +addr = ":8080" +public_url = "https://gen.example.com" + +[git] +type = "gitea" +base_url = "https://git.example.com" +token = "${GIT_TOKEN}" + +[cluster] +kubeconfig = "/etc/proto-gen/kubeconfig" +namespace = "proto-gen" +job_timeout = 600 +max_concurrent = 2 + +toolchain_registry = "registry.example.com/proto-gen" +target_repo_template = "{proto_repo}-gen-{lang}" + +[[plugins]] +language = "go" +image = "proto-gen-go:v1.34.2" + +[[plugins]] +language = "nodejs" +image = "proto-gen-ts:v0.9.0"