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

Function SSHSigningKeyUpload

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

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

Callers 1

runAddFunction · 0.85

Calls 5

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

Tested by

no test coverage detected