-
Notifications
You must be signed in to change notification settings - Fork 7
/
status.go
138 lines (124 loc) · 3.88 KB
/
status.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package command
import (
"io"
"net/http"
"net/url"
"os"
"time"
"github.com/lunarway/release-manager/cmd/hamctl/command/completion"
"github.com/lunarway/release-manager/cmd/hamctl/template"
httpinternal "github.com/lunarway/release-manager/internal/http"
"github.com/spf13/cobra"
)
func NewStatus(client *httpinternal.Client, service *string) *cobra.Command {
var namespace string
var command = &cobra.Command{
Use: "status",
Short: "List the status of the environments",
Args: cobra.ExactArgs(0),
PreRun: func(c *cobra.Command, args []string) {
defaultShuttleString(shuttleSpecFromFile, &namespace, func(s *shuttleSpec) string {
return s.Vars.K8S.Namespace
})
},
RunE: func(c *cobra.Command, args []string) error {
var resp httpinternal.StatusResponse
params := url.Values{}
params.Add("service", *service)
if namespace != "" {
params.Add("namespace", namespace)
}
path, err := client.URLWithQuery("status", params)
if err != nil {
return err
}
err = client.Do(http.MethodGet, path, nil, &resp)
if err != nil {
return err
}
err = templateStatus(os.Stdout, mapToStatusData(resp, *service))
if err != nil {
return err
}
return nil
},
}
command.Flags().StringVarP(&namespace, "namespace", "n", "", "namespace the service is deployed to (defaults to env)")
completion.FlagAnnotation(command, "namespace", "__hamctl_get_namespaces")
return command
}
func mapToStatusData(resp httpinternal.StatusResponse, service string) statusData {
var envs []statusDataEnvironment
for _, e := range resp.Environments {
envs = append(envs, mapEnvironment(&e, e.Name))
}
return statusData{
EnvironmentsManaged: someManaged(resp.Environments...),
UsingDefaultNamespaces: resp.DefaultNamespaces,
Service: service,
Environments: envs,
}
}
var statusTemplate = `Status for service {{ .Service }}
{{- if not .EnvironmentsManaged }}
No environments managed by release-manager.
{{ if .UsingDefaultNamespaces -}}
Using environment specific namespace, ie. dev, prod.
{{ end -}}
Are you setting the right namespace?
{{- end -}}
{{- if not (eq (len .Environments) 0) }}
{{/* add 2 as we have ": " added to the environment column */ -}}
{{ $columnWidth := add (maxLength .Environments "Environment") 2 -}}
{{- range .Environments }}
{{ rightPad (printf "%s:" .Environment) $columnWidth }}{{ .Date.Format dateFormat }} {{ .Tag }} by {{ .Committer }}: {{ .CommitMessage }}
{{- end }}
{{- end }}
`
type statusData struct {
EnvironmentsManaged bool
UsingDefaultNamespaces bool
Service string
Environments []statusDataEnvironment
}
type statusDataEnvironment struct {
Environment string
Tag string
Author string
Committer string
CommitMessage string
Date time.Time
BuildURL string
HighVulnerabilities int64
MediumVulnerabilities int64
LowVulnerabilities int64
}
func templateStatus(dest io.Writer, data statusData) error {
return template.Output(dest, "status", statusTemplate, data)
}
func mapEnvironment(env *httpinternal.Environment, name string) statusDataEnvironment {
return statusDataEnvironment{
Environment: name,
Tag: env.Tag,
Author: env.Author,
Committer: env.Committer,
CommitMessage: env.Message,
Date: Time(env.Date),
BuildURL: env.BuildUrl,
HighVulnerabilities: env.HighVulnerabilities,
MediumVulnerabilities: env.MediumVulnerabilities,
LowVulnerabilities: env.LowVulnerabilities,
}
}
// someManaged returns true if any of provided environments are managed.
func someManaged(envs ...httpinternal.Environment) bool {
for _, e := range envs {
if e.Tag != "" {
return true
}
}
return false
}
func Time(epoch int64) time.Time {
return time.Unix(epoch/1000, 0)
}