set_username.go

 1package cmd
 2
 3import (
 4	"github.com/charmbracelet/soft-serve/pkg/backend"
 5	"github.com/charmbracelet/soft-serve/pkg/sshutils"
 6	"github.com/spf13/cobra"
 7)
 8
 9// SetUsernameCommand returns a command that sets the user's username.
10func SetUsernameCommand() *cobra.Command {
11	cmd := &cobra.Command{
12		Use:   "set-username USERNAME",
13		Short: "Set your username",
14		Args:  cobra.ExactArgs(1),
15		RunE: func(cmd *cobra.Command, args []string) error {
16			ctx := cmd.Context()
17			be := backend.FromContext(ctx)
18			pk := sshutils.PublicKeyFromContext(ctx)
19			user, err := be.UserByPublicKey(ctx, pk)
20			if err != nil {
21				return err //nolint:wrapcheck
22			}
23
24			return be.SetUsername(ctx, user.Username(), args[0])
25		},
26	}
27
28	return cmd
29}