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

Function SSHSigningKeyUpload

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

60
61// Uploads the provided SSH Signing key. Returns true if the key was uploaded, false if it was not.
62func SSHSigningKeyUpload(httpClient *http.Client, hostname string, keyFile io.Reader, title string) (bool, error) {
63 u, err := safeurl.JoinPath("user", "ssh_signing_keys")
64 if err != nil {
65 return false, err
66 }
67
68 keyBytes, err := io.ReadAll(keyFile)
69 if err != nil {
70 return false, err
71 }
72
73 fullUserKey := string(keyBytes)
74 splitKey := strings.Fields(fullUserKey)
75 if len(splitKey) < 2 {
76 return false, errors.New("provided key is not in a valid format")
77 }
78
79 keyToCompare := splitKey[0] + " " + splitKey[1]
80
81 keys, err := shared.UserSigningKeys(httpClient, hostname, "")
82 if err != nil {
83 return false, err
84 }
85
86 for _, k := range keys {
87 if k.Key == keyToCompare {
88 return false, nil
89 }
90 }
91
92 payload := map[string]string{
93 "title": title,
94 "key": fullUserKey,
95 }
96
97 err = keyUpload(httpClient, hostname, u, payload)
98
99 if err != nil {
100 return false, err
101 }
102
103 return true, nil
104}
105
106func keyUpload(httpClient *http.Client, hostname string, u safeurl.SafeURL, payload map[string]string) error {
107 payloadBytes, err := json.Marshal(payload)

Callers 1

runAddFunction · 0.85

Calls 4

JoinPathFunction · 0.92
UserSigningKeysFunction · 0.92
keyUploadFunction · 0.85
FieldsMethod · 0.65

Tested by

no test coverage detected