-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathservices.go
More file actions
104 lines (85 loc) · 2.49 KB
/
services.go
File metadata and controls
104 lines (85 loc) · 2.49 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
package cmd
import (
"fmt"
"regexp"
"strconv"
"github.com/olekukonko/tablewriter"
"github.com/drycc/controller-sdk-go/services"
)
// ServicesList lists extra services for the app
func (d *DryccCmd) ServicesList(appID string) error {
s, appID, err := load(d.ConfigFile, appID)
if err != nil {
return err
}
services, err := services.List(s.Client, appID)
if d.checkAPICompatibility(s.Client, err) != nil {
return err
}
d.Printf("=== %s Services\n", appID)
if len(services) > 0 {
table := tablewriter.NewWriter(d.WOut)
table.SetHeader([]string{"Type", "Name", "Port", "Protocol", "TargetPort"})
for _, service := range services {
for _, port := range service.Ports {
table.Append([]string{service.ProcfileType, port.Name, fmt.Sprint(port.Port), port.Protocol, fmt.Sprint(port.TargetPort)})
}
}
table.SetAutoMergeCellsByColumnIndex([]int{0})
table.SetRowLine(true)
table.Render()
}
return nil
}
// ServicesAdd adds a service to an app.
func (d *DryccCmd) ServicesAdd(appID, procfileType string, ports string, protocol string) error {
s, appID, err := load(d.ConfigFile, appID)
if err != nil {
return err
}
portArray, err := parsePorts(ports)
if err != nil {
return err
}
d.Printf("Adding %s (%d) to %s... ", procfileType, portArray[0], appID)
quit := progress(d.WOut)
err = services.New(s.Client, appID, procfileType, portArray[0], protocol, portArray[1])
quit <- true
<-quit
if d.checkAPICompatibility(s.Client, err) != nil {
return err
}
d.Println("done")
return nil
}
// ServicesRemove removes a service for procfileType registered with an app.
func (d *DryccCmd) ServicesRemove(appID, procfileType string, protocol string, port int) error {
s, appID, err := load(d.ConfigFile, appID)
if err != nil {
return err
}
d.Printf("Removing %s from %s... ", procfileType, appID)
quit := progress(d.WOut)
err = services.Delete(s.Client, appID, procfileType, protocol, port)
quit <- true
<-quit
if d.checkAPICompatibility(s.Client, err) != nil {
return err
}
d.Println("done")
return nil
}
// parsePorts transfer ports to [2]int
func parsePorts(param string) ([2]int, error) {
var ports [2]int
var err error
regex := regexp.MustCompile(`(^[1-9]+[0-9_]+):([1-9]+[0-9_]+)$`)
if regex.MatchString(param) {
captures := regex.FindStringSubmatch(param)
ports[0], _ = strconv.Atoi(captures[1])
ports[1], _ = strconv.Atoi(captures[2])
} else {
err = fmt.Errorf("'%s' does not match the pattern 'port:targatPort', ex: 80:8000", param)
}
return ports, err
}