diff --git a/repl/accounts.go b/repl/accounts.go index 1a6fb887b3addcbd511cc2a6a09d430bed2640e8..da8a3d818439598a8dd8032032ad8a2fbfff9b00 100644 --- a/repl/accounts.go +++ b/repl/accounts.go @@ -1,7 +1,10 @@ package repl import ( + "bufio" + "bytes" "fmt" + "net/http" "strings" "git.lyda.ie/kevin/bulletin/ask" @@ -206,7 +209,7 @@ func ActionSSHAdd(cmd *dclish.Command) error { fmt.Printf("ERROR: Failed to read ssh key (%s).\n", err) return nil } - key.Add(login, sshkey) + key.Add(u.Login, sshkey) fmt.Println("Key is added.") return nil } @@ -276,3 +279,39 @@ func ActionSSHDelete(cmd *dclish.Command) error { fmt.Println("Key deleted.") return nil } + +// ActionSSHFetch handles the `SSH FETCH` command. +func ActionSSHFetch(cmd *dclish.Command) error { + sites := map[string]string{ + "codeberg": "https://codeberg.org/%s.keys", + "gitlab": "https://gitlab.com/%s.keys", + "github": "https://github.com/%s.keys", + } + siteTemplate := sites[strings.ToLower(cmd.Args[0])] + if siteTemplate == "" { + fmt.Println("ERROR: site nickname unknown.") + return nil + } + site := fmt.Sprintf(siteTemplate, cmd.Args[1]) + resp, err := http.Get(site) + if err != nil { + fmt.Printf("ERROR: Failed to fetch ssh keys (%s).\n", err) + return nil + } + scanner := bufio.NewScanner(resp.Body) + keys := 0 + for scanner.Scan() { + keyline := string(bytes.TrimSpace(scanner.Bytes())) + key.Add(this.User.Login, keyline) + keys++ + } + switch keys { + case 0: + fmt.Println("No keys added.") + case 1: + fmt.Println("Key is added.") + default: + fmt.Printf("%d keys added.\n", keys) + } + return nil +} diff --git a/repl/command.go b/repl/command.go index 79ec62eb640e5084df58972a9481a9d653b3b050..06026a50c7d8167165104a94fe25f28967873874 100644 --- a/repl/command.go +++ b/repl/command.go @@ -1079,7 +1079,7 @@ The following actions are available: The following commands are available: - ADD DELETE LIST + ADD DELETE FETCH LIST `, Action: ActionSSH, Commands: dclish.Commands{ @@ -1106,6 +1106,26 @@ The following commands are available: Action: ActionSSHDelete, MaxArgs: 1, }, + "FETCH": { + Description: ` + Fetches ssh keys for a user from a site like github. + + The following sites are supported: + + +---------------+--------------------------------+ + | Site Nickname | Site key url | + +---------------+--------------------------------+ + | codeberg | https://codeberg.org/USER.keys | + | gitlab | https://gitlab.com/USER.keys | + | github | https://github.com/USER.keys | + +---------------+--------------------------------+ + + Format: + SSH FETCH site-nickname site-username`, + Action: ActionSSHFetch, + MinArgs: 2, + MaxArgs: 2, + }, "LIST": { Description: ` Prints a list of ssh keys for a user. Only an admin can list ssh keys for another user. @@ -1525,7 +1545,7 @@ have done this.`, Specifies to display the latest message that was read by the user(s) for the specified foldername. If the foldername is not specified, the - selected folder will be used.`, + selected folder will be used.`, OptArg: true, }, "/SINCE": {