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)
| 15 | |
| 16 | // Uploads the provided SSH key. Returns true if the key was uploaded, false if it was not. |
| 17 | func 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. |
| 59 | func SSHSigningKeyUpload(httpClient *http.Client, hostname string, keyFile io.Reader, title string) (bool, error) { |
no test coverage detected