pulumi/cmd/logout.go
Matt Ellis 94d11884f8 Fix login/logout issue against non api.pulumi.com clouds
Pat ran into a weird error when trying to do some development agains
the testing cloud:

```
$ pulumi logout
$ pulumi login --cloud-url [test-cloud-url]
Logged into [test-cloud-url]
$ pulumi stack ls
Enter your Pulumi access token from https://pulumi.com/account:
```

In his case, we did not have `current` set in our credentials.json
file (likely due to him calling `pulumi logout` at some point) but we
did have stored credentials for that cloud. When he logged in the CLI
noticed we could reuse the stored credentials but did not update the
the current setting to set the current cloud.

While investigating, I also noticed that `logout` did not always do
the right thing when you were logged into a different backend than
pulumi.com
2018-04-27 15:41:50 -07:00

47 lines
1.2 KiB
Go

// Copyright 2016-2018, Pulumi Corporation. All rights reserved.
package cmd
import (
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/pulumi/pulumi/pkg/backend/cloud"
"github.com/pulumi/pulumi/pkg/backend/local"
"github.com/pulumi/pulumi/pkg/util/cmdutil"
"github.com/pulumi/pulumi/pkg/workspace"
)
func newLogoutCmd() *cobra.Command {
var cloudURL string
cmd := &cobra.Command{
Use: "logout",
Short: "Log out of the Pulumi Cloud",
Long: "Log out of the Pulumi Cloud. Deletes stored credentials on the local machine.",
Args: cmdutil.NoArgs,
Run: cmdutil.RunFunc(func(cmd *cobra.Command, args []string) error {
if cloudURL == "" {
creds, err := workspace.GetStoredCredentials()
if err != nil {
return errors.Wrap(err, "could not determine current cloud")
}
cloudURL = creds.Current
}
if local.IsLocalBackendURL(cloudURL) {
return local.New(cmdutil.Diag(), cloudURL).Logout()
}
b, err := cloud.New(cmdutil.Diag(), cloudURL)
if err != nil {
return err
}
return b.Logout()
}),
}
cmd.PersistentFlags().StringVarP(&cloudURL, "cloud-url", "c", "",
"A cloud URL to log out of (defaults to current cloud)")
return cmd
}