Skip to content

Commit e60eeff

Browse files
authored
Merge pull request #33 from notmaxx/services_support
feat(workflow-cli): support command services in CLI
2 parents a69dcaf + d66af42 commit e60eeff

5 files changed

Lines changed: 208 additions & 1 deletion

File tree

cmd/cmd.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ type Commander interface {
4444
DomainsList(string, int) error
4545
DomainsAdd(string, string) error
4646
DomainsRemove(string, string) error
47+
ServicesList(string) error
48+
ServicesAdd(string, string, string) error
49+
ServicesRemove(string, string) error
4750
GitRemote(string, string, bool) error
4851
GitRemove(string) error
4952
HealthchecksList(string, string) error

cmd/services.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/teamhephy/pkg/prettyprint"
7+
8+
"github.com/teamhephy/controller-sdk-go/services"
9+
)
10+
11+
// ServicesList lists extra services for the app
12+
func (d *DeisCmd) ServicesList(appID string) error {
13+
s, appID, err := load(d.ConfigFile, appID)
14+
15+
if err != nil {
16+
return err
17+
}
18+
19+
services, err := services.List(s.Client, appID)
20+
if d.checkAPICompatibility(s.Client, err) != nil {
21+
return err
22+
}
23+
24+
d.Printf("=== %s Services\n", appID)
25+
servicesMap := make(map[string]string)
26+
if len(services) > 0 {
27+
for _, service := range services {
28+
servicesMap[service.ProcfileType] = fmt.Sprintf("%v", service.PathPattern)
29+
}
30+
d.Print(prettyprint.PrettyTabs(servicesMap, 5))
31+
}
32+
return nil
33+
}
34+
35+
// ServicesAdd adds a service to an app.
36+
func (d *DeisCmd) ServicesAdd(appID, procfileType string, pathPattern string) error {
37+
s, appID, err := load(d.ConfigFile, appID)
38+
39+
if err != nil {
40+
return err
41+
}
42+
43+
d.Printf("Adding %s (%s) to %s... ", procfileType, pathPattern, appID)
44+
45+
quit := progress(d.WOut)
46+
_, err = services.New(s.Client, appID, procfileType, pathPattern)
47+
quit <- true
48+
<-quit
49+
if d.checkAPICompatibility(s.Client, err) != nil {
50+
return err
51+
}
52+
53+
d.Println("done")
54+
return nil
55+
}
56+
57+
// ServicesRemove removes a service for procfileType registered with an app.
58+
func (d *DeisCmd) ServicesRemove(appID, procfileType string) error {
59+
s, appID, err := load(d.ConfigFile, appID)
60+
61+
if err != nil {
62+
return err
63+
}
64+
65+
d.Printf("Removing %s from %s... ", procfileType, appID)
66+
67+
quit := progress(d.WOut)
68+
err = services.Delete(s.Client, appID, procfileType)
69+
quit <- true
70+
<-quit
71+
if d.checkAPICompatibility(s.Client, err) != nil {
72+
return err
73+
}
74+
75+
d.Println("done")
76+
return nil
77+
}

deis.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ Subcommands, use 'deis help [subcommand]' to learn more::
6969
users manage users
7070
version display client version
7171
whitelist manage whitelisted addresses of an application
72-
timeouts manage pods temrination grace period
72+
services manage services for your applications
73+
timeouts manage pods termination grace period
7374
7475
Shortcut commands, use 'deis shortcuts' to see all::
7576
@@ -121,6 +122,8 @@ Use 'git push deis master' to deploy to an application.
121122
err = parser.Config(argv, &cmdr)
122123
case "domains":
123124
err = parser.Domains(argv, &cmdr)
125+
case "services":
126+
err = parser.Services(argv, &cmdr)
124127
case "git":
125128
err = parser.Git(argv, &cmdr)
126129
case "healthchecks":

glide.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

parser/services.go

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
package parser
2+
3+
import (
4+
"github.com/teamhephy/workflow-cli/cmd"
5+
docopt "github.com/docopt/docopt-go"
6+
)
7+
8+
// Services routes service commands to their specific function.
9+
func Services(argv []string, cmdr cmd.Commander) error {
10+
usage := `
11+
Valid commands for services:
12+
13+
services:add create service for an application
14+
services:list list application services
15+
services:remove remove service from an application
16+
17+
Use 'deis help [command]' to learn more.
18+
`
19+
20+
switch argv[0] {
21+
case "services:add":
22+
return servicesAdd(argv, cmdr)
23+
case "services:list":
24+
return servicesList(argv, cmdr)
25+
case "services:remove":
26+
return servicesRemove(argv, cmdr)
27+
default:
28+
if printHelp(argv, usage) {
29+
return nil
30+
}
31+
32+
if argv[0] == "services" {
33+
argv[0] = "services:list"
34+
return servicesList(argv, cmdr)
35+
}
36+
37+
PrintUsage(cmdr)
38+
return nil
39+
}
40+
}
41+
42+
func servicesAdd(argv []string, cmdr cmd.Commander) error {
43+
usage := `
44+
Creates extra service for an application and binds it to specific route of the main app domain
45+
46+
Usage: deis services:add --type <procfile_type> --route <path_pattern> [options]
47+
48+
Arguments:
49+
<procfile_type>
50+
Procfile type which should handle the request, e.g. webhooks (should be bind to the port PORT).
51+
Only single extra service per Porcfile type could be created
52+
53+
<path_pattern>
54+
Nginx locations where route requests, one or many via comma,
55+
e.g. /webhooks/notify
56+
OR "/webhooks/notify,~ ^/users/[0-9]+/.*/webhooks/notify,/webhooks/rest"
57+
58+
Options:
59+
-a --app=<app>
60+
the uniquely identifiable name for the application.
61+
`
62+
63+
args, err := docopt.Parse(usage, argv, true, "", false, true)
64+
65+
if err != nil {
66+
return err
67+
}
68+
69+
app := safeGetValue(args, "--app")
70+
procfileType := safeGetValue(args, "<procfile_type>")
71+
pathPattern := safeGetValue(args, "<path_pattern>")
72+
73+
return cmdr.ServicesAdd(app, procfileType, pathPattern)
74+
}
75+
76+
func servicesList(argv []string, cmdr cmd.Commander) error {
77+
usage := `
78+
Lists extra services for an application
79+
80+
Usage: deis services:list [options]
81+
82+
Options:
83+
-a --app=<app>
84+
the uniquely identifiable name for the application.
85+
`
86+
87+
args, err := docopt.Parse(usage, argv, true, "", false, true)
88+
89+
if err != nil {
90+
return err
91+
}
92+
93+
app := safeGetValue(args, "--app")
94+
95+
return cmdr.ServicesList(app)
96+
}
97+
98+
func servicesRemove(argv []string, cmdr cmd.Commander) error {
99+
usage := `
100+
Deletes specific extra service for application
101+
102+
Usage: deis services:remove <procfile_type> [options]
103+
104+
Arguments:
105+
<procfile_type>
106+
extra service for procfile type that should be removed
107+
108+
Options:
109+
-a --app=<app>
110+
the uniquely identifiable name for the application.
111+
`
112+
113+
args, err := docopt.Parse(usage, argv, true, "", false, true)
114+
115+
if err != nil {
116+
return err
117+
}
118+
119+
app := safeGetValue(args, "--app")
120+
procfileType := safeGetValue(args, "<procfile_type>")
121+
122+
return cmdr.ServicesRemove(app, procfileType)
123+
}

0 commit comments

Comments
 (0)