pulumi/cmd/history.go
Joe Duffy 776a76dffd
Make some stack-related CLI improvements (#947)
This change includes a handful of stack-related CLI formatting
improvements that I've been noodling on in the background for a while,
based on things that tend to trip up demos and the inner loop workflow.

This includes:

* If `pulumi stack select` is run by itself, use an interactive
  CLI menu to let the user select an existing stack, or choose to
  create a new one.  This looks as follows

      $ pulumi stack select
      Please choose a stack, or choose to create a new one:
        abcdef
        babblabblabble
      > currentlyselected
        defcon
        <create a new stack>

  and is navigated in the usual way (key up, down, enter).

* If a stack name is passed that does not exist, prompt the user
  to ask whether s/he wants to create one on-demand.  This hooks
  interesting moments in time, like `pulumi stack select foo`,
  and cuts down on the need to run additional commands.

* If a current stack is required, but none is currently selected,
  then pop the same interactive menu shown above to select one.
  Depending on the command being run, we may or may not show the
  option to create a new stack (e.g., that doesn't make much sense
  when you're running `pulumi destroy`, but might when you're
  running `pulumi stack`).  This again lets you do with a single
  command what would have otherwise entailed an error with multiple
  commands to recover from it.

* If you run `pulumi stack init` without any additional arguments,
  we interactively prompt for the stack name.  Before, we would
  error and you'd then need to run `pulumi stack init <name>`.

* Colorize some things nicely; for example, now all prompts will
  by default become bright white.
2018-02-16 15:03:54 -08:00

81 lines
2 KiB
Go

// Copyright 2016-2017, Pulumi Corporation. All rights reserved.
package cmd
import (
"encoding/json"
"fmt"
"os"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/pulumi/pulumi/pkg/backend"
"github.com/pulumi/pulumi/pkg/tokens"
"github.com/pulumi/pulumi/pkg/util/cmdutil"
)
func newHistoryCmd() *cobra.Command {
var stack string
var outputJSON bool // Requires PULUMI_DEBUG_COMMANDS
var cmd = &cobra.Command{
Use: "history",
Aliases: []string{"hist"},
SuggestFor: []string{"updates"},
Short: "Update history for a stack",
Long: "Update history for a stack\n" +
"\n" +
"This command lists data about previous updates for a stack.",
Args: cmdutil.NoArgs,
Run: cmdutil.RunFunc(func(cmd *cobra.Command, args []string) error {
s, err := requireStack(tokens.QName(stack), false)
if err != nil {
return err
}
b := s.Backend()
updates, err := b.GetHistory(s.Name())
if err != nil {
return errors.Wrap(err, "getting history")
}
// Sort the updates to ensure the most recent updates come first.
backend.Sort(updates)
if outputJSON {
b, err := json.MarshalIndent(updates, "", " ")
if err != nil {
return err
}
fmt.Println(string(b))
} else {
printUpdateHistory(updates)
}
return nil
}),
}
cmd.PersistentFlags().StringVarP(
&stack, "stack", "s", "",
"Choose an stack other than the currently selected one")
// pulumi/issues/496 tracks adding a --format option across all commands. Rather than expose a partial solution
// for just `history`, we put the JSON flag behind an env var so we can use in tests w/o making public.
if cmdutil.IsTruthy(os.Getenv("PULUMI_DEBUG_COMMANDS")) {
cmd.PersistentFlags().BoolVar(&outputJSON, "output-json", false, "Output stack history as JSON")
}
return cmd
}
func printUpdateHistory(updates []backend.UpdateInfo) {
if len(updates) == 0 {
fmt.Println("Stack has never been updated")
return
}
for _, update := range updates {
fmt.Printf("%8v %8v %v\n", update.Kind, update.Result, update.Message)
}
}