-
Notifications
You must be signed in to change notification settings - Fork 5
/
cloud_providers.go
120 lines (87 loc) · 3.24 KB
/
cloud_providers.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package cmd
import (
"github.com/eleven-sh/cli/internal/globals"
"github.com/eleven-sh/eleven/features"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
type cloudProviderFeature[T interface{}] interface {
Execute(input T) error
}
type cloudProvider struct {
LongName string
ShortName string
GlobalName globals.CloudProvider
DefaultInstanceType string
ExampleInstanceType string
AddFlagsToBaseCmd func(*cobra.Command)
ProvideInitFeature func() cloudProviderFeature[features.InitInput]
ProvideEditFeature func() cloudProviderFeature[features.EditInput]
ProvideRemoveFeature func() cloudProviderFeature[features.RemoveInput]
ProvideServeFeature func() cloudProviderFeature[features.ServeInput]
ProvideUnserveFeature func() cloudProviderFeature[features.UnserveInput]
UninstallSuccessMessage string
UninstallAlreadyUninstalledMessage string
ProvideUninstallFeature func() cloudProviderFeature[features.UninstallInput]
}
func addCloudProviderCmdToRootCmd(
rootCmd *cobra.Command,
provider *cloudProvider,
) *cobra.Command {
var cloudProviderCmd = &cobra.Command{
Use: provider.ShortName,
Short: "Use Eleven on " + provider.LongName,
Long: `Use Eleven on ` + provider.LongName + `.
To begin, create your first sandbox using the command:
eleven ` + provider.ShortName + ` init <sandbox_name>
Once created, you may want to connect your editor to it using the command:
eleven ` + provider.ShortName + ` edit <sandbox_name>
If you don't plan to use this sandbox again, you could remove it using the command:
eleven ` + provider.ShortName + ` remove <sandbox_name>`,
Example: ` eleven ` + provider.ShortName + ` init eleven-api --instance-type ` + provider.ExampleInstanceType + `
eleven ` + provider.ShortName + ` edit eleven-api
eleven ` + provider.ShortName + ` remove eleven-api`,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
ensureUserIsLoggedIn()
populateCurrentCloudProviderGlobals(provider.GlobalName, cmd)
},
}
provider.AddFlagsToBaseCmd(cloudProviderCmd)
rootCmd.AddCommand(cloudProviderCmd)
return cloudProviderCmd
}
func populateCurrentCloudProviderGlobals(
cloudProvider globals.CloudProvider,
cloudProviderCmd *cobra.Command,
) {
globals.CurrentCloudProvider = cloudProvider
globals.CurrentCloudProviderArgs = ""
// command ran without subcommand -> help displayed -> no args to parse
if !cloudProviderCmd.HasParent() {
return
}
cloudProviderCmd.Parent().Flags().VisitAll(func(f *pflag.Flag) {
if !f.Changed {
return
}
if len(globals.CurrentCloudProviderArgs) > 0 {
globals.CurrentCloudProviderArgs += " "
}
globals.CurrentCloudProviderArgs += "--" + f.Name + " " + f.Value.String()
})
}
func init() {
availableCloudProviders := []*cloudProvider{
getAWSCloudProvider(),
getHetznerCloudProvider(),
}
for _, cloudProvider := range availableCloudProviders {
cloudProviderCmd := addCloudProviderCmdToRootCmd(rootCmd, cloudProvider)
loadInitCmd(cloudProviderCmd, cloudProvider)
loadEditCmd(cloudProviderCmd, cloudProvider)
loadServeCmd(cloudProviderCmd, cloudProvider)
loadUnserveCmd(cloudProviderCmd, cloudProvider)
loadRemoveCmd(cloudProviderCmd, cloudProvider)
loadUninstallCmd(cloudProviderCmd, cloudProvider)
}
}