Support using short names for config keys

Previously, you had to fully qualify configuration values (e.g
example:config:message). As a convience, let's support adding
configuration values where the key is not a fully qualified module
member. In this case, we'll treat the key as if
`<program-name>:config:` had been prepended to it.

In addition, when we print config, shorten keys of the form
`<program-name>:config:<key-name>` to `<key-name>`.

I've updated one integration test to use the new syntax and left the
other as is to ensure both continue to work.
This commit is contained in:
Matt Ellis 2017-10-16 16:01:34 -07:00
parent 8fda24a776
commit 15a0692ac8
3 changed files with 79 additions and 4 deletions

View file

@ -5,6 +5,7 @@ package cmd
import (
"fmt"
"sort"
"strings"
"github.com/pulumi/pulumi/pkg/util/contract"
@ -31,7 +32,7 @@ func newConfigCmd() *cobra.Command {
return listConfig(stackName)
}
key, err := tokens.ParseModuleMember(args[0])
key, err := parseConfigKey(args[0])
if err != nil {
return errors.Wrap(err, "invalid configuration key")
}
@ -57,6 +58,38 @@ func newConfigCmd() *cobra.Command {
return cmd
}
func parseConfigKey(key string) (tokens.ModuleMember, error) {
// As a convience, we'll treat any key with no delimiter as if:
// <program-name>:config:<key> had been written instead
if !strings.Contains(key, tokens.TokenDelimiter) {
_, pkg, err := getPackage()
if err != nil {
return "", err
}
return tokens.ParseModuleMember(fmt.Sprintf("%s:config:%s", pkg.Name, key))
}
return tokens.ParseModuleMember(key)
}
func prettyKey(key string) string {
_, pkg, err := getPackage()
if err != nil {
return key
}
s := key
defaultPrefix := fmt.Sprintf("%s:config:", pkg.Name)
if strings.HasPrefix(s, defaultPrefix) {
return s[len(defaultPrefix):]
}
return s
}
func listConfig(stackName tokens.QName) error {
config, err := getConfiguration(stackName)
if err != nil {
@ -67,11 +100,13 @@ func listConfig(stackName tokens.QName) error {
fmt.Printf("%-32s %-32s\n", "KEY", "VALUE")
var keys []string
for key := range config {
// Note that we use the fully qualified module member here instead of a `prettyKey`, this lets us ensure that all the config
// values for the current program are displayed next to one another in the output.
keys = append(keys, string(key))
}
sort.Strings(keys)
for _, key := range keys {
fmt.Printf("%-32s %-32s\n", key, config[tokens.ModuleMember(key)])
fmt.Printf("%-32s %-32s\n", prettyKey(key), config[tokens.ModuleMember(key)])
}
}
@ -91,7 +126,7 @@ func getConfig(stackName tokens.QName, key tokens.ModuleMember) error {
}
}
return errors.Errorf("configuration key '%v' not found for stack '%v'", key, stackName)
return errors.Errorf("configuration key '%v' not found for stack '%v'", prettyKey(key.String()), stackName)
}
func getConfiguration(stackName tokens.QName) (map[tokens.ModuleMember]string, error) {

View file

@ -6,6 +6,9 @@ import (
"io/ioutil"
"os"
"github.com/pulumi/pulumi/pkg/encoding"
"github.com/pulumi/pulumi/pkg/pack"
"github.com/pkg/errors"
"github.com/pulumi/pulumi/pkg/diag"
@ -99,3 +102,40 @@ func displayEvents(events <-chan engine.Event, done chan bool, debug bool) {
}
}
}
func getPackage() (string, pack.Package, error) {
dir, err := os.Getwd()
if err != nil {
return "", pack.Package{}, err
}
pkgPath, err := workspace.DetectPackage(dir)
if err != nil {
return "", pack.Package{}, err
}
if pkgPath == "" {
return "", pack.Package{}, errors.Errorf("could not find Pulumi.yaml, started search in %s", dir)
}
m, _ := encoding.Detect(pkgPath)
b, err := ioutil.ReadFile(pkgPath)
if err != nil {
return "", pack.Package{}, err
}
var pkg pack.Package
err = m.Unmarshal(b, &pkg)
if err != nil {
return "", pack.Package{}, err
}
err = pkg.Validate()
if err != nil {
return "", pack.Package{}, err
}
return pkgPath, pkg, err
}

View file

@ -22,7 +22,7 @@ func TestExamples(t *testing.T) {
Dir: path.Join(cwd, "minimal"),
Dependencies: []string{"pulumi"},
Config: map[string]string{
"minimal:config:name": "Pulumi",
"name": "Pulumi",
},
},
{