-
Notifications
You must be signed in to change notification settings - Fork 24
/
import_keys.go
108 lines (90 loc) · 2.73 KB
/
import_keys.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
package scmintegrations
import (
"os"
"strings"
"github.com/olekukonko/tablewriter"
"gopkg.in/errgo.v1"
"github.com/Scalingo/cli/config"
"github.com/Scalingo/cli/io"
"github.com/Scalingo/go-scalingo"
)
func ImportKeys(id string) error {
var keys []scalingo.Key
c, err := config.ScalingoAuthClient()
if err != nil {
return errgo.Notef(err, "fail to get Scalingo client")
}
integration, err := c.SCMIntegrationsShow(id)
if err != nil {
return errgo.Notef(err, "not linked SCM integration or unknown SCM integration")
}
importedKeys, err := c.SCMIntegrationsImportKeys(id)
if err != nil {
return errgo.Notef(err, "fail to import keys")
}
nbrKeys := len(importedKeys)
if nbrKeys == 0 {
alreadyImportedKeys, err := keysContainsName(c, integration.SCMType.Str())
if err != nil {
return errgo.Notef(err, "fail to list already imported keys")
}
alreadyImportedKeysLength := len(alreadyImportedKeys)
pluralKey := ""
if alreadyImportedKeysLength > 1 {
pluralKey = "s"
}
io.Statusf("0 key imported from %s.\n", scalingo.SCMTypeDisplay[integration.SCMType])
if alreadyImportedKeysLength == 0 {
io.Infof("No public key is available in your %s account\n", scalingo.SCMTypeDisplay[integration.SCMType])
return nil
}
io.Info()
io.Statusf(
"%d key%s have already been imported from %s:\n",
alreadyImportedKeysLength, pluralKey, scalingo.SCMTypeDisplay[integration.SCMType],
)
keys = alreadyImportedKeys
} else {
keys = importedKeys
}
t := tablewriter.NewWriter(os.Stdout)
t.SetColWidth(60)
t.SetHeader([]string{"Name", "Content"})
for _, k := range keys {
t.Append([]string{k.Name, k.Content[0:20] + "..." + k.Content[len(k.Content)-30:]})
}
t.Render()
if nbrKeys != 0 {
pluralKey := ""
if nbrKeys > 1 {
pluralKey = "s"
}
io.Statusf("%d key%s have been imported from %s.\n", nbrKeys, pluralKey, scalingo.SCMTypeDisplay[integration.SCMType])
}
return nil
}
func keysContainsName(c *scalingo.Client, name string) ([]scalingo.Key, error) {
keys, err := c.KeysList()
if err != nil {
return nil, errgo.Notef(err, "fail to get keys")
}
var keysAlreadyImported []scalingo.Key
for _, k := range keys {
if !strings.Contains(k.Name, name+"-") {
continue
}
switch scalingo.SCMType(name) {
case scalingo.SCMGitlabType:
if !strings.Contains(k.Name, scalingo.SCMGitlabSelfHostedType.Str()) {
keysAlreadyImported = append(keysAlreadyImported, k)
}
case scalingo.SCMGithubType:
if !strings.Contains(k.Name, scalingo.SCMGithubEnterpriseType.Str()) {
keysAlreadyImported = append(keysAlreadyImported, k)
}
case scalingo.SCMGithubEnterpriseType, scalingo.SCMGitlabSelfHostedType:
keysAlreadyImported = append(keysAlreadyImported, k)
}
}
return keysAlreadyImported, nil
}