2017-03-15 03:26:14 +01:00
|
|
|
// Copyright 2017 Pulumi, Inc. All rights reserved.
|
2017-02-26 22:06:33 +01:00
|
|
|
|
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
2017-03-06 15:32:39 +01:00
|
|
|
func newEnvLsCmd() *cobra.Command {
|
2017-02-26 22:06:33 +01:00
|
|
|
return &cobra.Command{
|
|
|
|
Use: "ls",
|
|
|
|
Aliases: []string{"list"},
|
2017-03-06 15:32:39 +01:00
|
|
|
Short: "List all known environments",
|
2017-03-07 14:47:42 +01:00
|
|
|
Run: runFunc(func(cmd *cobra.Command, args []string) error {
|
|
|
|
// Read the environment directory.
|
2017-03-06 15:32:39 +01:00
|
|
|
path := workspace.EnvPath("")
|
2017-02-26 22:06:33 +01:00
|
|
|
files, err := ioutil.ReadDir(path)
|
|
|
|
if err != nil && !os.IsNotExist(err) {
|
2017-03-07 14:47:42 +01:00
|
|
|
return fmt.Errorf("could not read environments: %v", err)
|
2017-02-26 22:06:33 +01:00
|
|
|
}
|
|
|
|
|
2017-03-07 14:47:42 +01:00
|
|
|
// Create a new context to share amongst all of the loads.
|
|
|
|
ctx := resource.NewContext(sink())
|
|
|
|
defer ctx.Close()
|
|
|
|
|
2017-02-26 22:06:33 +01:00
|
|
|
fmt.Printf("%-20s %-48s %-12s\n", "NAME", "LAST DEPLOYMENT", "RESOURCE COUNT")
|
Make major commands more pleasant
This change eliminates the need to constantly type in the environment
name when performing major commands like configuration, planning, and
deployment. It's probably due to my age, however, I keep fat-fingering
simple commands in front of investors and I am embarrassed!
In the new model, there is a notion of a "current environment", and
I have modeled it kinda sorta just like Git's notion of "current branch."
By default, the current environment is set when you `init` something.
Otherwise, there is the `coco env select <env>` command to change it.
(Running this command w/out a new <env> will show you the current one.)
The major commands `config`, `plan`, `deploy`, and `destroy` will prefer
to use the current environment, unless it is overridden by using the
--env flag. All of the `coco env <cmd> <env>` commands still require the
explicit passing of an environment which seems reasonable since they are,
after all, about manipulating environments.
As part of this, I've overhauled the aging workspace settings cruft,
which had fallen into disrepair since the initial prototype.
2017-03-22 03:23:32 +01:00
|
|
|
curr := getCurrentEnv()
|
2017-02-26 22:06:33 +01:00
|
|
|
for _, file := range files {
|
|
|
|
// Ignore directories.
|
|
|
|
if file.IsDir() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip files without valid extensions (e.g., *.bak files).
|
2017-03-06 15:32:39 +01:00
|
|
|
envfn := file.Name()
|
|
|
|
ext := filepath.Ext(envfn)
|
2017-02-26 22:06:33 +01:00
|
|
|
if _, has := encoding.Marshalers[ext]; !has {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-03-07 14:47:42 +01:00
|
|
|
// Read in this environment's information.
|
2017-03-06 15:32:39 +01:00
|
|
|
name := tokens.QName(envfn[:len(envfn)-len(ext)])
|
|
|
|
envfile, env, old := readEnv(ctx, name)
|
|
|
|
if env == nil {
|
2017-02-26 22:06:33 +01:00
|
|
|
contract.Assert(!ctx.Diag.Success())
|
2017-03-06 15:32:39 +01:00
|
|
|
continue // failure reading the environment information.
|
2017-02-26 22:06:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now print out the name, last deployment time (if any), and resources (if any).
|
|
|
|
lastDeploy := "n/a"
|
|
|
|
resourceCount := "n/a"
|
2017-03-06 15:32:39 +01:00
|
|
|
if envfile.Latest != nil {
|
|
|
|
lastDeploy = envfile.Latest.Time.String()
|
2017-02-26 22:06:33 +01:00
|
|
|
}
|
|
|
|
if old != nil {
|
|
|
|
resourceCount = strconv.Itoa(len(old.Resources()))
|
|
|
|
}
|
Make major commands more pleasant
This change eliminates the need to constantly type in the environment
name when performing major commands like configuration, planning, and
deployment. It's probably due to my age, however, I keep fat-fingering
simple commands in front of investors and I am embarrassed!
In the new model, there is a notion of a "current environment", and
I have modeled it kinda sorta just like Git's notion of "current branch."
By default, the current environment is set when you `init` something.
Otherwise, there is the `coco env select <env>` command to change it.
(Running this command w/out a new <env> will show you the current one.)
The major commands `config`, `plan`, `deploy`, and `destroy` will prefer
to use the current environment, unless it is overridden by using the
--env flag. All of the `coco env <cmd> <env>` commands still require the
explicit passing of an environment which seems reasonable since they are,
after all, about manipulating environments.
As part of this, I've overhauled the aging workspace settings cruft,
which had fallen into disrepair since the initial prototype.
2017-03-22 03:23:32 +01:00
|
|
|
display := env.Name
|
|
|
|
if display == curr {
|
|
|
|
display += "*" // fancify the current environment.
|
|
|
|
}
|
|
|
|
fmt.Printf("%-20s %-48s %-12s\n", display, lastDeploy, resourceCount)
|
2017-02-26 22:06:33 +01:00
|
|
|
}
|
2017-03-07 14:47:42 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}),
|
2017-02-26 22:06:33 +01:00
|
|
|
}
|
|
|
|
}
|