pulumi/cmd/stack_ls.go
Matt Ellis d3240fdc64 Require pulumi login before commands that need a backend
This change does three major things:

1. Removes the ability to be logged into multiple clouds at the same
time. Previously, we supported being logged into multiple clouds at
the same time and the CLI would fan out requests and join responses
when needed. In general, this was only useful for Pulumi employees
that wanted run against multiple copies of the service (say production
and staging) but overall was very confusing (for example in the old
world a stack with the same identity could appear twice (since it was
in two backends) which the CLI didn't handle very well).

2. Stops treating the "local" backend as a special thing, from the
point of view of the CLI. Previouly we'd always connect to the local
backend and merge that data with whatever was in clouds we were
connected to. We had gestures like `--local` in `pulumi stack init`
that meant "use the local mode". Instead, to use the local mode now
you run `pulumi login --cloud-url local://` and then you are logged in
the local backend. Since you can only ever be logged into a single
backend, we can remove the `--local` and `--remote` flags from `pulumi
stack init`, it just now requires you to be logged in and creates a
stack in whatever back end you were logged into. When logging into the
local backend, you are not prompted for an access key.

3. Prompt for login in places where you have to log in, if you are not
already logged in.
2018-04-05 10:19:41 -07:00

108 lines
2.9 KiB
Go

// Copyright 2016-2018, Pulumi Corporation. All rights reserved.
package cmd
import (
"fmt"
"sort"
"strconv"
"github.com/dustin/go-humanize"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/pulumi/pulumi/pkg/backend"
"github.com/pulumi/pulumi/pkg/backend/cloud"
"github.com/pulumi/pulumi/pkg/backend/state"
"github.com/pulumi/pulumi/pkg/tokens"
"github.com/pulumi/pulumi/pkg/util/cmdutil"
"github.com/pulumi/pulumi/pkg/workspace"
)
func newStackLsCmd() *cobra.Command {
return &cobra.Command{
Use: "ls",
Short: "List all known stacks",
Args: cmdutil.NoArgs,
Run: cmdutil.RunFunc(func(cmd *cobra.Command, args []string) error {
// Ensure we are in a project; if not, we will fail.
proj, err := workspace.DetectProjectPath()
if err != nil {
return errors.Wrapf(err, "could not detect current project")
} else if proj == "" {
return errors.New("no Pulumi.yaml found; please run this command in a project directory")
}
// Get a list of all known backends, as we will query them all.
b, err := currentBackend()
if err != nil {
return err
}
// Get the current stack so we can print a '*' next to it.
var current tokens.QName
if s, _ := state.CurrentStack(b); s != nil {
// If we couldn't figure out the current stack, just don't print the '*' later on instead of failing.
current = s.Name()
}
// Now produce a list of summaries, and enumerate them sorted by name.
var result error
var stackNames []string
stacks := make(map[string]backend.Stack)
bs, err := b.ListStacks()
if err != nil {
return err
}
for _, stack := range bs {
name := string(stack.Name())
stacks[name] = stack
stackNames = append(stackNames, name)
}
sort.Strings(stackNames)
// Devote 48 characters to the name width, unless there is a longer name.
maxname := 48
for _, name := range stackNames {
if len(name) > maxname {
maxname = len(name)
}
}
fmt.Printf("%-"+strconv.Itoa(maxname)+"s %-24s %-18s %-25s\n",
"NAME", "LAST UPDATE", "RESOURCE COUNT", "CLOUD")
for _, name := range stackNames {
// Mark the name as current '*' if we've selected it.
stack := stacks[name]
if name == string(current) {
name += "*"
}
// Get last deployment info, provided that it exists.
none := "n/a"
lastUpdate := none
resourceCount := none
if snap := stack.Snapshot(); snap != nil {
if t := snap.Manifest.Time; !t.IsZero() {
lastUpdate = humanize.Time(t)
}
resourceCount = strconv.Itoa(len(snap.Resources))
}
// Print out the cloud URL.
var cloudInfo string
if cs, ok := stack.(cloud.Stack); ok {
cloudInfo = fmt.Sprintf("%s:%s/%s", cs.CloudURL(), cs.OrgName(), cs.CloudName())
} else {
cloudInfo = none
}
fmt.Printf("%-"+strconv.Itoa(maxname)+"s %-24s %-18s %-25s\n",
name, lastUpdate, resourceCount, cloudInfo)
}
return result
}),
}
}