forked from a2call/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.go
84 lines (75 loc) · 2.34 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package deploykeys
import (
"crypto/sha256"
"encoding/base64"
"fmt"
"strings"
"github.com/Sirupsen/logrus"
"github.com/daticahealth/cli/commands/services"
"github.com/daticahealth/cli/models"
"github.com/olekukonko/tablewriter"
)
func CmdList(svcName string, id IDeployKeys, is services.IServices) error {
service, err := is.RetrieveByLabel(svcName)
if err != nil {
return err
}
if service == nil {
return fmt.Errorf("Could not find a service with the label \"%s\". You can list services with the \"datica services list\" command.", svcName)
}
if service.Type != "code" {
return fmt.Errorf("You can only list deploy keys for code services, not %s services", service.Type)
}
keys, err := id.List(service.ID)
if err != nil {
return err
}
if keys == nil || len(*keys) == 0 {
logrus.Println("No deploy-keys found")
return nil
}
invalidKeys := map[string]string{}
data := [][]string{{"NAME", "TYPE", "FINGERPRINT"}}
for _, key := range *keys {
if key.Type != "ssh" {
continue
}
s, err := id.ParsePublicKey([]byte(key.Key))
if err != nil {
invalidKeys[key.Name] = err.Error()
continue
}
h := sha256.New()
h.Write(s.Marshal())
fingerprint := base64.StdEncoding.EncodeToString(h.Sum(nil))
data = append(data, []string{key.Name, key.Type, fmt.Sprintf("SHA256:%s", strings.TrimRight(fingerprint, "="))})
}
table := tablewriter.NewWriter(logrus.StandardLogger().Out)
table.SetBorder(false)
table.SetRowLine(false)
table.SetCenterSeparator("")
table.SetColumnSeparator("")
table.SetRowSeparator("")
table.AppendBulk(data)
table.Render()
if len(invalidKeys) > 0 {
logrus.Println("\nInvalid Keys:")
for keyName, reason := range invalidKeys {
logrus.Printf("%s: %s", keyName, reason)
}
}
return nil
}
func (d *SDeployKeys) List(svcID string) (*[]models.DeployKey, error) {
headers := d.Settings.HTTPManager.GetHeaders(d.Settings.SessionToken, d.Settings.Version, d.Settings.Pod, d.Settings.UsersID)
resp, statusCode, err := d.Settings.HTTPManager.Get(nil, fmt.Sprintf("%s%s/environments/%s/services/%s/ssh_keys", d.Settings.PaasHost, d.Settings.PaasHostVersion, d.Settings.EnvironmentID, svcID), headers)
if err != nil {
return nil, err
}
var deployKeys []models.DeployKey
err = d.Settings.HTTPManager.ConvertResp(resp, statusCode, &deployKeys)
if err != nil {
return nil, err
}
return &deployKeys, nil
}