MCPcopy Index your code
hub / github.com/cli/cli / SSHSigningKeyUpload

Function SSHSigningKeyUpload

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

Uploads the provided SSH Signing 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

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) {
60 url := ghinstance.RESTPrefix(hostname) + "user/ssh_signing_keys"
61
62 keyBytes, err := io.ReadAll(keyFile)
63 if err != nil {
64 return false, err
65 }
66
67 fullUserKey := string(keyBytes)
68 splitKey := strings.Fields(fullUserKey)
69 if len(splitKey) < 2 {
70 return false, errors.New("provided key is not in a valid format")
71 }
72
73 keyToCompare := splitKey[0] + " " + splitKey[1]
74
75 keys, err := shared.UserSigningKeys(httpClient, hostname, "")
76 if err != nil {
77 return false, err
78 }
79
80 for _, k := range keys {
81 if k.Key == keyToCompare {
82 return false, nil
83 }
84 }
85
86 payload := map[string]string{
87 "title": title,
88 "key": fullUserKey,
89 }
90
91 err = keyUpload(httpClient, url, payload)
92
93 if err != nil {
94 return false, err
95 }
96
97 return true, nil
98}
99
100func keyUpload(httpClient *http.Client, url string, payload map[string]string) error {
101 payloadBytes, err := json.Marshal(payload)

Callers 1

runAddFunction · 0.85

Calls 4

RESTPrefixFunction · 0.92
UserSigningKeysFunction · 0.92
keyUploadFunction · 0.85
FieldsMethod · 0.65

Tested by

no test coverage detected