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