Add pulumi whoami

It's often helpful to understand the user the CLI thinks you are
logged in as, so let's add a command that does that.

Fixes #1507
This commit is contained in:
Matt Ellis 2018-06-15 08:44:33 -07:00
parent c2b2f3b117
commit 2b471bda70
6 changed files with 67 additions and 1 deletions

View file

@ -46,7 +46,12 @@ func newLoginCmd() *cobra.Command {
return err
}
fmt.Printf("Logged into %s\n", b.Name())
if currentUser, err := b.CurrentUser(); err == nil {
fmt.Printf("Logged into %s as %s\n", b.Name(), currentUser)
} else {
fmt.Printf("Logged into %s\n", b.Name())
}
return nil
}),
}

View file

@ -114,6 +114,7 @@ func NewPulumiCmd() *cobra.Command {
cmd.AddCommand(newStackCmd())
cmd.AddCommand(newUpdateCmd())
cmd.AddCommand(newVersionCmd())
cmd.AddCommand(newWhoAmICmd())
// Less common, and thus hidden, commands:
cmd.AddCommand(newGenBashCompletionCmd(cmd))

50
cmd/whoami.go Normal file
View file

@ -0,0 +1,50 @@
// 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.
package cmd
import (
"fmt"
"github.com/spf13/cobra"
"github.com/pulumi/pulumi/pkg/util/cmdutil"
)
func newWhoAmICmd() *cobra.Command {
cmd := &cobra.Command{
Use: "whoami",
Short: "Display current logged in user",
Long: "Display current logged in user\n" +
"\n" +
"Displays the username of the currently logged in user.",
Args: cmdutil.NoArgs,
Run: cmdutil.RunFunc(func(cmd *cobra.Command, args []string) error {
b, err := currentBackend()
if err != nil {
return err
}
name, err := b.CurrentUser()
if err != nil {
return err
}
fmt.Println(name)
return nil
}),
}
return cmd
}

View file

@ -101,6 +101,8 @@ type Backend interface {
ImportDeployment(ctx context.Context, stackRef StackReference, deployment *apitype.UntypedDeployment) error
// Logout logs you out of the backend and removes any stored credentials.
Logout() error
// Returns the identity of the current user for the backend.
CurrentUser() (string, error)
}
// UpdateOptions is the full set of update options, including backend and engine options.

View file

@ -325,6 +325,10 @@ func (b *cloudBackend) Name() string {
return b.url
}
func (b *cloudBackend) CurrentUser() (string, error) {
return b.client.GetPulumiAccountName(context.Background())
}
func (b *cloudBackend) CloudURL() string { return b.url }
func (b *cloudBackend) ParseStackReference(s string) (backend.StackReference, error) {

View file

@ -410,6 +410,10 @@ func (b *localBackend) Logout() error {
return workspace.DeleteAccessToken(b.url)
}
func (b *localBackend) CurrentUser() (string, error) {
return "", errors.New("the local backend does not support multiple users")
}
func (b *localBackend) getLocalStacks() ([]tokens.QName, error) {
var stacks []tokens.QName