/
deploy.go
104 lines (88 loc) · 2.45 KB
/
deploy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package cmd
import (
"errors"
"fmt"
"os"
"github.com/spf13/cobra"
"github.com/operatorai/kettle-cli/clouds"
"github.com/operatorai/kettle-cli/config"
"github.com/operatorai/kettle-cli/settings"
"github.com/operatorai/kettle-cli/templates"
)
var deployCmd = &cobra.Command{
Use: "deploy",
Short: "Ship a project you have created from a kettle template",
Long: `🚢 The kettle CLI tool can automatically deploy
your projects to your cloud provider.`,
Args: validateDeployArgs,
RunE: runDeploy,
}
func init() {
rootCmd.AddCommand(deployCmd)
}
func validateDeployArgs(cmd *cobra.Command, args []string) error {
// Validate that args exist
if len(args) == 0 {
return errors.New("please specify a path or directory name")
}
return nil
}
// runDeploy creates or updates a cloud function
func runDeploy(cmd *cobra.Command, args []string) error {
// Construct the path we want to deploy from
deploymentPath, err := templates.GetProject(args)
if err != nil {
return formatError(err)
}
// Read the template's config
templateConfig, err := config.ReadConfig(deploymentPath)
if err != nil {
return formatError(err)
}
// Read global settings
cloudSettings, err := settings.ReadSettings()
if err != nil {
return formatError(err)
}
// Get the cloud provider & service type
cloudProvider, err := clouds.GetCloudProvider(templateConfig.Config.CloudProvider)
if err != nil {
return formatError(err)
}
if err := cloudProvider.Setup(cloudSettings); err != nil {
return formatError(err)
}
service, err := cloudProvider.GetService(templateConfig.Config.DeploymentType)
if err != nil {
return formatError(err)
}
// Store the current directory before changing away from it
rootDir, err := os.Getwd()
if err != nil {
return formatError(err)
}
// Change to the directory where the function to deploy is implemented
// and run the deployment command
os.Chdir(deploymentPath)
defer func() {
// Return to the original root directory
os.Chdir(rootDir)
}()
// Deploy
if err := service.Deploy(deploymentPath, templateConfig, cloudSettings); err != nil {
return formatError(err)
}
// Write the settings & config back (they may have been changed)
if err := settings.WriteSettings(cloudSettings); err != nil {
if settings.DebugMode {
fmt.Println(err.Error())
}
}
if err := config.WriteConfig(deploymentPath, templateConfig); err != nil {
if settings.DebugMode {
fmt.Println(err.Error())
}
}
fmt.Println("✅ Deployed!")
return nil
}