2018-05-22 21:43:36 +02:00
|
|
|
// Copyright 2016-2018, Pulumi Corporation.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2017-12-08 21:14:14 +01:00
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2018-04-27 23:47:16 +02:00
|
|
|
"github.com/pkg/errors"
|
2017-12-08 21:14:14 +01:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
2018-09-04 19:44:25 +02:00
|
|
|
"github.com/pulumi/pulumi/pkg/backend"
|
2018-09-04 21:38:58 +02:00
|
|
|
"github.com/pulumi/pulumi/pkg/backend/filestate"
|
|
|
|
"github.com/pulumi/pulumi/pkg/backend/httpstate"
|
2017-12-08 21:14:14 +01:00
|
|
|
"github.com/pulumi/pulumi/pkg/util/cmdutil"
|
2018-04-27 23:47:16 +02:00
|
|
|
"github.com/pulumi/pulumi/pkg/workspace"
|
2017-12-08 21:14:14 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func newLogoutCmd() *cobra.Command {
|
|
|
|
var cloudURL string
|
2018-09-04 19:44:25 +02:00
|
|
|
var localMode bool
|
|
|
|
|
2017-12-08 21:14:14 +01:00
|
|
|
cmd := &cobra.Command{
|
2018-09-04 19:44:25 +02:00
|
|
|
Use: "logout <url>",
|
|
|
|
Short: "Log out of the Pulumi service",
|
|
|
|
Long: "Log out of the Pulumi service.\n" +
|
|
|
|
"\n" +
|
|
|
|
"This command deletes stored credentials on the local machine for a single login.\n" +
|
|
|
|
"\n" +
|
|
|
|
"Because you may be logged into multiple backends simultaneously, you can optionally pass\n" +
|
|
|
|
"a specific URL argument, formatted just as you logged in, to log out of a specific one.\n" +
|
|
|
|
"If none is supplied, you will be logged out of the default cloud.",
|
|
|
|
Args: cmdutil.MaximumNArgs(1),
|
2017-12-08 21:14:14 +01:00
|
|
|
Run: cmdutil.RunFunc(func(cmd *cobra.Command, args []string) error {
|
2018-09-04 19:44:25 +02:00
|
|
|
// If a <cloud> was specified as an argument, use it.
|
|
|
|
if len(args) > 0 {
|
|
|
|
if cloudURL != "" {
|
|
|
|
return errors.New("only one of --cloud-url or argument URL may be specified, not both")
|
|
|
|
}
|
|
|
|
cloudURL = args[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
// For local mode, store state by default in the user's home directory.
|
|
|
|
if localMode {
|
|
|
|
if cloudURL != "" {
|
|
|
|
return errors.New("a URL may not be specified when --local mode is enabled")
|
|
|
|
}
|
|
|
|
cloudURL = "file://~"
|
|
|
|
}
|
|
|
|
|
2018-04-27 23:47:16 +02:00
|
|
|
if cloudURL == "" {
|
|
|
|
creds, err := workspace.GetStoredCredentials()
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "could not determine current cloud")
|
|
|
|
}
|
|
|
|
|
|
|
|
cloudURL = creds.Current
|
|
|
|
}
|
|
|
|
|
2018-09-04 19:44:25 +02:00
|
|
|
var be backend.Backend
|
|
|
|
var err error
|
2018-09-04 21:38:58 +02:00
|
|
|
if filestate.IsLocalBackendURL(cloudURL) {
|
|
|
|
be, err = filestate.New(cmdutil.Diag(), cloudURL)
|
2018-09-04 19:44:25 +02:00
|
|
|
} else {
|
2018-09-04 21:38:58 +02:00
|
|
|
be, err = httpstate.New(cmdutil.Diag(), cloudURL)
|
2017-12-08 21:14:14 +01:00
|
|
|
}
|
2018-04-05 00:31:01 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-09-04 19:44:25 +02:00
|
|
|
return be.Logout()
|
2017-12-08 21:14:14 +01:00
|
|
|
}),
|
|
|
|
}
|
2018-09-04 19:44:25 +02:00
|
|
|
|
2018-04-05 00:31:01 +02:00
|
|
|
cmd.PersistentFlags().StringVarP(&cloudURL, "cloud-url", "c", "",
|
|
|
|
"A cloud URL to log out of (defaults to current cloud)")
|
2018-09-04 19:44:25 +02:00
|
|
|
cmd.PersistentFlags().BoolVarP(&localMode, "local", "l", false,
|
|
|
|
"Log out of using local mode")
|
|
|
|
|
2017-12-08 21:14:14 +01:00
|
|
|
return cmd
|
|
|
|
}
|