Skip to content

Commit 6caee5d

Browse files
Add environment-aware deployment engine with executor and strategy resolution
1 parent 9d257f4 commit 6caee5d

File tree

5 files changed

+187
-0
lines changed

5 files changed

+187
-0
lines changed

cmd/deploy.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/aryansharma9917/codewise-cli/pkg/deploy"
7+
"github.com/spf13/cobra"
8+
)
9+
10+
var (
11+
deployEnv string
12+
dryRun bool
13+
)
14+
15+
var deployCmd = &cobra.Command{
16+
Use: "deploy",
17+
Short: "Deploy an application using the configured environment",
18+
Run: func(cmd *cobra.Command, args []string) {
19+
20+
if deployEnv == "" {
21+
fmt.Println("please provide --env")
22+
return
23+
}
24+
25+
if err := deploy.Run(deployEnv, dryRun); err != nil {
26+
fmt.Println("deploy error:", err)
27+
}
28+
},
29+
}
30+
31+
func init() {
32+
33+
deployCmd.Flags().StringVar(&deployEnv, "env", "", "Environment to deploy")
34+
deployCmd.Flags().BoolVar(&dryRun, "dry-run", false, "Preview deployment")
35+
36+
rootCmd.AddCommand(deployCmd)
37+
}

pkg/deploy/deploy.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package deploy
2+
3+
import (
4+
"fmt"
5+
"os/exec"
6+
)
7+
8+
func checkDependency(name string) error {
9+
10+
_, err := exec.LookPath(name)
11+
if err != nil {
12+
return fmt.Errorf("%s not found in PATH. please install it to continue", name)
13+
}
14+
15+
return nil
16+
}
17+
18+
func Run(envName string, dryRun bool) error {
19+
20+
environment, err := LoadEnvironment(envName)
21+
if err != nil {
22+
return err
23+
}
24+
25+
strategy := ResolveStrategy(environment)
26+
27+
executor := Executor{
28+
DryRun: dryRun,
29+
}
30+
31+
switch strategy {
32+
33+
case StrategyHelm:
34+
35+
if err := checkDependency("helm"); err != nil {
36+
return err
37+
}
38+
39+
args := []string{
40+
"upgrade",
41+
"--install",
42+
environment.Helm.Release,
43+
environment.Helm.Chart,
44+
"--namespace",
45+
environment.K8s.Namespace,
46+
}
47+
48+
// inject kube-context if provided
49+
if environment.K8s.Context != "" {
50+
args = append(args, "--kube-context", environment.K8s.Context)
51+
}
52+
53+
return executor.Run("helm", args...)
54+
55+
case StrategyKubectl:
56+
57+
if err := checkDependency("kubectl"); err != nil {
58+
return err
59+
}
60+
61+
args := []string{
62+
"apply",
63+
"-f",
64+
"k8s/",
65+
"-n",
66+
environment.K8s.Namespace,
67+
}
68+
69+
if environment.K8s.Context != "" {
70+
args = append(args, "--context", environment.K8s.Context)
71+
}
72+
73+
return executor.Run("kubectl", args...)
74+
75+
default:
76+
return fmt.Errorf("unknown deployment strategy")
77+
}
78+
}

pkg/deploy/executor.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package deploy
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"os/exec"
7+
"strings"
8+
)
9+
10+
type Executor struct {
11+
DryRun bool
12+
}
13+
14+
func (e *Executor) Run(name string, args ...string) error {
15+
16+
cmdStr := name + " " + strings.Join(args, " ")
17+
18+
if e.DryRun {
19+
fmt.Println("[dry-run]", cmdStr)
20+
return nil
21+
}
22+
23+
cmd := exec.Command(name, args...)
24+
cmd.Stdout = os.Stdout
25+
cmd.Stderr = os.Stderr
26+
27+
fmt.Println("Running:", cmdStr)
28+
29+
return cmd.Run()
30+
}

pkg/deploy/loader.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package deploy
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/aryansharma9917/codewise-cli/pkg/env"
7+
)
8+
9+
func LoadEnvironment(name string) (*env.Env, error) {
10+
11+
e, err := env.LoadEnv(name)
12+
if err != nil {
13+
return nil, fmt.Errorf("failed to load environment: %w", err)
14+
}
15+
16+
return e, nil
17+
}

pkg/deploy/resolver.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package deploy
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
7+
"github.com/aryansharma9917/codewise-cli/pkg/env"
8+
)
9+
10+
type Strategy string
11+
12+
const (
13+
StrategyHelm Strategy = "helm"
14+
StrategyKubectl Strategy = "kubectl"
15+
)
16+
17+
func ResolveStrategy(e *env.Env) Strategy {
18+
19+
// check if helm chart exists
20+
if _, err := os.Stat(filepath.Join(".", "helm", "chart")); err == nil {
21+
return StrategyHelm
22+
}
23+
24+
return StrategyKubectl
25+
}

0 commit comments

Comments
 (0)