-
Notifications
You must be signed in to change notification settings - Fork 7
/
status.go
87 lines (79 loc) · 2.41 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
package command
import (
"fmt"
"net/http"
"net/url"
"time"
"github.com/lunarway/color"
"github.com/lunarway/release-manager/cmd/hamctl/command/completion"
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",
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
}
if !someManaged(resp.Dev, resp.Staging, resp.Prod) {
if resp.DefaultNamespaces {
fmt.Printf("Using default namespaces. ")
}
fmt.Printf("Are you setting the right namespace?\n")
}
fmt.Printf("Status for service: %s\n", *service)
fmt.Printf("\n")
color.Green("dev:\n")
printStatus(resp.Dev)
color.Green("staging:\n")
printStatus(resp.Staging)
color.Green("prod:\n")
printStatus(resp.Prod)
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
}
// someManaged returns true if any of provided environments are managed.
func someManaged(envs ...*httpinternal.Environment) bool {
for _, e := range envs {
if e != nil && e.Tag != "" {
return true
}
}
return false
}
func Time(epoch int64) time.Time {
return time.Unix(epoch/1000, 0)
}
func printStatus(e *httpinternal.Environment) {
if e == nil {
return
}
if e.Tag == "" {
fmt.Printf(" Not managed by the release-manager\n\n")
return
}
fmt.Printf(" Tag: %s\n Author: %s\n Committer: %s\n Message: %s\n Date: %s\n Link: %s\n Vulnerabilities: %d high, %d medium, %d low\n\n", e.Tag, e.Author, e.Committer, e.Message, Time(e.Date), e.BuildUrl, e.HighVulnerabilities, e.MediumVulnerabilities, e.LowVulnerabilities)
}