Remove config from Meta; it's only used right now with the token helper.

This commit is contained in:
Jeff Mitchell 2016-04-01 16:02:18 -04:00
parent a137081241
commit b0888e8af1
9 changed files with 29 additions and 46 deletions

View File

@ -52,7 +52,7 @@ func (c *AuthCommand) Run(args []string) int {
args = flags.Args()
tokenHelper, err := c.TokenHelper(&c.Meta)
tokenHelper, err := c.TokenHelper()
if err != nil {
c.Ui.Error(fmt.Sprintf(
"Error initializing token helper: %s\n\n"+

View File

@ -69,7 +69,7 @@ func TestAuth_token(t *testing.T) {
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
}
helper, err := c.TokenHelper(&c.Meta)
helper, err := c.TokenHelper()
if err != nil {
t.Fatalf("err: %s", err)
}
@ -166,7 +166,7 @@ func TestAuth_method(t *testing.T) {
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
}
helper, err := c.TokenHelper(&c.Meta)
helper, err := c.TokenHelper()
if err != nil {
t.Fatalf("err: %s", err)
}

View File

@ -6,8 +6,6 @@ import (
"github.com/hashicorp/vault/api"
)
const FixturePath = "./test-fixtures"
func testClient(t *testing.T, addr string, token string) *api.Client {
config := api.DefaultConfig()
config.Address = addr

View File

@ -1,4 +1,4 @@
package meta
package command
import (
"fmt"
@ -22,7 +22,7 @@ const (
// Config is the CLI configuration for Vault that can be specified via
// a `$HOME/.vault` file which is HCL-formatted (therefore HCL or JSON).
type Config struct {
type DefaultConfig struct {
// TokenHelper is the executable/command that is executed for storing
// and retrieving the authentication token for the Vault CLI. If this
// is not specified, then vault's internal token store will be used, which
@ -30,10 +30,22 @@ type Config struct {
TokenHelper string `hcl:"token_helper"`
}
// Config loads the configuration and returns it. If the configuration
// is already loaded, it is returned.
func Config() (*DefaultConfig, error) {
var err error
config, err := LoadConfig("")
if err != nil {
return nil, err
}
return config, nil
}
// LoadConfig reads the configuration from the given path. If path is
// empty, then the default path will be used, or the environment variable
// if set.
func LoadConfig(path string) (*Config, error) {
func LoadConfig(path string) (*DefaultConfig, error) {
if path == "" {
path = DefaultConfigPath
}
@ -55,7 +67,7 @@ func LoadConfig(path string) (*Config, error) {
}
// ParseConfig parses the given configuration as a string.
func ParseConfig(contents string) (*Config, error) {
func ParseConfig(contents string) (*DefaultConfig, error) {
root, err := hcl.Parse(contents)
if err != nil {
return nil, err
@ -74,7 +86,7 @@ func ParseConfig(contents string) (*Config, error) {
return nil, err
}
var c Config
var c DefaultConfig
if err := hcl.DecodeObject(&c, list); err != nil {
return nil, err
}

View File

@ -1,4 +1,4 @@
package meta
package command
import (
"path/filepath"
@ -15,7 +15,7 @@ func TestLoadConfig(t *testing.T) {
t.Fatalf("err: %s", err)
}
expected := &Config{
expected := &DefaultConfig{
TokenHelper: "foo",
}
if !reflect.DeepEqual(expected, config) {

View File

@ -424,7 +424,7 @@ func (c *ServerCommand) enableDev(core *vault.Core, rootTokenID string) (*vault.
}
// Set the token
tokenHelper, err := c.TokenHelper(&c.Meta)
tokenHelper, err := c.TokenHelper()
if err != nil {
return nil, err
}

View File

@ -1,13 +1,10 @@
package command
import (
"github.com/hashicorp/vault/command/token"
"github.com/hashicorp/vault/meta"
)
import "github.com/hashicorp/vault/command/token"
// DefaultTokenHelper returns the token helper that is configured for Vault.
func DefaultTokenHelper(m *meta.Meta) (token.TokenHelper, error) {
config, err := m.Config()
func DefaultTokenHelper() (token.TokenHelper, error) {
config, err := LoadConfig("")
if err != nil {
return nil, err
}

View File

@ -24,7 +24,7 @@ import (
// default FlagSet returned by Meta.FlagSet.
type FlagSetFlags uint
type TokenHelperFunc func(*Meta) (token.TokenHelper, error)
type TokenHelperFunc func() (token.TokenHelper, error)
const (
FlagSetNone FlagSetFlags = 0
@ -39,8 +39,7 @@ type Meta struct {
Ui cli.Ui
// The things below can be set, but aren't common
ForceAddress string // Address to force for API clients
ForceConfig *Config // Force a config, don't load from disk
ForceAddress string // Address to force for API clients
// These are set by the command line flags.
flagAddress string
@ -50,10 +49,6 @@ type Meta struct {
flagClientKey string
flagInsecure bool
// These are internal and shouldn't be modified or access by anyone
// except Meta.
config *Config
// Queried if no token can be found
TokenHelper TokenHelperFunc
}
@ -127,7 +122,7 @@ func (m *Meta) Client() (*api.Client, error) {
if token == "" {
if m.TokenHelper != nil {
// If we have a token, then set that
tokenHelper, err := m.TokenHelper(m)
tokenHelper, err := m.TokenHelper()
if err != nil {
return nil, err
}
@ -146,25 +141,6 @@ func (m *Meta) Client() (*api.Client, error) {
return client, nil
}
// Config loads the configuration and returns it. If the configuration
// is already loaded, it is returned.
func (m *Meta) Config() (*Config, error) {
if m.config != nil {
return m.config, nil
}
if m.ForceConfig != nil {
return m.ForceConfig, nil
}
var err error
m.config, err = LoadConfig("")
if err != nil {
return nil, err
}
return m.config, nil
}
// FlagSet returns a FlagSet with the common flags that every
// command implements. The exact behavior of FlagSet can be configured
// using the flags as the second parameter, for example to disable