2017-02-26 22:06:33 +01:00
|
|
|
// Copyright 2016 Pulumi, Inc. All rights reserved.
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
|
|
|
"github.com/pulumi/coconut/pkg/encoding"
|
|
|
|
"github.com/pulumi/coconut/pkg/resource"
|
|
|
|
"github.com/pulumi/coconut/pkg/tokens"
|
|
|
|
"github.com/pulumi/coconut/pkg/util/contract"
|
|
|
|
"github.com/pulumi/coconut/pkg/workspace"
|
|
|
|
)
|
|
|
|
|
|
|
|
func newHuskLsCmd() *cobra.Command {
|
|
|
|
return &cobra.Command{
|
|
|
|
Use: "ls",
|
|
|
|
Aliases: []string{"list"},
|
|
|
|
Short: "List all known husks",
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
path := workspace.HuskPath("")
|
|
|
|
files, err := ioutil.ReadDir(path)
|
|
|
|
if err != nil && !os.IsNotExist(err) {
|
2017-03-01 19:09:27 +01:00
|
|
|
exitError("could not read husks: %v", err)
|
2017-02-26 22:06:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("%-20s %-48s %-12s\n", "NAME", "LAST DEPLOYMENT", "RESOURCE COUNT")
|
|
|
|
for _, file := range files {
|
|
|
|
// Ignore directories.
|
|
|
|
if file.IsDir() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip files without valid extensions (e.g., *.bak files).
|
2017-03-01 00:43:46 +01:00
|
|
|
huskfn := file.Name()
|
|
|
|
ext := filepath.Ext(huskfn)
|
2017-02-26 22:06:33 +01:00
|
|
|
if _, has := encoding.Marshalers[ext]; !has {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new context and read in the husk information.
|
2017-03-01 00:43:46 +01:00
|
|
|
name := tokens.QName(huskfn[:len(huskfn)-len(ext)])
|
2017-02-26 22:06:33 +01:00
|
|
|
ctx := resource.NewContext(sink())
|
2017-03-01 00:43:46 +01:00
|
|
|
huskfile, husk, old := readHusk(ctx, name)
|
|
|
|
if husk == nil {
|
2017-02-26 22:06:33 +01:00
|
|
|
contract.Assert(!ctx.Diag.Success())
|
|
|
|
continue // failure reading the husk information.
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now print out the name, last deployment time (if any), and resources (if any).
|
|
|
|
lastDeploy := "n/a"
|
|
|
|
resourceCount := "n/a"
|
2017-03-01 00:43:46 +01:00
|
|
|
if huskfile.Latest != nil {
|
|
|
|
lastDeploy = huskfile.Latest.Time.String()
|
2017-02-26 22:06:33 +01:00
|
|
|
}
|
|
|
|
if old != nil {
|
|
|
|
resourceCount = strconv.Itoa(len(old.Resources()))
|
|
|
|
}
|
2017-03-01 00:43:46 +01:00
|
|
|
fmt.Printf("%-20s %-48s %-12s\n", husk.Name, lastDeploy, resourceCount)
|
2017-02-26 22:06:33 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|