/
endpoints.go
71 lines (61 loc) · 2.05 KB
/
endpoints.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
package claim
import (
"fmt"
"github.com/Aptomi/aptomi/cmd/aptomictl/io"
"github.com/Aptomi/aptomi/pkg/api"
"github.com/Aptomi/aptomi/pkg/client/rest"
"github.com/Aptomi/aptomi/pkg/client/rest/http"
"github.com/Aptomi/aptomi/pkg/config"
"github.com/Aptomi/aptomi/pkg/lang"
"github.com/Aptomi/aptomi/pkg/util"
"github.com/gosuri/uitable"
"github.com/spf13/cobra"
)
func newEndpointsCommand(cfg *config.Client) *cobra.Command {
paths := make([]string, 0)
cmd := &cobra.Command{
Use: "endpoints",
Short: "claim endpoints",
Long: "claim endpoints",
Run: func(cmd *cobra.Command, args []string) {
allObjects, err := io.ReadLangObjects(paths)
if err != nil {
panic(fmt.Sprintf("error while reading policy files: %s", err))
}
claims := []*lang.Claim{}
for _, obj := range allObjects {
if d, ok := obj.(*lang.Claim); ok {
claims = append(claims, d)
}
}
printEndpoints(cfg, claims)
},
}
cmd.Flags().StringSliceVarP(&paths, "policyPaths", "f", make([]string, 0), "Paths to files/dirs with claim files")
return cmd
}
// TODO: ideally we should use common.Format() here to support writing into json and yaml, but it doesn't blend too well with maps and sorted keys
func printEndpoints(cfg *config.Client, claims []*lang.Claim) {
result, errAPI := rest.New(cfg, http.NewClient(cfg)).Claim().Status(claims, api.ClaimQueryDeploymentStatusOnly)
if errAPI != nil {
panic(fmt.Sprintf("error while requesting claim status: %s", errAPI))
}
table := uitable.New()
table.MaxColWidth = 120
table.Wrap = true
table.AddRow("CLAIM", "COMPONENT", "ENDPOINT TYPE", "ENDPOINT URL")
for _, dKey := range util.GetSortedStringKeys(result.Status) {
first := true
for _, cKey := range util.GetSortedStringKeys(result.Status[dKey].Endpoints) {
for _, eType := range util.GetSortedStringKeys(result.Status[dKey].Endpoints[cKey]) {
claimKeyStr := ""
if first {
claimKeyStr = dKey
first = false
}
table.AddRow(claimKeyStr, cKey, eType, result.Status[dKey].Endpoints[cKey][eType])
}
}
}
fmt.Println(table)
}