Skip to content

Commit 42f1572

Browse files
committed
Refactored to use Terraform
1 parent accc601 commit 42f1572

35 files changed

+1132
-945
lines changed

cmd/deploy.go

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,20 @@ import (
44
"fmt"
55

66
"github.com/codeupify/upify/internal/config"
7-
"github.com/codeupify/upify/internal/platform/aws/lambda"
8-
"github.com/codeupify/upify/internal/platform/gcp/cloudrun"
7+
"github.com/codeupify/upify/internal/platform"
8+
"github.com/codeupify/upify/internal/platform/aws"
9+
"github.com/codeupify/upify/internal/platform/gcp"
910
"github.com/spf13/cobra"
1011
)
1112

1213
var deployCmd = &cobra.Command{
1314
Use: "deploy [platform]",
1415
Short: "Deploy the application to a specified platform",
1516
Long: `Deploy the application to a specified platform.
16-
Currently supported platforms: aws-lambda, gcp-cloudrun
17+
Currently supported platforms: aws, gcp
1718
1819
Example:
19-
upify deploy aws-lambda`,
20+
upify deploy aws`,
2021
Args: cobra.ExactArgs(1),
2122
RunE: func(cmd *cobra.Command, args []string) error {
2223
platform := args[0]
@@ -33,26 +34,20 @@ func init() {
3334
rootCmd.AddCommand(deployCmd)
3435
}
3536

36-
func deploy(platform string, cfg *config.Config) error {
37-
switch platform {
38-
case "aws-lambda":
39-
if cfg.AWSLambda == nil {
40-
return fmt.Errorf("aws-lambda configuration is not set up. Please run 'upify platform add aws-lambda' first")
37+
func deploy(platformStr string, cfg *config.Config) error {
38+
switch platformStr {
39+
case string(platform.AWS):
40+
fmt.Println("Deploying to AWS...")
41+
if err := aws.Deploy(cfg); err != nil {
42+
return fmt.Errorf("failed to deploy to AWS: %w", err)
4143
}
42-
fmt.Println("Deploying to AWS Lambda...")
43-
if err := lambda.Deploy(cfg); err != nil {
44-
return fmt.Errorf("failed to deploy to AWS Lambda: %w", err)
45-
}
46-
case "gcp-cloudrun":
47-
if cfg.GCPCloudRun == nil {
48-
return fmt.Errorf("gcp-cloudrun configuration is not set up. Please run 'upify platform add gcp-cloudrun' first")
49-
}
50-
fmt.Println("Deploying to GCP Cloud Run...")
51-
if err := cloudrun.Deploy(cfg); err != nil {
52-
return fmt.Errorf("failed to deploy to GCP Cloud Run: %w", err)
44+
case string(platform.GCP):
45+
fmt.Println("Deploying to GCP...")
46+
if err := gcp.Deploy(cfg); err != nil {
47+
return fmt.Errorf("failed to deploy to GCP: %w", err)
5348
}
5449
default:
55-
return fmt.Errorf("unsupported platform: %s", platform)
50+
return fmt.Errorf("unsupported platform: %s", platformStr)
5651
}
5752

5853
return nil

cmd/init.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"github.com/AlecAivazis/survey/v2"
1111
"github.com/codeupify/upify/internal/config"
1212
"github.com/codeupify/upify/internal/framework"
13-
"github.com/codeupify/upify/internal/handler"
13+
"github.com/codeupify/upify/internal/infra"
1414
"github.com/codeupify/upify/internal/lang"
1515
"github.com/spf13/cobra"
1616
)
@@ -120,12 +120,12 @@ var initCmd = &cobra.Command{
120120
AppVar: appVar,
121121
}
122122

123-
if err := handler.AddHandlerFile(cfg); err != nil {
123+
if err := infra.AddHandlerFile(cfg); err != nil {
124124
return err
125125
}
126126

127127
if entrypoint == "" {
128-
if err := handler.AddMainFile(cfg); err != nil {
128+
if err := infra.AddMainFile(cfg); err != nil {
129129
return err
130130
}
131131
}
@@ -134,10 +134,15 @@ var initCmd = &cobra.Command{
134134
return fmt.Errorf("failed to save configuration: %w", err)
135135
}
136136

137+
err = infra.AddEnvironmentFile()
138+
if err != nil {
139+
return err
140+
}
141+
137142
fmt.Println("Done!")
138143

139144
if entrypoint == "" {
140-
mainPath := handler.GetMainPath(selectedLanguage)
145+
mainPath := infra.GetMainPath(selectedLanguage)
141146
fmt.Printf("\n\033[1mImportant!\033[0m To finish the configuration wrap your script by modifying the file at \033[1m%s\033[0m\n", mainPath)
142147
}
143148

cmd/platform.go

Lines changed: 29 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ import (
55

66
"github.com/AlecAivazis/survey/v2"
77
"github.com/codeupify/upify/internal/config"
8+
"github.com/codeupify/upify/internal/infra"
89
"github.com/codeupify/upify/internal/lang"
9-
"github.com/codeupify/upify/internal/platform/aws/lambda"
10-
"github.com/codeupify/upify/internal/platform/gcp/cloudrun"
10+
"github.com/codeupify/upify/internal/platform/aws"
11+
"github.com/codeupify/upify/internal/platform/gcp"
1112
"github.com/spf13/cobra"
1213
)
1314

@@ -29,10 +30,10 @@ var platformListCmd = &cobra.Command{
2930
},
3031
}
3132

32-
var awsLambdaCmd = &cobra.Command{
33-
Use: "aws-lambda",
34-
Short: "Add AWS Lambda configuration",
35-
RunE: addAWSLambda,
33+
var awsCmd = &cobra.Command{
34+
Use: "aws",
35+
Short: "Add AWS configuration",
36+
RunE: addAws,
3637
}
3738

3839
var awsRegion string
@@ -42,28 +43,28 @@ var gcpRegion string
4243
var gcpProjectId string
4344
var gcpRuntime string
4445

45-
var gcpCloudRunCmd = &cobra.Command{
46-
Use: "gcp-cloudrun",
47-
Short: "Add GCP Cloud Run configuration",
48-
RunE: addGCPCloudRun,
46+
var gcpCmd = &cobra.Command{
47+
Use: "gcp",
48+
Short: "Add GCP configuration",
49+
RunE: addGCP,
4950
}
5051

5152
func init() {
5253
rootCmd.AddCommand(platformCmd)
5354
platformCmd.AddCommand(platformAddCmd)
5455
platformCmd.AddCommand(platformListCmd)
5556

56-
platformAddCmd.AddCommand(awsLambdaCmd)
57-
awsLambdaCmd.Flags().StringVar(&awsRegion, "region", "", "AWS region")
58-
awsLambdaCmd.Flags().StringVar(&awsRuntime, "runtime", "", "Lambda runtime")
57+
platformAddCmd.AddCommand(awsCmd)
58+
awsCmd.Flags().StringVar(&awsRegion, "region", "", "AWS region")
59+
awsCmd.Flags().StringVar(&awsRuntime, "runtime", "", "Lambda runtime")
5960

60-
platformAddCmd.AddCommand(gcpCloudRunCmd)
61-
gcpCloudRunCmd.Flags().StringVar(&gcpRegion, "region", "", "GCP region")
62-
gcpCloudRunCmd.Flags().StringVar(&gcpProjectId, "project-id", "", "GCP project ID")
63-
gcpCloudRunCmd.Flags().StringVar(&gcpRuntime, "runtime", "", "Cloud Run runtime")
61+
platformAddCmd.AddCommand(gcpCmd)
62+
gcpCmd.Flags().StringVar(&gcpRegion, "region", "", "GCP region")
63+
gcpCmd.Flags().StringVar(&gcpProjectId, "project-id", "", "GCP project ID")
64+
gcpCmd.Flags().StringVar(&gcpRuntime, "runtime", "", "Cloud Run runtime")
6465
}
6566

66-
func addAWSLambda(cmd *cobra.Command, args []string) error {
67+
func addAws(cmd *cobra.Command, args []string) error {
6768
cfg, err := config.LoadConfig()
6869
if err != nil {
6970
return fmt.Errorf("failed to load config: %w", err)
@@ -80,7 +81,7 @@ func addAWSLambda(cmd *cobra.Command, args []string) error {
8081
}
8182

8283
if awsRuntime == "" {
83-
runtimes := getAWSRuntimes(cfg.Language)
84+
runtimes := getAWSLambdaRuntimes(cfg.Language)
8485
if len(runtimes) == 0 {
8586
return fmt.Errorf("CLI doesn't support the specified language yet for AWS Lambda: %s", cfg.Language)
8687
}
@@ -94,23 +95,15 @@ func addAWSLambda(cmd *cobra.Command, args []string) error {
9495
}
9596
}
9697

97-
if err := lambda.AddConfig(cfg, awsRegion, awsRuntime); err != nil {
98+
if err := aws.AddPlatform(cfg, awsRegion, awsRuntime); err != nil {
9899
return err
99100
}
100101

101-
if err := lambda.AddHandler(cfg); err != nil {
102-
return err
103-
}
104-
105-
if err := config.SaveConfig(cfg); err != nil {
106-
return fmt.Errorf("failed to save config: %w", err)
107-
}
108-
109-
fmt.Println("Added AWS Lambda platform.")
102+
fmt.Println("Added AWS platform.")
110103
return nil
111104
}
112105

113-
func addGCPCloudRun(cmd *cobra.Command, args []string) error {
106+
func addGCP(cmd *cobra.Command, args []string) error {
114107
cfg, err := config.LoadConfig()
115108
if err != nil {
116109
return fmt.Errorf("failed to load config: %w", err)
@@ -136,9 +129,9 @@ func addGCPCloudRun(cmd *cobra.Command, args []string) error {
136129
}
137130

138131
if gcpRuntime == "" {
139-
runtimes := getGCPRuntimes(cfg.Language)
132+
runtimes := getGCPCloudRunRuntimes(cfg.Language)
140133
if len(runtimes) == 0 {
141-
return fmt.Errorf("CLI doesn't support the specified language yet for GCP Cloud Run: %s", cfg.Language)
134+
return fmt.Errorf("CLI doesn't support the specified language yet for GCP: %s", cfg.Language)
142135
}
143136

144137
runtimeQ := &survey.Select{
@@ -150,36 +143,16 @@ func addGCPCloudRun(cmd *cobra.Command, args []string) error {
150143
}
151144
}
152145

153-
if err := cloudrun.AddConfig(cfg, gcpRegion, gcpProjectId, gcpRuntime); err != nil {
146+
if err := gcp.AddPlatform(cfg, gcpRegion, gcpRuntime, gcpProjectId); err != nil {
154147
return err
155148
}
156149

157-
if err := cloudrun.AddHandler(cfg); err != nil {
158-
return err
159-
}
160-
161-
if err := config.SaveConfig(cfg); err != nil {
162-
return fmt.Errorf("failed to save config: %w", err)
163-
}
164-
165150
return nil
166151
}
167152

168153
func listPlatforms() error {
169-
cfg, err := config.LoadConfig()
170-
if err != nil {
171-
return fmt.Errorf("failed to load config: %w", err)
172-
}
173-
174-
var platforms []string
175-
176-
if cfg.AWSLambda != nil {
177-
platforms = append(platforms, "aws-lambda")
178-
}
179-
if cfg.GCPCloudRun != nil {
180-
platforms = append(platforms, "gcp-cloudrun")
181-
}
182154

155+
platforms := infra.ListPlatforms()
183156
if len(platforms) == 0 {
184157
fmt.Println("No platforms configured.")
185158
} else {
@@ -192,7 +165,7 @@ func listPlatforms() error {
192165
return nil
193166
}
194167

195-
func getAWSRuntimes(language lang.Language) []string {
168+
func getAWSLambdaRuntimes(language lang.Language) []string {
196169
switch language {
197170
case lang.Python:
198171
return []string{"python3.8", "python3.9", "python3.10", "python3.11", "python3.12"}
@@ -203,7 +176,7 @@ func getAWSRuntimes(language lang.Language) []string {
203176
}
204177
}
205178

206-
func getGCPRuntimes(language lang.Language) []string {
179+
func getGCPCloudRunRuntimes(language lang.Language) []string {
207180
switch language {
208181
case lang.Python:
209182
return []string{"python37", "python38", "python39", "python310", "python311", "python312"}

docs/assets/demo.tape

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ Require echo
6262
Set Shell "bash"
6363
Set FontSize 20
6464
Set Width 1600
65-
Set Height 500
65+
Set Height 800
6666

6767
Sleep 1s
6868
Type@.15s "upify init" Sleep 1s Enter

docs/commands.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@ upify init
1818
Add platform support to your project.
1919

2020
```bash
21-
upify platform add aws-lambda
22-
upify platform add gcp-cloudrun
21+
upify platform add aws
22+
upify platform add gcp
2323
```
2424

2525
## deploy
2626
Deploy your application to the specified platform.
2727

2828
```bash
29-
upify deploy aws-lambda
30-
upify deploy gcp-cloudrun
29+
upify deploy aws
30+
upify deploy gcp
3131
```
3232

3333
## Flags

docs/configuration.md

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,7 @@ entrypoint: main.py
1919
app_var: app
2020
```
2121
22-
## AWS Lambda Configuration
23-
```yaml
24-
aws-lambda:
25-
region: us-east-1
26-
role_name: lambda-role
27-
runtime: python3.12 | nodejs20.x
28-
```
29-
30-
## GCP Cloud Run Configuration
31-
```yaml
32-
gcp-cloudrun:
33-
region: us-central1
34-
project_id: your-project-id
35-
runtime: python312 | nodejs20
36-
```
37-
38-
## Reference
22+
### Reference
3923
4024
| Field | Description |
4125
|-------|-------------|
@@ -44,4 +28,13 @@ gcp-cloudrun:
4428
| language | Programming language |
4529
| package_manager | Package management tool |
4630
| entrypoint | Main application file |
47-
| app_var | App variable name in entrypoint |
31+
| app_var | App variable name in entrypoint |
32+
33+
# Terraform
34+
35+
Upify leverages Terraform for infrastructure management, terraform files are written to:
36+
37+
- `.upify/environments`
38+
- `.upify/modules`
39+
40+
At this time Upify only supports a single `prod` environment, but in the future we will add support for additional environments

docs/getting-started.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ upify init
4141

4242
4. Add cloud platform:
4343
```bash
44-
upify platform add aws-lambda
44+
upify platform add aws
4545
```
4646

4747
5. Deploy:
4848
```bash
49-
upify deploy aws-lambda
49+
upify deploy aws
5050
```

docs/wrappers.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ from main import app
1818

1919
handler = None
2020

21-
if os.getenv("UPIFY_DEPLOY_PLATFORM") == "aws-lambda":
21+
if os.getenv("UPIFY_DEPLOY_PLATFORM") == "aws":
2222
from apig_wsgi import make_lambda_handler
2323
handler = make_lambda_handler(app)
24-
if os.getenv("UPIFY_DEPLOY_PLATFORM") == "gcp-cloudrun":
24+
if os.getenv("UPIFY_DEPLOY_PLATFORM") == "gcp":
2525
import functions_framework
2626

2727
@functions_framework.http

0 commit comments

Comments
 (0)