Skip to content

Commit 5335ab1

Browse files
Refactor deploy engine to use command builder and enforce environment validation
1 parent 7953121 commit 5335ab1

2 files changed

Lines changed: 14 additions & 50 deletions

File tree

pkg/deploy/deploy.go

Lines changed: 9 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -22,57 +22,18 @@ func Run(envName string, dryRun bool) error {
2222
return err
2323
}
2424

25-
strategy := ResolveStrategy(environment)
25+
command, _, err := BuildCommand(environment)
26+
if err != nil {
27+
return err
28+
}
29+
30+
if err := checkDependency(command.Name); err != nil {
31+
return err
32+
}
2633

2734
executor := Executor{
2835
DryRun: dryRun,
2936
}
3037

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-
}
38+
return executor.Run(command.Name, command.Args...)
7839
}

pkg/deploy/loader.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@ import (
99
func LoadEnvironment(name string) (*env.Env, error) {
1010

1111
e, err := env.LoadEnv(name)
12-
if err != nil {
13-
return nil, fmt.Errorf("failed to load environment: %w", err)
12+
if err != nil || e == nil {
13+
return nil, fmt.Errorf(
14+
"environment %q not found. run 'codewise env list' to see available environments",
15+
name,
16+
)
1417
}
1518

1619
return e, nil

0 commit comments

Comments
 (0)