pulumi/cmd/config.go
joeduffy 3d74eac67d 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-21 19:23:32 -07:00

76 lines
2 KiB
Go

// Copyright 2017 Pulumi, Inc. All rights reserved.
package cmd
import (
"fmt"
"github.com/spf13/cobra"
"github.com/pulumi/coconut/pkg/resource"
"github.com/pulumi/coconut/pkg/tokens"
)
func newConfigCmd() *cobra.Command {
var env string
var unset bool
cmd := &cobra.Command{
Use: "config [<key> [value]]",
Short: "Query, set, replace, or unset configuration values",
Run: runFunc(func(cmd *cobra.Command, args []string) error {
info, err := initEnvCmdName(tokens.QName(env), args)
if err != nil {
return err
}
defer info.Close() // ensure we clean up resources before exiting.
config := info.Env.Config
if len(info.Args) == 0 {
// If no args were supplied, we are just printing out the current configuration.
if config != nil {
fmt.Printf("%-32s %-32s\n", "KEY", "VALUE")
for _, key := range resource.StableConfigKeys(info.Env.Config) {
v := info.Env.Config[key]
// TODO: print complex values.
fmt.Printf("%-32s %-32s\n", key, v)
}
}
} else {
key := tokens.Token(info.Args[0])
if config == nil {
config = make(resource.ConfigMap)
info.Env.Config = config
}
if len(info.Args) > 1 {
// If there is a value, we are setting the configuration entry.
// TODO: support values other than strings.
config[key] = info.Args[1]
saveEnv(info.Env, info.Old, "", true)
} else {
// If there was no value supplied, we are either reading or unsetting the entry.
if unset {
delete(config, key)
saveEnv(info.Env, info.Old, "", true)
} else if v, has := config[key]; has {
// TODO: print complex values.
fmt.Printf("%v\n", v)
} else {
return fmt.Errorf("configuration key '%v' not found for environment '%v'", key, info.Env.Name)
}
}
}
return nil
}),
}
cmd.PersistentFlags().StringVarP(
&env, "env", "e", "",
"Choose an environment other than the currently selected one")
cmd.PersistentFlags().BoolVar(
&unset, "unset", false,
"Unset a configuration value")
return cmd
}