-
Notifications
You must be signed in to change notification settings - Fork 7
/
list.go
69 lines (64 loc) · 1.86 KB
/
list.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
package policy
import (
"fmt"
"net/http"
"net/url"
httpinternal "github.com/lunarway/release-manager/internal/http"
"github.com/spf13/cobra"
)
func NewList(client *httpinternal.Client, service *string) *cobra.Command {
var command = &cobra.Command{
Use: "list",
Short: "List current policies",
RunE: func(c *cobra.Command, args []string) error {
var resp httpinternal.ListPoliciesResponse
params := url.Values{}
params.Add("service", *service)
path, err := client.URLWithQuery(path, params)
if err != nil {
return err
}
err = client.Do(http.MethodGet, path, nil, &resp)
if err != nil {
responseErr, ok := err.(*httpinternal.ErrorResponse)
if !ok || responseErr.Status != http.StatusNotFound {
return err
}
fmt.Printf("No policies exist for service\n")
return nil
}
fmt.Printf("Policies for service %s\n", resp.Service)
fmt.Println()
if len(resp.AutoReleases) != 0 {
fmt.Printf("Auto-releases:\n")
fmt.Println()
maxBranchLen := maxLen(resp.AutoReleases, func(p httpinternal.AutoReleasePolicy) string {
return p.Branch
})
maxEnvLen := maxLen(resp.AutoReleases, func(p httpinternal.AutoReleasePolicy) string {
return p.Environment
})
maxIDLen := maxLen(resp.AutoReleases, func(p httpinternal.AutoReleasePolicy) string {
return p.ID
})
formatString := fmt.Sprintf("%%-%ds %%-%ds %%-%ds\n", maxBranchLen, maxEnvLen, maxIDLen)
fmt.Printf(formatString, "BRANCH", "ENV", "ID")
for _, p := range resp.AutoReleases {
fmt.Printf(formatString, p.Branch, p.Environment, p.ID)
}
}
return nil
},
}
return command
}
func maxLen(policies []httpinternal.AutoReleasePolicy, f func(httpinternal.AutoReleasePolicy) string) int {
longest := 0
for _, p := range policies {
str := f(p)
if len(str) > longest {
longest = len(str)
}
}
return longest
}