-
Notifications
You must be signed in to change notification settings - Fork 7
/
describe.go
144 lines (125 loc) · 4.99 KB
/
describe.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
139
140
141
142
143
144
package command
import (
"fmt"
"net/http"
"net/url"
"os"
"github.com/lunarway/release-manager/cmd/hamctl/command/completion"
httpinternal "github.com/lunarway/release-manager/internal/http"
"github.com/spf13/cobra"
)
func NewDescribe(client *httpinternal.Client, service *string) *cobra.Command {
var command = &cobra.Command{
Use: "describe",
Short: "Show details of resources controlled by the release manager.",
Example: `Get details about available artifacts for a service:
hamctl describe artifact --service product
Get details about the current release of product in the dev environment:
hamctl describe release --service product --env dev`,
}
command.AddCommand(newDescribeRelease(client, service))
command.AddCommand(newDescribeArtifact(client, service))
return command
}
var describeReleaseDefaultTemplate = `Service: {{ .Artifact.Service }}
{{ if ne (len .Artifact.Namespace) 0 -}}
Namespace: {{ .Artifact.Namespace }}
{{ end -}}
Environment: {{ .Environment }}
Released at: {{ .ReleasedAt.Format "2006-01-02 15:04:03" }}
Released by: {{ .ReleasedByName }} ({{ .ReleasedByEmail }})
Commit: {{ .Artifact.Application.URL }}
`
func newDescribeRelease(client *httpinternal.Client, service *string) *cobra.Command {
var environment, namespace, template string
var command = &cobra.Command{
Use: "release",
Short: "Show details about a release.",
Example: `Get details about the current release of product in the dev environment:
hamctl describe release --service product --env dev
Format the output with a custom template:
hamctl describe release --service product --env dev --template '{{ .Service }}'`,
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.DescribeReleaseResponse
params := url.Values{}
if namespace != "" {
params.Add("namespace", namespace)
}
path, err := client.URLWithQuery(fmt.Sprintf("describe/release/%s/%s", *service, environment), params)
if err != nil {
return err
}
err = client.Do(http.MethodGet, path, nil, &resp)
if err != nil {
return err
}
if len(template) == 0 {
template = describeReleaseDefaultTemplate
}
err = templateOutput(os.Stdout, "describeRelease", template, resp)
if err != nil {
return err
}
return nil
},
}
command.Flags().StringVarP(&environment, "env", "e", "", "environment to promote to (required)")
completion.FlagAnnotation(command, "env", "__hamctl_get_environments")
// errors are skipped here as the only case they can occour are if thee flag
// does not exist on the command.
//nolint:errcheck
command.MarkFlagRequired("env")
command.Flags().StringVarP(&namespace, "namespace", "n", "", "namespace the service is deployed to (defaults to env)")
completion.FlagAnnotation(command, "namespace", "__hamctl_get_namespaces")
command.Flags().StringVarP(&template, "template", "", "", "template string to format the output. The format is Go templates (http://golang.org/pkg/text/template/#pkg-overview). Available data structure is an 'http.DescribeReleaseResponse' struct.")
return command
}
var describeArtifactDefaultTemplate = `Latest artifacts for service: {{ .Service }}
{{ rightPad "Date" 21 }}{{ rightPad "Artifact" 30 }}Message
{{ range $k, $v := .Artifacts -}}
{{ rightPad (.CI.End.Format "2006-01-02 15:04:03") 21 }}{{ rightPad .ID 30 }}{{ .Application.Message }}
{{ end -}}
`
func newDescribeArtifact(client *httpinternal.Client, service *string) *cobra.Command {
var count int
var template string
var command = &cobra.Command{
Use: "artifact",
Short: "Show details about an artifact.",
Example: `Get details about available artifacts for a service:
hamctl describe artifact --service product
Get details about the latest 5 artifacts for a service:
hamctl describe artifact --service product --count 5
Format the output with a custom template:
hamctl describe artifact --service product --template '{{ .Service }}'`,
RunE: func(c *cobra.Command, args []string) error {
var resp httpinternal.DescribeArtifactResponse
params := url.Values{}
params.Add("count", fmt.Sprintf("%d", count))
path, err := client.URLWithQuery(fmt.Sprintf("describe/artifact/%s", *service), params)
if err != nil {
return err
}
err = client.Do(http.MethodGet, path, nil, &resp)
if err != nil {
return err
}
if len(template) == 0 {
template = describeArtifactDefaultTemplate
}
err = templateOutput(os.Stdout, "describeArtifact", template, resp)
if err != nil {
return err
}
return nil
},
}
command.Flags().IntVar(&count, "count", 1, "Number of artifacts to return sorted by latest")
command.Flags().StringVarP(&template, "template", "", "", "template string to format the output. The format is Go templates (http://golang.org/pkg/text/template/#pkg-overview). Available data structure is an 'http.DescribeArtifactResponse' struct.")
return command
}