pulumi/cmd/history.go
Chris Smith 4c217fd358
Add "pulumi history" command (#826)
This PR adds a new `pulumi history` command, which prints the update history for a stack.

The local backend stores the update history in a JSON file on disk, next to the checkpoint file. The cloud backend simply provides the update metadata, and expects to receive all the data from a (NYI) `/history` REST endpoint.

`pkg/backend/updates.go` defines the data that is being persisted. The way the data is wired through the system is adding a new `backend.UpdateMetadata` parameter to a Stack/Backend's `Update` and `Destroy` methods.

I use `tests/integration/stack_outputs/` as the simple app for the related tests, hence the addition to the `.gitignore` and fixing the name in the `Pulumi.yaml`.

Fixes #636.
2018-01-24 18:22:41 -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))
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)
}
}