Move some configuration logic into the CLI
The CLI is now responsible for actually displaying information and the engine is only concerned with getting the configuration. As part of this change, I've removed the display a single configuration value API from the engine. It can now be done in terms of getting all the config for an environment and selecting the value the user is interested in
This commit is contained in:
parent
7900e2edb1
commit
aa6c6d6617
|
@ -3,6 +3,9 @@
|
|||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
|
@ -18,7 +21,7 @@ func newConfigCmd() *cobra.Command {
|
|||
Short: "Query, set, replace, or unset configuration values",
|
||||
Run: cmdutil.RunFunc(func(cmd *cobra.Command, args []string) error {
|
||||
if len(args) == 0 {
|
||||
return lumiEngine.ListConfig(env)
|
||||
return listConfig(env)
|
||||
}
|
||||
|
||||
key, err := tokens.ParseModuleMember(args[0])
|
||||
|
@ -28,7 +31,7 @@ func newConfigCmd() *cobra.Command {
|
|||
|
||||
if len(args) == 1 {
|
||||
if !unset {
|
||||
return lumiEngine.GetConfig(env, key)
|
||||
return getConfig(env, key)
|
||||
}
|
||||
return lumiEngine.DeleteConfig(env, key)
|
||||
}
|
||||
|
@ -46,3 +49,41 @@ func newConfigCmd() *cobra.Command {
|
|||
|
||||
return cmd
|
||||
}
|
||||
|
||||
func listConfig(env string) error {
|
||||
config, err := lumiEngine.GetConfiguration(env)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if config != nil {
|
||||
fmt.Printf("%-32s %-32s\n", "KEY", "VALUE")
|
||||
var keys []string
|
||||
for key := range config {
|
||||
keys = append(keys, string(key))
|
||||
}
|
||||
sort.Strings(keys)
|
||||
for _, key := range keys {
|
||||
fmt.Printf("%-32s %-32s\n", key, config[tokens.ModuleMember(key)])
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func getConfig(env string, key tokens.ModuleMember) error {
|
||||
config, err := lumiEngine.GetConfiguration(env)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if config != nil {
|
||||
if v, ok := config[key]; ok {
|
||||
fmt.Printf("%v\n", v)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
return errors.Errorf("configuration key '%v' not found for environment '%v'", key, env)
|
||||
|
||||
}
|
||||
|
|
|
@ -1,27 +0,0 @@
|
|||
// Copyright 2017, Pulumi Corporation. All rights reserved.
|
||||
|
||||
package engine
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/pulumi/pulumi/pkg/tokens"
|
||||
)
|
||||
|
||||
func (eng *Engine) GetConfig(envName string, key tokens.ModuleMember) error {
|
||||
info, err := eng.initEnvCmdName(tokens.QName(envName), "")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
config := info.Target.Config
|
||||
if config != nil {
|
||||
if v, has := config[key]; has {
|
||||
fmt.Fprintf(eng.Stdout, "%v\n", v)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
return errors.Errorf("configuration key '%v' not found for environment '%v'", key, info.Target.Name)
|
||||
}
|
|
@ -3,31 +3,14 @@
|
|||
package engine
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
|
||||
"github.com/pulumi/pulumi/pkg/tokens"
|
||||
)
|
||||
|
||||
func (eng *Engine) ListConfig(envName string) error {
|
||||
info, err := eng.initEnvCmdName(tokens.QName(envName), "")
|
||||
func (eng *Engine) GetConfiguration(environment string) (map[tokens.ModuleMember]string, error) {
|
||||
info, err := eng.initEnvCmdName(tokens.QName(environment), "")
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
config := info.Target.Config
|
||||
if config != nil {
|
||||
fmt.Fprintf(eng.Stdout, "%-32s %-32s\n", "KEY", "VALUE")
|
||||
var keys []string
|
||||
for key := range info.Target.Config {
|
||||
keys = append(keys, string(key))
|
||||
}
|
||||
sort.Strings(keys)
|
||||
for _, key := range keys {
|
||||
v := info.Target.Config[tokens.ModuleMember(key)]
|
||||
// TODO[pulumi/pulumi#113]: print complex values.
|
||||
fmt.Fprintf(eng.Stdout, "%-32s %-32s\n", key, v)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
return info.Target.Config, nil
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue