Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cli): logged out error for most commands #1130

Merged
merged 4 commits into from
Mar 4, 2022
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
68 changes: 42 additions & 26 deletions internal/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,17 @@ import (
"github.com/infrahq/infra/internal/server"
)

var errorNotLoggedIn = fmt.Errorf("Not logged in. Run 'infra login' before running this command.")
func mustBeLoggedIn(cmd *cobra.Command, args []string) error {
var errNotLoggedIn = fmt.Errorf("Not logged in. Run 'infra login' before running this command.")
kimskimchi marked this conversation as resolved.
Show resolved Hide resolved

config, err := currentHostConfig()
if err != nil {
return err
} else if !config.isLoggedIn() {
kimskimchi marked this conversation as resolved.
Show resolved Hide resolved
return errNotLoggedIn
}
return nil
}

func parseOptions(cmd *cobra.Command, options interface{}, envPrefix string) error {
v := viper.New()
Expand Down Expand Up @@ -208,9 +218,10 @@ func newLogoutCmd() *cobra.Command {

func newListCmd() *cobra.Command {
return &cobra.Command{
Use: "list",
Aliases: []string{"ls"},
Short: "List destinations and your access",
Use: "list",
Aliases: []string{"ls"},
Short: "List destinations and your access",
PersistentPreRunE: mustBeLoggedIn,
kimskimchi marked this conversation as resolved.
Show resolved Hide resolved
RunE: func(cmd *cobra.Command, args []string) error {
return list()
},
Expand All @@ -228,7 +239,8 @@ $ infra use kubernetes.development
# Connect to a Kubernetes namespace
$ infra use kubernetes.development.kube-system
`,
Args: cobra.ExactArgs(1),
Args: cobra.ExactArgs(1),
PersistentPreRunE: mustBeLoggedIn,
RunE: func(cmd *cobra.Command, args []string) error {
name := args[0]

Expand Down Expand Up @@ -264,8 +276,9 @@ $ infra use kubernetes.development.kube-system

func newAccessCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "access",
Short: "Manage access",
Use: "access",
Short: "Manage access",
PersistentPreRunE: mustBeLoggedIn,
}

cmd.AddCommand(newAccessListCmd())
Expand All @@ -277,8 +290,9 @@ func newAccessCmd() *cobra.Command {

func newKeysCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "keys",
Short: "Manage access keys used by machines to authenticate with Infra and call the API",
Use: "keys",
Short: "Manage access keys used by machines to authenticate with Infra and call the API",
PersistentPreRunE: mustBeLoggedIn,
}

cmd.AddCommand(newKeysListCmd())
Expand All @@ -290,8 +304,9 @@ func newKeysCmd() *cobra.Command {

func newDestinationsCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "destinations",
Short: "Connect & manage destinations",
Use: "destinations",
Short: "Connect & manage destinations",
PersistentPreRunE: mustBeLoggedIn,
}

cmd.AddCommand(newDestinationsListCmd())
Expand Down Expand Up @@ -421,8 +436,9 @@ func newEngineCmd() *cobra.Command {

func newTokensCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "tokens",
Short: "Create & manage tokens",
Use: "tokens",
Short: "Create & manage tokens",
PersistentPreRunE: mustBeLoggedIn,
}

cmd.AddCommand(newTokensCreateCmd())
Expand All @@ -432,8 +448,9 @@ func newTokensCmd() *cobra.Command {

func newProvidersCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "providers",
Short: "Add & manage identity providers",
Use: "providers",
Short: "Add & manage identity providers",
PersistentPreRunE: mustBeLoggedIn,
}

cmd.AddCommand(newProvidersListCmd())
Expand All @@ -445,8 +462,9 @@ func newProvidersCmd() *cobra.Command {

func newInfoCmd() *cobra.Command {
return &cobra.Command{
Use: "info",
Short: "Display the info about the current session",
Use: "info",
Short: "Display the info about the current session",
PreRunE: mustBeLoggedIn,
RunE: func(cmd *cobra.Command, args []string) error {
config, err := currentHostConfig()
if err != nil {
Expand All @@ -461,10 +479,6 @@ func newInfoCmd() *cobra.Command {
w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', tabwriter.AlignRight)
defer w.Flush()

if !config.isLoggedIn() {
return errorNotLoggedIn
}

if config.PolymorphicID.IsUser() {
provider, err := client.GetProvider(config.ProviderID)
if err != nil {
Expand Down Expand Up @@ -519,9 +533,10 @@ func newImportCmd() *cobra.Command {
}

cmd := &cobra.Command{
Use: "import FILE",
Short: "Import an Infra server configuration",
Args: cobra.ExactArgs(1),
Use: "import FILE",
Short: "Import an Infra server configuration",
Args: cobra.ExactArgs(1),
PersistentPreRunE: mustBeLoggedIn,
RunE: func(cmd *cobra.Command, args []string) error {
var options importOptions
if err := parseOptions(cmd, &options, "INFRA_IMPORT"); err != nil {
Expand Down Expand Up @@ -555,8 +570,9 @@ func newImportCmd() *cobra.Command {

func newMachinesCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "machines",
Short: "Create & manage machine identities",
Use: "machines",
Short: "Create & manage machine identities",
PersistentPreRunE: mustBeLoggedIn,
}

cmd.AddCommand(newMachinesCreateCmd())
Expand Down