2017-06-26 23:46:34 +02:00
|
|
|
// Copyright 2016-2017, Pulumi Corporation. All rights reserved.
|
2017-02-26 22:06:33 +01:00
|
|
|
|
2017-09-22 04:18:21 +02:00
|
|
|
package cmd
|
2017-02-26 22:06:33 +01:00
|
|
|
|
|
|
|
import (
|
2017-10-02 22:34:33 +02:00
|
|
|
"fmt"
|
2017-10-10 02:47:55 +02:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2017-10-02 22:34:33 +02:00
|
|
|
"strconv"
|
|
|
|
|
2017-10-10 02:47:55 +02:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/pulumi/pulumi/pkg/encoding"
|
|
|
|
"github.com/pulumi/pulumi/pkg/workspace"
|
|
|
|
|
2017-10-03 01:37:12 +02:00
|
|
|
"github.com/pulumi/pulumi/pkg/tokens"
|
|
|
|
|
2017-02-26 22:06:33 +01:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
2017-09-22 04:18:21 +02:00
|
|
|
"github.com/pulumi/pulumi/pkg/util/cmdutil"
|
2017-02-26 22:06:33 +01:00
|
|
|
)
|
|
|
|
|
2017-10-16 21:04:35 +02:00
|
|
|
func newStackLsCmd() *cobra.Command {
|
2017-02-26 22:06:33 +01:00
|
|
|
return &cobra.Command{
|
2017-09-23 00:29:24 +02:00
|
|
|
Use: "ls",
|
2017-10-16 21:04:35 +02:00
|
|
|
Short: "List all known stacks",
|
2017-04-12 20:12:25 +02:00
|
|
|
Run: cmdutil.RunFunc(func(cmd *cobra.Command, args []string) error {
|
2017-10-16 21:04:35 +02:00
|
|
|
currentStack, err := getCurrentStack()
|
2017-10-03 01:37:12 +02:00
|
|
|
if err != nil {
|
2017-10-16 21:04:35 +02:00
|
|
|
// If we couldn't figure out the current stack, just don't print the '*' later
|
2017-10-03 01:37:12 +02:00
|
|
|
// on instead of failing.
|
2017-10-16 21:04:35 +02:00
|
|
|
currentStack = tokens.QName("")
|
2017-10-03 01:37:12 +02:00
|
|
|
}
|
|
|
|
|
2017-10-16 21:04:35 +02:00
|
|
|
stacks, err := getStacks()
|
2017-10-02 22:34:33 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("%-20s %-48s %-12s\n", "NAME", "LAST UPDATE", "RESOURCE COUNT")
|
2017-10-16 21:04:35 +02:00
|
|
|
for _, stack := range stacks {
|
|
|
|
_, snapshot, err := getStack(stack)
|
2017-10-10 02:47:55 +02:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-10-02 22:34:33 +02:00
|
|
|
// Now print out the name, last deployment time (if any), and resources (if any).
|
|
|
|
lastDeploy := "n/a"
|
|
|
|
resourceCount := "n/a"
|
2017-10-10 02:47:55 +02:00
|
|
|
if snapshot != nil {
|
2017-10-10 03:02:27 +02:00
|
|
|
lastDeploy = snapshot.Time.String()
|
2017-10-10 02:47:55 +02:00
|
|
|
resourceCount = strconv.Itoa(len(snapshot.Resources))
|
2017-10-02 22:34:33 +02:00
|
|
|
}
|
2017-10-16 21:04:35 +02:00
|
|
|
display := stack.String()
|
|
|
|
if stack == currentStack {
|
|
|
|
display += "*" // fancify the current stack.
|
2017-10-02 22:34:33 +02:00
|
|
|
}
|
|
|
|
fmt.Printf("%-20s %-48s %-12s\n", display, lastDeploy, resourceCount)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2017-08-23 01:23:50 +02:00
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
2017-10-10 02:47:55 +02:00
|
|
|
|
2017-10-16 21:04:35 +02:00
|
|
|
func getStacks() ([]tokens.QName, error) {
|
|
|
|
var stacks []tokens.QName
|
2017-10-10 02:47:55 +02:00
|
|
|
|
2017-10-16 21:04:35 +02:00
|
|
|
// Read the stack directory.
|
|
|
|
path := workspace.StackPath("")
|
2017-10-10 02:47:55 +02:00
|
|
|
files, err := ioutil.ReadDir(path)
|
|
|
|
if err != nil && !os.IsNotExist(err) {
|
2017-10-16 21:04:35 +02:00
|
|
|
return nil, errors.Errorf("could not read stacks: %v", err)
|
2017-10-10 02:47:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, file := range files {
|
|
|
|
// Ignore directories.
|
|
|
|
if file.IsDir() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip files without valid extensions (e.g., *.bak files).
|
2017-10-16 21:04:35 +02:00
|
|
|
stackfn := file.Name()
|
|
|
|
ext := filepath.Ext(stackfn)
|
2017-10-10 02:47:55 +02:00
|
|
|
if _, has := encoding.Marshalers[ext]; !has {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-10-16 21:04:35 +02:00
|
|
|
// Read in this stack's information.
|
|
|
|
name := tokens.QName(stackfn[:len(stackfn)-len(ext)])
|
|
|
|
_, _, err := getStack(name)
|
2017-10-10 02:47:55 +02:00
|
|
|
if err != nil {
|
2017-10-16 21:04:35 +02:00
|
|
|
continue // failure reading the stack information.
|
2017-10-10 02:47:55 +02:00
|
|
|
}
|
|
|
|
|
2017-10-16 21:04:35 +02:00
|
|
|
stacks = append(stacks, name)
|
2017-10-10 02:47:55 +02:00
|
|
|
}
|
|
|
|
|
2017-10-16 21:04:35 +02:00
|
|
|
return stacks, nil
|
2017-10-10 02:47:55 +02:00
|
|
|
}
|