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
37 changes: 37 additions & 0 deletions cmd/deploy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package cmd

import (
"fmt"

"github.com/aryansharma9917/codewise-cli/pkg/deploy"
"github.com/spf13/cobra"
)

var (
deployEnv string
dryRun bool
)

var deployCmd = &cobra.Command{
Use: "deploy",
Short: "Deploy an application using the configured environment",
Run: func(cmd *cobra.Command, args []string) {

if deployEnv == "" {
fmt.Println("please provide --env")
return
}

if err := deploy.Run(deployEnv, dryRun); err != nil {
fmt.Println("deploy error:", err)
}
},
}

func init() {

deployCmd.Flags().StringVar(&deployEnv, "env", "", "Environment to deploy")
deployCmd.Flags().BoolVar(&dryRun, "dry-run", false, "Preview deployment")

rootCmd.AddCommand(deployCmd)
}
27 changes: 19 additions & 8 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,32 @@ import (
var rootCmd = &cobra.Command{
Use: "codewise",
Short: "CLI that helps you scaffold, encode, validate, and automate DevOps workflows easily.",
Long: `Codewise CLI v1.1.0
Long: `Codewise CLI

This CLI helps you with common DevOps tasks like:
- JSON/YAML conversions
- Base64 encoding/decoding
- Dockerfile and Kubernetes manifest generation
- Project scaffolding and GitHub Actions template rendering
A powerful platform-style CLI for DevOps workflows including:

• Environment-aware deployments
• Kubernetes orchestration
• Helm automation
• Docker tooling
• Template generation
• Encoding utilities
`,
Run: func(cmd *cobra.Command, args []string) {

// Banner ONLY when no subcommand is provided
PrintBanner()

_ = cmd.Help()
},
Version: "v1.1.0",
}

// Execute runs the root command.
func Execute() {
PrintBanner()

if err := rootCmd.Execute(); err != nil {
fmt.Println("", err)
fmt.Println("error:", err)
os.Exit(1)
}
}
Expand All @@ -40,5 +50,6 @@ func PrintBanner() {
\____\___/ \__,_|\___/|_.__/ \__, |
|___/
Codewise CLI - Simplify your DevOps

`)
}
78 changes: 78 additions & 0 deletions pkg/deploy/deploy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package deploy

import (
"fmt"
"os/exec"
)

func checkDependency(name string) error {

_, err := exec.LookPath(name)
if err != nil {
return fmt.Errorf("%s not found in PATH. please install it to continue", name)
}

return nil
}

func Run(envName string, dryRun bool) error {

environment, err := LoadEnvironment(envName)
if err != nil {
return err
}

strategy := ResolveStrategy(environment)

executor := Executor{
DryRun: dryRun,
}

switch strategy {

case StrategyHelm:

if err := checkDependency("helm"); err != nil {
return err
}

args := []string{
"upgrade",
"--install",
environment.Helm.Release,
environment.Helm.Chart,
"--namespace",
environment.K8s.Namespace,
}

// inject kube-context if provided
if environment.K8s.Context != "" {
args = append(args, "--kube-context", environment.K8s.Context)
}

return executor.Run("helm", args...)

case StrategyKubectl:

if err := checkDependency("kubectl"); err != nil {
return err
}

args := []string{
"apply",
"-f",
"k8s/",
"-n",
environment.K8s.Namespace,
}

if environment.K8s.Context != "" {
args = append(args, "--context", environment.K8s.Context)
}

return executor.Run("kubectl", args...)

default:
return fmt.Errorf("unknown deployment strategy")
}
}
30 changes: 30 additions & 0 deletions pkg/deploy/executor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package deploy

import (
"fmt"
"os"
"os/exec"
"strings"
)

type Executor struct {
DryRun bool
}

func (e *Executor) Run(name string, args ...string) error {

cmdStr := name + " " + strings.Join(args, " ")

if e.DryRun {
fmt.Println("[dry-run]", cmdStr)
return nil
}

cmd := exec.Command(name, args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

fmt.Println("Running:", cmdStr)

return cmd.Run()
}
17 changes: 17 additions & 0 deletions pkg/deploy/loader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package deploy

import (
"fmt"

"github.com/aryansharma9917/codewise-cli/pkg/env"
)

func LoadEnvironment(name string) (*env.Env, error) {

e, err := env.LoadEnv(name)
if err != nil {
return nil, fmt.Errorf("failed to load environment: %w", err)
}

return e, nil
}
25 changes: 25 additions & 0 deletions pkg/deploy/resolver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package deploy

import (
"os"
"path/filepath"

"github.com/aryansharma9917/codewise-cli/pkg/env"
)

type Strategy string

const (
StrategyHelm Strategy = "helm"
StrategyKubectl Strategy = "kubectl"
)

func ResolveStrategy(e *env.Env) Strategy {

// check if helm chart exists
if _, err := os.Stat(filepath.Join(".", "helm", "chart")); err == nil {
return StrategyHelm
}

return StrategyKubectl
}