mirror of
https://github.com/coder/coder.git
synced 2026-06-03 04:58:23 +00:00
bddb808b25
Fixes all our Go file imports to match the preferred spec that we've _mostly_ been using. For example: ``` import ( "context" "time" "github.com/prometheus/client_golang/prometheus" "golang.org/x/xerrors" "gopkg.in/natefinch/lumberjack.v2" "cdr.dev/slog/v3" "github.com/coder/coder/v2/codersdk/agentsdk" "github.com/coder/serpent" ) ``` 3 groups: standard library, 3rd partly libs, Coder libs. This PR makes the change across the codebase. The PR in the stack above modifies our formatting to maintain this state of affairs, and is a separate PR so it's possible to review that one in detail.
73 lines
2.0 KiB
Go
73 lines
2.0 KiB
Go
package cli
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
"github.com/coder/coder/v2/cli/cliui"
|
|
"github.com/coder/coder/v2/codersdk"
|
|
"github.com/coder/pretty"
|
|
"github.com/coder/serpent"
|
|
)
|
|
|
|
func (r *RootCmd) publickey() *serpent.Command {
|
|
var reset bool
|
|
cmd := &serpent.Command{
|
|
Use: "publickey",
|
|
Aliases: []string{"pubkey"},
|
|
Short: "Output your Coder public key used for Git operations",
|
|
Handler: func(inv *serpent.Invocation) error {
|
|
client, err := r.InitClient(inv)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if reset {
|
|
// Confirm prompt if using --reset. We don't want to accidentally
|
|
// reset our public key.
|
|
_, err := cliui.Prompt(inv, cliui.PromptOptions{
|
|
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.
|
|
_, err = client.RegenerateGitSSHKey(inv.Context(), codersdk.Me)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
key, err := client.GitSSHKey(inv.Context(), codersdk.Me)
|
|
if err != nil {
|
|
return xerrors.Errorf("create codersdk client: %w", err)
|
|
}
|
|
|
|
cliui.Info(inv.Stdout,
|
|
"This is your public key for using "+pretty.Sprint(cliui.DefaultStyles.Field, "git")+" in "+
|
|
"Coder. All clones with SSH will be authenticated automatically 🪄.",
|
|
)
|
|
cliui.Info(inv.Stdout, pretty.Sprint(cliui.DefaultStyles.Code, strings.TrimSpace(key.PublicKey))+"\n")
|
|
cliui.Info(inv.Stdout, "Add to GitHub and GitLab:")
|
|
cliui.Info(inv.Stdout, "> https://github.com/settings/ssh/new")
|
|
cliui.Info(inv.Stdout, "> https://gitlab.com/-/profile/keys")
|
|
|
|
return nil
|
|
},
|
|
}
|
|
|
|
cmd.Options = serpent.OptionSet{
|
|
{
|
|
Flag: "reset",
|
|
Description: "Regenerate your public key. This will require updating the key on any services it's registered with.",
|
|
Value: serpent.BoolOf(&reset),
|
|
},
|
|
cliui.SkipPromptOption(),
|
|
}
|
|
|
|
return cmd
|
|
}
|