Move environment printing back into the CLI

Previously the engine was concerned with displaying information about
the environment. Now the engine returns an environment info object
which the CLI uses to display environment information.
This commit is contained in:
Matt Ellis 2017-10-02 13:34:33 -07:00
parent c022db9285
commit 7900e2edb1
2 changed files with 45 additions and 21 deletions

View file

@ -3,6 +3,9 @@
package cmd
import (
"fmt"
"strconv"
"github.com/spf13/cobra"
"github.com/pulumi/pulumi/pkg/util/cmdutil"
@ -13,7 +16,30 @@ func newEnvLsCmd() *cobra.Command {
Use: "ls",
Short: "List all known environments",
Run: cmdutil.RunFunc(func(cmd *cobra.Command, args []string) error {
return lumiEngine.ListEnvs()
envs, err := lumiEngine.GetEnvironments()
if err != nil {
return err
}
fmt.Printf("%-20s %-48s %-12s\n", "NAME", "LAST UPDATE", "RESOURCE COUNT")
for _, env := range envs {
// Now print out the name, last deployment time (if any), and resources (if any).
lastDeploy := "n/a"
resourceCount := "n/a"
if env.Checkpoint.Latest != nil {
lastDeploy = env.Checkpoint.Latest.Time.String()
}
if env.Snapshot != nil {
resourceCount = strconv.Itoa(len(env.Snapshot.Resources))
}
display := env.Name
if env.IsCurrent {
display += "*" // fancify the current environment.
}
fmt.Printf("%-20s %-48s %-12s\n", display, lastDeploy, resourceCount)
}
return nil
}),
}
}

View file

@ -3,27 +3,35 @@
package engine
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"github.com/pkg/errors"
"github.com/pulumi/pulumi/pkg/encoding"
"github.com/pulumi/pulumi/pkg/resource/deploy"
"github.com/pulumi/pulumi/pkg/resource/environment"
"github.com/pulumi/pulumi/pkg/tokens"
"github.com/pulumi/pulumi/pkg/workspace"
)
func (eng *Engine) ListEnvs() error {
type EnvironmentInfo struct {
Name string
Snapshot *deploy.Snapshot
Checkpoint *environment.Checkpoint
IsCurrent bool
}
func (eng *Engine) GetEnvironments() ([]EnvironmentInfo, error) {
var envs []EnvironmentInfo
// Read the environment directory.
path := workspace.EnvPath("")
files, err := ioutil.ReadDir(path)
if err != nil && !os.IsNotExist(err) {
return errors.Errorf("could not read environments: %v", err)
return nil, errors.Errorf("could not read environments: %v", err)
}
fmt.Fprintf(eng.Stdout, "%-20s %-48s %-12s\n", "NAME", "LAST UPDATE", "RESOURCE COUNT")
curr := eng.getCurrentEnv()
for _, file := range files {
// Ignore directories.
@ -45,21 +53,11 @@ func (eng *Engine) ListEnvs() error {
continue // failure reading the environment information.
}
// Now print out the name, last deployment time (if any), and resources (if any).
lastDeploy := "n/a"
resourceCount := "n/a"
if checkpoint.Latest != nil {
lastDeploy = checkpoint.Latest.Time.String()
}
if snapshot != nil {
resourceCount = strconv.Itoa(len(snapshot.Resources))
}
display := target.Name
if display == curr {
display += "*" // fancify the current environment.
}
fmt.Fprintf(eng.Stdout, "%-20s %-48s %-12s\n", display, lastDeploy, resourceCount)
envs = append(envs, EnvironmentInfo{Name: target.Name.String(),
Snapshot: snapshot,
Checkpoint: checkpoint,
IsCurrent: (curr == target.Name)})
}
return nil
return envs, nil
}