diff --git a/cmd/k8s.go b/cmd/k8s.go index 2672d30..f5bcc65 100644 --- a/cmd/k8s.go +++ b/cmd/k8s.go @@ -1,42 +1,12 @@ package cmd -import ( - "fmt" - - "github.com/aryansharma9917/codewise-cli/pkg/k8s" - "github.com/spf13/cobra" -) - -var ( - k8sAppName string - k8sImage string -) +import "github.com/spf13/cobra" var k8sCmd = &cobra.Command{ Use: "k8s", Short: "Kubernetes helpers", } -var k8sInitCmd = &cobra.Command{ - Use: "init", - Short: "Generate Kubernetes manifests", - Run: func(cmd *cobra.Command, args []string) { - err := k8s.InitK8sManifests(k8s.Options{ - AppName: k8sAppName, - Image: k8sImage, - }) - if err != nil { - fmt.Println("ℹ️", err.Error()) - return - } - fmt.Println("✅ Kubernetes manifests created in ./k8s/app") - }, -} - func init() { - k8sInitCmd.Flags().StringVar(&k8sAppName, "app", "", "Application name") - k8sInitCmd.Flags().StringVar(&k8sImage, "image", "", "Docker image name") - - k8sCmd.AddCommand(k8sInitCmd) rootCmd.AddCommand(k8sCmd) } diff --git a/cmd/k8s_apply.go b/cmd/k8s_apply.go index 98959b2..fe3a339 100644 --- a/cmd/k8s_apply.go +++ b/cmd/k8s_apply.go @@ -7,7 +7,10 @@ import ( "github.com/spf13/cobra" ) -var k8sNamespace string +var ( + k8sNamespace string + k8sContext string +) var k8sApplyCmd = &cobra.Command{ Use: "apply", @@ -23,7 +26,7 @@ var k8sApplyCmd = &cobra.Command{ return } - if err := k8s.ApplyManifests(k8sNamespace); err != nil { + if err := k8s.ApplyManifests(k8sNamespace, k8sContext); err != nil { fmt.Println("info:", err.Error()) return } @@ -32,5 +35,7 @@ var k8sApplyCmd = &cobra.Command{ func init() { k8sApplyCmd.Flags().StringVar(&k8sNamespace, "namespace", "", "Kubernetes namespace for deployment") + k8sApplyCmd.Flags().StringVar(&k8sContext, "context", "", "Kubernetes context for deployment") + k8sCmd.AddCommand(k8sApplyCmd) } diff --git a/pkg/config/config.go b/pkg/config/config.go index e1be9ac..64d5df1 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -23,6 +23,7 @@ type Config struct { Image string `yaml:"image"` RepoURL string `yaml:"repo_url"` Namespace string `yaml:"namespace"` + Context string `yaml:"context"` } `yaml:"defaults"` } @@ -34,6 +35,7 @@ defaults: image: codewise:latest repo_url: https://github.com/example/repo namespace: default + context: "" `) func InitConfig() (string, error) { diff --git a/pkg/k8s/apply.go b/pkg/k8s/apply.go index dee21d8..c850847 100644 --- a/pkg/k8s/apply.go +++ b/pkg/k8s/apply.go @@ -39,7 +39,20 @@ func resolveNamespace(flag string) string { return "default" } -func ApplyManifests(namespace string) error { +func resolveContext(flag string) string { + if flag != "" { + return flag + } + + cfg, err := config.ReadConfig() + if err == nil && cfg.Defaults.Context != "" { + return cfg.Defaults.Context + } + + return "" +} + +func ApplyManifests(namespace, context string) error { path := filepath.Join("k8s", "app") if _, err := os.Stat(path); err != nil { @@ -47,8 +60,17 @@ func ApplyManifests(namespace string) error { } namespace = resolveNamespace(namespace) + context = resolveContext(context) + + args := []string{"apply", "-f", path} - args := []string{"apply", "-f", path, "-n", namespace} + if namespace != "" { + args = append(args, "-n", namespace) + } + + if context != "" { + args = append(args, "--context", context) + } cmd := exec.Command("kubectl", args...) cmd.Stdout = os.Stdout