MCPcopy
hub / github.com/cli/cli / SSHKeyUpload

Function SSHKeyUpload

pkg/cmd/ssh-key/add/http.go:17–56  ·  view source on GitHub ↗

Uploads the provided SSH key. Returns true if the key was uploaded, false if it was not.

(httpClient *http.Client, hostname string, keyFile io.Reader, title string)

Source from the content-addressed store, hash-verified

15
16// Uploads the provided SSH key. Returns true if the key was uploaded, false if it was not.
17func SSHKeyUpload(httpClient *http.Client, hostname string, keyFile io.Reader, title string) (bool, error) {
18 url := ghinstance.RESTPrefix(hostname) + "user/keys"
19
20 keyBytes, err := io.ReadAll(keyFile)
21 if err != nil {
22 return false, err
23 }
24
25 fullUserKey := string(keyBytes)
26 splitKey := strings.Fields(fullUserKey)
27 if len(splitKey) < 2 {
28 return false, errors.New("provided key is not in a valid format")
29 }
30
31 keyToCompare := splitKey[0] + " " + splitKey[1]
32
33 keys, err := shared.UserKeys(httpClient, hostname, "")
34 if err != nil {
35 return false, err
36 }
37
38 for _, k := range keys {
39 if k.Key == keyToCompare {
40 return false, nil
41 }
42 }
43
44 payload := map[string]string{
45 "title": title,
46 "key": fullUserKey,
47 }
48
49 err = keyUpload(httpClient, url, payload)
50
51 if err != nil {
52 return false, err
53 }
54
55 return true, nil
56}
57
58// Uploads the provided SSH Signing key. Returns true if the key was uploaded, false if it was not.
59func SSHSigningKeyUpload(httpClient *http.Client, hostname string, keyFile io.Reader, title string) (bool, error) {

Callers 2

sshKeyUploadFunction · 0.92
runAddFunction · 0.85

Calls 4

RESTPrefixFunction · 0.92
UserKeysFunction · 0.92
keyUploadFunction · 0.85
FieldsMethod · 0.65

Tested by

no test coverage detected