-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathservices.go
More file actions
121 lines (105 loc) · 4.09 KB
/
services.go
File metadata and controls
121 lines (105 loc) · 4.09 KB
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
121
package parser
import (
"strconv"
"github.com/drycc/workflow-cli/internal/commands"
"github.com/drycc/workflow-cli/internal/completion"
"github.com/drycc/workflow-cli/internal/template"
"github.com/drycc/workflow-cli/pkg/i18n"
"github.com/spf13/cobra"
)
// NewServicesCommand creates a command for managing application services.
func NewServicesCommand(cmdr *commands.DryccCmd) *cobra.Command {
cmd := &cobra.Command{
Use: "services",
Short: i18n.T("Manage services for your applications"),
RunE: func(_ *cobra.Command, _ []string) error {
return cmdr.ServicesList(app)
},
}
cmd.PersistentFlags().StringVarP(&app, "app", "a", "", i18n.T("The uniquely identifiable name for the application"))
appCompletion := completion.AppCompletion{ArgsLen: -1, ConfigFile: &cmdr.ConfigFile}
cmd.RegisterFlagCompletionFunc("app", appCompletion.CompletionFunc)
cmd.AddCommand(servicesList(cmdr))
cmd.AddCommand(servicesAdd(cmdr))
cmd.AddCommand(servicesRemove(cmdr))
return cmd
}
func servicesAdd(cmdr *commands.DryccCmd) *cobra.Command {
var flags struct {
port string
protocol string
}
ptsArgsCompletion := completion.PtsArgsCompletion{
PtsCompletion: &completion.PtsCompletion{AppID: &app, ArgsLen: 0, ConfigFile: &cmdr.ConfigFile},
}
cmd := &cobra.Command{
Use: "add <ptype> <port>:<target>",
Args: cobra.ExactArgs(2),
Example: template.CustomExample(
"drycc services add web-new 80:8080",
map[string]string{
"<ptype>": i18n.T(`procfile type which should handle the request, e.g. webhooks (should be bind to the port PORT)
only single extra service per Porcfile type could be created.`),
"<port>": i18n.T("The port that will be exposed by this service"),
"<target>": i18n.T("Number or name of the port to access on the pods targeted by the service"),
},
),
Short: i18n.T("Create service for an application"),
Long: i18n.T("Creates extra service for an application and binds it to specific route of the main app domain"),
ValidArgsFunction: ptsArgsCompletion.CompletionFunc,
RunE: func(_ *cobra.Command, args []string) error {
ptype := args[0]
portTarget := args[1]
return cmdr.ServicesAdd(app, ptype, portTarget, flags.protocol)
},
}
cmd.Flags().StringVar(&flags.protocol, "protocol", "TCP", i18n.T("The IP protocol for this port. Supports TCP, UDP, and SCTP"))
serviceProtocolCompletion := completion.ServiceProtocolCompletion{ArgsLen: -1, ConfigFile: &cmdr.ConfigFile}
cmd.RegisterFlagCompletionFunc("protocol", serviceProtocolCompletion.CompletionFunc)
return cmd
}
func servicesList(cmdr *commands.DryccCmd) *cobra.Command {
cmd := &cobra.Command{
Use: "list",
Example: "drycc services list",
Short: i18n.T("List application services"),
RunE: func(_ *cobra.Command, _ []string) error {
return cmdr.ServicesList(app)
},
}
return cmd
}
func servicesRemove(cmdr *commands.DryccCmd) *cobra.Command {
var flags struct {
app string
port int
protocol string
}
ServiceCompletion := completion.ServiceCompletion{AppID: &app, ArgsLen: 0, ConfigFile: &cmdr.ConfigFile}
cmd := &cobra.Command{
Use: "remove <ptype> <port>",
Args: cobra.ExactArgs(2),
Example: template.CustomExample(
"drycc services remove web-new 80",
map[string]string{
"<ptype>": i18n.T("procfile type which should handle the request, e.g. webhooks"),
"<port>": i18n.T("The port exposed by this service"),
},
),
Short: i18n.T("Remove service from an application"),
ValidArgsFunction: ServiceCompletion.CompletionFunc,
RunE: func(_ *cobra.Command, args []string) error {
ptype := args[0]
port, err := strconv.Atoi(args[1])
if err != nil {
return err
}
flags.port = port
return cmdr.ServicesRemove(app, ptype, flags.protocol, port)
},
}
cmd.Flags().StringVar(&flags.protocol, "protocol", "TCP", i18n.T("The IP protocol for this port. Supports TCP, UDP, and SCTP"))
serviceProtocolCompletion := completion.ServiceProtocolCompletion{ArgsLen: -1, ConfigFile: &cmdr.ConfigFile}
cmd.RegisterFlagCompletionFunc("protocol", serviceProtocolCompletion.CompletionFunc)
return cmd
}