aboutsummaryrefslogtreecommitdiff
path: root/internal/utils
diff options
context:
space:
mode:
Diffstat (limited to 'internal/utils')
-rw-r--r--internal/utils/ssh_helpers.go38
-rw-r--r--internal/utils/utils.go34
2 files changed, 38 insertions, 34 deletions
diff --git a/internal/utils/ssh_helpers.go b/internal/utils/ssh_helpers.go
new file mode 100644
index 0000000..9ffcf0a
--- /dev/null
+++ b/internal/utils/ssh_helpers.go
@@ -0,0 +1,38 @@
+package utils
+
+import (
+ "fmt"
+ "io"
+ "net/http"
+ "strings"
+)
+
+func GetPubKeyFromService(username, service string) ([]byte, error) {
+ // TODO: Read custom services from config
+
+ services := map[string][2]string{
+ "github" : [2]string{"https://github.com/", ".keys"},
+ "gitlab" : [2]string{"https://gitlab.com/", ".keys"},
+ "codeberg": [2]string{"https://codeberg.org", ".keys"},
+ }
+
+ url := strings.TrimSuffix(services[service][0], "/") + "/" + username + services[service][1]
+
+ resp, err := http.Get(url)
+ if err != nil {
+ return nil, fmt.Errorf("failed to fetch key from %s: %w", service, err)
+ }
+ defer resp.Body.Close()
+
+ if resp.StatusCode != http.StatusOK {
+ return nil, fmt.Errorf("%s returned status %d", service, resp.StatusCode)
+ }
+
+ body, err := io.ReadAll(resp.Body)
+ if err != nil {
+ return nil, fmt.Errorf("failed to read response from %s: %w", service, err)
+ }
+
+ return body, nil
+}
+
diff --git a/internal/utils/utils.go b/internal/utils/utils.go
index a43b268..cea00a3 100644
--- a/internal/utils/utils.go
+++ b/internal/utils/utils.go
@@ -3,8 +3,6 @@ package utils
import (
"crypto/rand"
"fmt"
- "io"
- "net/http"
"os"
"os/signal"
"strconv"
@@ -102,35 +100,3 @@ func CheckFileAuthMode(notePath string) (crypt.AuthMode, error) {
return headers.Auth, nil
}
-
-func GetPubKeyFromService(username, service string) ([]byte, error) {
- // TODO: Read custom services from config
-
- var url string
-
- switch service {
- case "github":
- url = "https://github.com/" + username + ".keys"
- case "gitlab":
- url = "https://gitlab.com/" + username + ".keys"
- default:
- return nil, fmt.Errorf("unknown service: %s", service)
- }
-
- resp, err := http.Get(url)
- if err != nil {
- return nil, fmt.Errorf("failed to fetch key from %s: %w", service, err)
- }
- defer resp.Body.Close()
-
- if resp.StatusCode != http.StatusOK {
- return nil, fmt.Errorf("%s returned status %d", service, resp.StatusCode)
- }
-
- body, err := io.ReadAll(resp.Body)
- if err != nil {
- return nil, fmt.Errorf("failed to read response from %s: %w", service, err)
- }
-
- return body, nil
-}