From 8d94ef289a1fdaf992dc4f6e075032d9377a6a6e Mon Sep 17 00:00:00 2001 From: aryansharma9917 Date: Thu, 8 Jan 2026 23:12:47 +0530 Subject: [PATCH] use config defaults for k8s values with flag override --- pkg/config/config.go | 33 ++++++++++++++++++++++++--------- pkg/k8s/k8s.go | 31 +++++++++++++++++++++++-------- 2 files changed, 47 insertions(+), 17 deletions(-) diff --git a/pkg/config/config.go b/pkg/config/config.go index 13e859e..e07c69a 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -4,6 +4,8 @@ import ( "fmt" "os" "path/filepath" + + "gopkg.in/yaml.v3" ) const ( @@ -11,16 +13,27 @@ const ( ConfigFileName = "config.yaml" ) -// DefaultConfig is the initial config content +type Config struct { + Version string `yaml:"version"` + User struct { + Name string `yaml:"name"` + } `yaml:"user"` + Defaults struct { + AppName string `yaml:"app_name"` + Image string `yaml:"image"` + RepoURL string `yaml:"repo_url"` + } `yaml:"defaults"` +} + var DefaultConfig = []byte(`version: v1 user: name: aryan defaults: app_name: myapp + image: codewise:latest repo_url: https://github.com/example/repo `) -// InitConfig creates the config directory and file func InitConfig() (string, error) { home, err := os.UserHomeDir() if err != nil { @@ -30,17 +43,14 @@ func InitConfig() (string, error) { configDir := filepath.Join(home, ConfigDirName) configPath := filepath.Join(configDir, ConfigFileName) - // Create directory if not exists if err := os.MkdirAll(configDir, 0755); err != nil { return "", err } - // If config already exists, do nothing if _, err := os.Stat(configPath); err == nil { return configPath, fmt.Errorf("config already exists") } - // Write default config if err := os.WriteFile(configPath, DefaultConfig, 0644); err != nil { return "", err } @@ -48,8 +58,7 @@ func InitConfig() (string, error) { return configPath, nil } -// ReadConfig reads and returns the config file contents -func ReadConfig() ([]byte, error) { +func ReadConfig() (*Config, error) { home, err := os.UserHomeDir() if err != nil { return nil, err @@ -57,9 +66,15 @@ func ReadConfig() ([]byte, error) { configPath := filepath.Join(home, ConfigDirName, ConfigFileName) - if _, err := os.Stat(configPath); err != nil { + data, err := os.ReadFile(configPath) + if err != nil { return nil, fmt.Errorf("config file not found") } - return os.ReadFile(configPath) + var cfg Config + if err := yaml.Unmarshal(data, &cfg); err != nil { + return nil, err + } + + return &cfg, nil } diff --git a/pkg/k8s/k8s.go b/pkg/k8s/k8s.go index 72e913a..64b41da 100644 --- a/pkg/k8s/k8s.go +++ b/pkg/k8s/k8s.go @@ -4,6 +4,8 @@ import ( "fmt" "os" "path/filepath" + + "github.com/aryansharma9917/codewise-cli/pkg/config" ) const k8sDir = "k8s/app" @@ -13,14 +15,27 @@ type Options struct { Image string } -func deploymentYAML(opts Options) []byte { +func applyDefaults(opts Options) Options { + cfg, err := config.ReadConfig() + if err == nil { + if opts.AppName == "" { + opts.AppName = cfg.Defaults.AppName + } + if opts.Image == "" { + opts.Image = cfg.Defaults.Image + } + } if opts.AppName == "" { opts.AppName = "codewise-app" } if opts.Image == "" { opts.Image = "codewise:latest" } + return opts +} +func deploymentYAML(opts Options) []byte { + opts = applyDefaults(opts) return []byte(fmt.Sprintf(`apiVersion: apps/v1 kind: Deployment metadata: @@ -43,11 +58,10 @@ spec: `, opts.AppName, opts.AppName, opts.AppName, opts.AppName, opts.Image)) } -func serviceYAML(appName string) []byte { - if appName == "" { - appName = "codewise-app" +func serviceYAML(app string) []byte { + if app == "" { + app = "codewise-app" } - return []byte(fmt.Sprintf(`apiVersion: v1 kind: Service metadata: @@ -59,13 +73,14 @@ spec: ports: - port: 80 targetPort: 8080 -`, appName, appName)) +`, app, app)) } -// InitK8sManifests creates Kubernetes manifests with optional values func InitK8sManifests(opts Options) error { + opts = applyDefaults(opts) + if _, err := os.Stat(k8sDir); err == nil { - return fmt.Errorf("k8s/app directory already exists") + return fmt.Errorf("%s directory already exists", k8sDir) } if err := os.MkdirAll(k8sDir, 0755); err != nil {