Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 1 addition & 31 deletions cmd/k8s.go
Original file line number Diff line number Diff line change
@@ -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)
}
9 changes: 7 additions & 2 deletions cmd/k8s_apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import (
"github.com/spf13/cobra"
)

var k8sNamespace string
var (
k8sNamespace string
k8sContext string
)

var k8sApplyCmd = &cobra.Command{
Use: "apply",
Expand All @@ -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
}
Expand All @@ -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)
}
2 changes: 2 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

Expand All @@ -34,6 +35,7 @@ defaults:
image: codewise:latest
repo_url: https://github.com/example/repo
namespace: default
context: ""
`)

func InitConfig() (string, error) {
Expand Down
26 changes: 24 additions & 2 deletions pkg/k8s/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,38 @@ 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 {
return fmt.Errorf("no manifests found at %s", path)
}

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
Expand Down