From d051adb6a860f9ed6d230685b3ce0ec94fdce58e Mon Sep 17 00:00:00 2001 From: aryansharma9917 Date: Mon, 12 Jan 2026 22:49:36 +0530 Subject: [PATCH 1/4] add k8s apply logic with kubectl and cluster validation --- pkg/k8s/apply.go | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 pkg/k8s/apply.go diff --git a/pkg/k8s/apply.go b/pkg/k8s/apply.go new file mode 100644 index 0000000..080d097 --- /dev/null +++ b/pkg/k8s/apply.go @@ -0,0 +1,40 @@ +package k8s + +import ( + "errors" + "fmt" + "os" + "os/exec" + "path/filepath" +) + +func CheckKubectl() error { + _, err := exec.LookPath("kubectl") + if err != nil { + return errors.New("kubectl not found in PATH") + } + return nil +} + +func CheckCluster() error { + cmd := exec.Command("kubectl", "version", "--short") + if err := cmd.Run(); err != nil { + return errors.New("cluster unreachable or misconfigured") + } + return nil +} + +func ApplyManifests() error { + path := filepath.Join("k8s", "app") + + if _, err := os.Stat(path); err != nil { + return fmt.Errorf("no manifests found at %s", path) + } + + cmd := exec.Command("kubectl", "apply", "-f", path) + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + + fmt.Println("running: kubectl apply -f", path) + return cmd.Run() +} From d70c00202ef038e3f709cbdc36a7c5c1a3d92a99 Mon Sep 17 00:00:00 2001 From: aryansharma9917 Date: Mon, 12 Jan 2026 22:49:36 +0530 Subject: [PATCH 2/4] add k8s apply command to invoke apply logic --- cmd/k8s_apply.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 cmd/k8s_apply.go diff --git a/cmd/k8s_apply.go b/cmd/k8s_apply.go new file mode 100644 index 0000000..af52fa3 --- /dev/null +++ b/cmd/k8s_apply.go @@ -0,0 +1,33 @@ +package cmd + +import ( + "fmt" + + "github.com/aryansharma9917/codewise-cli/pkg/k8s" + "github.com/spf13/cobra" +) + +var k8sApplyCmd = &cobra.Command{ + Use: "apply", + Short: "Apply Kubernetes manifests to the current cluster", + Run: func(cmd *cobra.Command, args []string) { + if err := k8s.CheckKubectl(); err != nil { + fmt.Println("info:", err.Error()) + return + } + + if err := k8s.CheckCluster(); err != nil { + fmt.Println("info:", err.Error()) + return + } + + if err := k8s.ApplyManifests(); err != nil { + fmt.Println("info:", err.Error()) + return + } + }, +} + +func init() { + k8sCmd.AddCommand(k8sApplyCmd) +} From 834d7f409fa8d51a6659ede0356d9d90d429c54a Mon Sep 17 00:00:00 2001 From: aryansharma9917 Date: Mon, 12 Jan 2026 22:56:23 +0530 Subject: [PATCH 3/4] add namespace support with config fallback for k8s apply --- pkg/config/config.go | 8 +++++--- pkg/k8s/apply.go | 25 ++++++++++++++++++++++--- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/pkg/config/config.go b/pkg/config/config.go index e07c69a..e1be9ac 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -19,9 +19,10 @@ type Config struct { Name string `yaml:"name"` } `yaml:"user"` Defaults struct { - AppName string `yaml:"app_name"` - Image string `yaml:"image"` - RepoURL string `yaml:"repo_url"` + AppName string `yaml:"app_name"` + Image string `yaml:"image"` + RepoURL string `yaml:"repo_url"` + Namespace string `yaml:"namespace"` } `yaml:"defaults"` } @@ -32,6 +33,7 @@ defaults: app_name: myapp image: codewise:latest repo_url: https://github.com/example/repo + namespace: default `) func InitConfig() (string, error) { diff --git a/pkg/k8s/apply.go b/pkg/k8s/apply.go index 080d097..dee21d8 100644 --- a/pkg/k8s/apply.go +++ b/pkg/k8s/apply.go @@ -6,6 +6,8 @@ import ( "os" "os/exec" "path/filepath" + + "github.com/aryansharma9917/codewise-cli/pkg/config" ) func CheckKubectl() error { @@ -24,17 +26,34 @@ func CheckCluster() error { return nil } -func ApplyManifests() error { +func resolveNamespace(flag string) string { + if flag != "" { + return flag + } + + cfg, err := config.ReadConfig() + if err == nil && cfg.Defaults.Namespace != "" { + return cfg.Defaults.Namespace + } + + return "default" +} + +func ApplyManifests(namespace string) error { path := filepath.Join("k8s", "app") if _, err := os.Stat(path); err != nil { return fmt.Errorf("no manifests found at %s", path) } - cmd := exec.Command("kubectl", "apply", "-f", path) + namespace = resolveNamespace(namespace) + + args := []string{"apply", "-f", path, "-n", namespace} + + cmd := exec.Command("kubectl", args...) cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr - fmt.Println("running: kubectl apply -f", path) + fmt.Println("running: kubectl", args) return cmd.Run() } From 6604f627bbd42d691c4b2dc8558f99d267a2d376 Mon Sep 17 00:00:00 2001 From: aryansharma9917 Date: Mon, 12 Jan 2026 22:56:23 +0530 Subject: [PATCH 4/4] add namespace flag to k8s apply command --- cmd/k8s_apply.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/cmd/k8s_apply.go b/cmd/k8s_apply.go index af52fa3..98959b2 100644 --- a/cmd/k8s_apply.go +++ b/cmd/k8s_apply.go @@ -7,6 +7,8 @@ import ( "github.com/spf13/cobra" ) +var k8sNamespace string + var k8sApplyCmd = &cobra.Command{ Use: "apply", Short: "Apply Kubernetes manifests to the current cluster", @@ -21,7 +23,7 @@ var k8sApplyCmd = &cobra.Command{ return } - if err := k8s.ApplyManifests(); err != nil { + if err := k8s.ApplyManifests(k8sNamespace); err != nil { fmt.Println("info:", err.Error()) return } @@ -29,5 +31,6 @@ var k8sApplyCmd = &cobra.Command{ } func init() { + k8sApplyCmd.Flags().StringVar(&k8sNamespace, "namespace", "", "Kubernetes namespace for deployment") k8sCmd.AddCommand(k8sApplyCmd) }