MCPcopy Create free account
hub / github.com/cli/cli / SSHKeyUpload

Function SSHKeyUpload

pkg/cmd/ssh-key/add/http.go:18–60  ·  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

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

Callers 2

sshKeyUploadFunction · 0.92
runAddFunction · 0.85

Calls 5

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

Tested by

no test coverage detected