When logging out of the active host while other hosts exist, the stale ActiveHost reference caused DefaultHost() to silently fall through to the first alphabetical host instead of treating the user as logged out. Signed-off-by: Bryan Frimin <bryan@getprobo.com>
96 lines
2.4 KiB
Go
96 lines
2.4 KiB
Go
// Copyright (c) 2026 Probo Inc <hello@getprobo.com>.
|
|
//
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
// copyright notice and this permission notice appear in all copies.
|
|
//
|
|
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
// PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
package logout
|
|
|
|
import (
|
|
"fmt"
|
|
"maps"
|
|
"slices"
|
|
|
|
"github.com/charmbracelet/huh"
|
|
"github.com/spf13/cobra"
|
|
"go.probo.inc/probo/pkg/cmd/cmdutil"
|
|
)
|
|
|
|
func NewCmdLogout(f *cmdutil.Factory) *cobra.Command {
|
|
var flagHost string
|
|
|
|
cmd := &cobra.Command{
|
|
Use: "logout",
|
|
Short: "Log out of a Probo host",
|
|
Example: ` # Log out of the active host
|
|
proboctl auth logout
|
|
|
|
# Log out of a specific host
|
|
proboctl auth logout --hostname app.getprobo.com`,
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
cfg, err := f.Config()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if flagHost == "" {
|
|
host, _, err := cfg.DefaultHost()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
hosts := slices.Sorted(maps.Keys(cfg.Hosts))
|
|
if len(hosts) > 1 && f.IOStreams.IsInteractive() {
|
|
options := make([]huh.Option[string], len(hosts))
|
|
for i, h := range hosts {
|
|
options[i] = huh.NewOption(h, h)
|
|
}
|
|
err := huh.NewSelect[string]().
|
|
Title("Select a host to log out of").
|
|
Options(options...).
|
|
Value(&flagHost).
|
|
Run()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
} else {
|
|
flagHost = host
|
|
}
|
|
}
|
|
|
|
if _, ok := cfg.Hosts[flagHost]; !ok {
|
|
return fmt.Errorf("not logged in to %s", flagHost)
|
|
}
|
|
|
|
delete(cfg.Hosts, flagHost)
|
|
if cfg.ActiveHost == flagHost {
|
|
cfg.ActiveHost = ""
|
|
}
|
|
|
|
if err := cfg.Save(); err != nil {
|
|
return err
|
|
}
|
|
|
|
_, _ = fmt.Fprintf(
|
|
f.IOStreams.ErrOut,
|
|
"Logged out of %s\n",
|
|
flagHost,
|
|
)
|
|
|
|
return nil
|
|
},
|
|
}
|
|
|
|
cmd.Flags().StringVar(&flagHost, "hostname", "", "Probo hostname to log out of")
|
|
|
|
return cmd
|
|
}
|