Auth methods CLI (#277)

This commit is contained in:
Jeff Mitchell authored and GitHub committed 2020-08-18 17:46:34 -04:00
1 parent 041e1f9fd3
commit adfc5681be
41 files changed
+951 -192

No files matched your search

+7 -3
View File
@@ -104,12 +104,14 @@ func MakeShutdownCh() chan struct{} {
// Client returns the HTTP API client. The client is cached on the command to
// save performance on future calls.
func (c *Command) Client() (*api.Client, error) {
func (c *Command) Client(opt ...Option) (*api.Client, error) {
// Read the test client if present
if c.client != nil {
return c.client, nil
}
opts := getOpts(opt...)
config, err := api.DefaultConfig()
if err != nil {
return nil, err
@@ -194,7 +196,9 @@ func (c *Command) Client() (*api.Client, error) {
c.UI.Error(fmt.Sprintf("Error unmarshaling stored token information after reading from system credential store: %s", err))
} else {
c.client.SetToken(authToken.Token)
c.client.SetScopeId(authToken.Scope.Id)
if !opts.withNoTokenScope {
c.client.SetScopeId(authToken.Scope.Id)
}
}
}
}
@@ -211,7 +215,7 @@ func (c *Command) Client() (*api.Client, error) {
c.UI.Info(fmt.Sprintf("Scope of %q set from -scope command flag", c.flagScope))
}
}
} else if c.client.ScopeId() != "" {
} else if c.client.ScopeId() != "" && !opts.withNoTokenScope {
// If it didn't come from env or a flag but isn't empty, it must have come from the token
if c.flagFormat == "table" && c.flagVerbose {
c.UI.Info(fmt.Sprintf("Scope of %q set from saved token with name %q", c.client.ScopeId(), tokenName))
+23
View File
@@ -54,6 +54,29 @@ func WrapForHelpText(lines []string) string {
return strings.Join(ret, "\n")
}
func WrapMap(prefixSpaces, maxLengthOverride int, input map[string]interface{}) string {
maxKeyLength := maxLengthOverride
if maxKeyLength == 0 {
for k := range input {
if len(k) > maxKeyLength {
maxKeyLength = len(k)
}
}
}
var ret []string
for k, v := range input {
spaces := maxKeyLength - len(k)
ret = append(ret, fmt.Sprintf("%s%s%s%s",
strings.Repeat(" ", prefixSpaces),
fmt.Sprintf("%s: ", k),
strings.Repeat(" ", spaces),
fmt.Sprintf("%v", v)),
)
}
return strings.Join(ret, "\n")
}
type FormatOptions struct {
Format string
}
+31
View File
@@ -0,0 +1,31 @@
package base
// getOpts - iterate the inbound Options and return a struct.
func getOpts(opt ...Option) Options {
opts := getDefaultOptions()
for _, o := range opt {
o(&opts)
}
return opts
}
// Option - how Options are passed as arguments.
type Option func(*Options)
// Options - how Options are represented.
type Options struct {
withNoTokenScope bool
}
func getDefaultOptions() Options {
return Options{}
}
// WithNoTokenScope tells the client not to set a scope for the client from a
// saved token's scope, as this can cause confusing behavior at authentication
// time.
func WithNoTokenScope() Option {
return func(o *Options) {
o.withNoTokenScope = true
}
}