2022-04-07 22:40:27 +00:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
2022-04-25 04:27:45 +00:00
|
|
|
"strings"
|
|
|
|
|
2022-04-07 22:40:27 +00:00
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
2023-08-18 18:55:43 +00:00
|
|
|
"github.com/coder/coder/v2/cli/clibase"
|
|
|
|
"github.com/coder/coder/v2/cli/cliui"
|
|
|
|
"github.com/coder/coder/v2/codersdk"
|
2022-04-07 22:40:27 +00:00
|
|
|
)
|
|
|
|
|
2023-03-23 22:42:20 +00:00
|
|
|
func (r *RootCmd) publickey() *clibase.Cmd {
|
2023-02-19 00:32:09 +00:00
|
|
|
var reset bool
|
2023-03-23 22:42:20 +00:00
|
|
|
client := new(codersdk.Client)
|
|
|
|
cmd := &clibase.Cmd{
|
|
|
|
Use: "publickey",
|
|
|
|
Aliases: []string{"pubkey"},
|
|
|
|
Short: "Output your Coder public key used for Git operations",
|
|
|
|
Middleware: r.InitClient(client),
|
|
|
|
Handler: func(inv *clibase.Invocation) error {
|
2022-05-24 16:53:04 +00:00
|
|
|
if reset {
|
|
|
|
// Confirm prompt if using --reset. We don't want to accidentally
|
|
|
|
// reset our public key.
|
2023-03-23 22:42:20 +00:00
|
|
|
_, err := cliui.Prompt(inv, cliui.PromptOptions{
|
2022-05-24 16:53:04 +00:00
|
|
|
Text: "Confirm regenerate a new sshkey for your workspaces? This will require updating the key " +
|
|
|
|
"on any services it is registered with. This action cannot be reverted.",
|
|
|
|
IsConfirm: true,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset the public key, let the retrieve re-read it.
|
2023-03-23 22:42:20 +00:00
|
|
|
_, err = client.RegenerateGitSSHKey(inv.Context(), codersdk.Me)
|
2022-05-24 16:53:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-23 22:42:20 +00:00
|
|
|
key, err := client.GitSSHKey(inv.Context(), codersdk.Me)
|
2022-04-07 22:40:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("create codersdk client: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-03-23 22:42:20 +00:00
|
|
|
cliui.Infof(inv.Stdout,
|
2023-06-07 05:22:58 +00:00
|
|
|
"This is your public key for using "+cliui.DefaultStyles.Field.Render("git")+" in "+
|
2023-08-13 16:48:11 +00:00
|
|
|
"Coder. All clones with SSH will be authenticated automatically 🪄.",
|
2023-03-23 22:42:20 +00:00
|
|
|
)
|
2023-08-13 16:48:11 +00:00
|
|
|
cliui.Infof(inv.Stdout, cliui.DefaultStyles.Code.Render(strings.TrimSpace(key.PublicKey))+"\n")
|
|
|
|
cliui.Infof(inv.Stdout, "Add to GitHub and GitLab:")
|
|
|
|
cliui.Infof(inv.Stdout, cliui.DefaultStyles.Prompt.String()+"https://github.com/settings/ssh/new")
|
|
|
|
cliui.Infof(inv.Stdout, cliui.DefaultStyles.Prompt.String()+"https://gitlab.com/-/profile/keys")
|
2022-04-07 22:40:27 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
2023-03-23 22:42:20 +00:00
|
|
|
|
|
|
|
cmd.Options = clibase.OptionSet{
|
|
|
|
{
|
|
|
|
Flag: "reset",
|
|
|
|
Description: "Regenerate your public key. This will require updating the key on any services it's registered with.",
|
|
|
|
Value: clibase.BoolOf(&reset),
|
|
|
|
},
|
|
|
|
cliui.SkipPromptOption(),
|
|
|
|
}
|
2022-05-24 16:53:04 +00:00
|
|
|
|
|
|
|
return cmd
|
2022-04-07 22:40:27 +00:00
|
|
|
}
|