From d74e30d94a6ab0092b8f5228cdad360747bc9cdb Mon Sep 17 00:00:00 2001 From: Jeff Mitchell Date: Fri, 7 Aug 2020 13:50:39 -0400 Subject: [PATCH] Minor, easy, linting fixes --- internal/cmd/base/base.go | 4 +- internal/cmd/base/flags.go | 30 ------- internal/cmd/base/format.go | 4 +- internal/cmd/base/helpers.go | 147 +--------------------------------- internal/cmd/base/listener.go | 12 ++- internal/cmd/base/servers.go | 8 +- 6 files changed, 22 insertions(+), 183 deletions(-) diff --git a/internal/cmd/base/base.go b/internal/cmd/base/base.go index 20244bd707..fe19f5ee83 100644 --- a/internal/cmd/base/base.go +++ b/internal/cmd/base/base.go @@ -119,7 +119,9 @@ func (c *Command) Client() (*api.Client, error) { } if c.flagAddr != NotSetValue { - c.client.SetAddr(c.flagAddr) + if err := c.client.SetAddr(c.flagAddr); err != nil { + return nil, fmt.Errorf("error setting address on client: %w", err) + } } // If we need custom TLS configuration, then set it diff --git a/internal/cmd/base/flags.go b/internal/cmd/base/flags.go index bed0da9767..63efaf7adc 100644 --- a/internal/cmd/base/flags.go +++ b/internal/cmd/base/flags.go @@ -748,33 +748,3 @@ func (f *FlagSet) Var(value flag.Value, name, usage string) { f.mainSet.Var(value, name, usage) f.flagSet.Var(value, name, usage) } - -// -- helpers -func envDefault(key, def string) string { - if v, exist := os.LookupEnv(key); exist { - return v - } - return def -} - -func envBoolDefault(key string, def bool) bool { - if v, exist := os.LookupEnv(key); exist { - b, err := strconv.ParseBool(v) - if err != nil { - panic(err) - } - return b - } - return def -} - -func envDurationDefault(key string, def time.Duration) time.Duration { - if v, exist := os.LookupEnv(key); exist { - d, err := time.ParseDuration(v) - if err != nil { - panic(err) - } - return d - } - return def -} diff --git a/internal/cmd/base/format.go b/internal/cmd/base/format.go index f39e80a087..6486ae2641 100644 --- a/internal/cmd/base/format.go +++ b/internal/cmd/base/format.go @@ -397,9 +397,9 @@ var Formatters = map[string]Formatter{ } func Format(ui cli.Ui) string { - switch ui.(type) { + switch t := ui.(type) { case *WatchtowerUI: - return ui.(*WatchtowerUI).Format + return t.Format } format := os.Getenv(EnvWatchtowerCLIFormat) diff --git a/internal/cmd/base/helpers.go b/internal/cmd/base/helpers.go index 33446d28ae..cca0697c7b 100644 --- a/internal/cmd/base/helpers.go +++ b/internal/cmd/base/helpers.go @@ -2,62 +2,13 @@ package base import ( "encoding/json" - "io" "strings" "time" - kvbuilder "github.com/hashicorp/vault/internalshared/kv-builder" "github.com/kr/text" - homedir "github.com/mitchellh/go-homedir" - "github.com/mitchellh/mapstructure" - "github.com/pkg/errors" "github.com/ryanuber/columnize" ) -// sanitizePath removes any leading or trailing things from a "path". -func sanitizePath(s string) string { - return ensureNoTrailingSlash(ensureNoLeadingSlash(strings.TrimSpace(s))) -} - -// ensureTrailingSlash ensures the given string has a trailing slash. -func ensureTrailingSlash(s string) string { - s = strings.TrimSpace(s) - if s == "" { - return "" - } - - for len(s) > 0 && s[len(s)-1] != '/' { - s = s + "/" - } - return s -} - -// ensureNoTrailingSlash ensures the given string has a trailing slash. -func ensureNoTrailingSlash(s string) string { - s = strings.TrimSpace(s) - if s == "" { - return "" - } - - for len(s) > 0 && s[len(s)-1] == '/' { - s = s[:len(s)-1] - } - return s -} - -// ensureNoLeadingSlash ensures the given string has a trailing slash. -func ensureNoLeadingSlash(s string) string { - s = strings.TrimSpace(s) - if s == "" { - return "" - } - - for len(s) > 0 && s[0] == '/' { - s = s[1:] - } - return s -} - // columnOuput prints the list of items as a table with no headers. func columnOutput(list []string, c *columnize.Config) string { if len(list) == 0 { @@ -108,80 +59,6 @@ func tableOutput(list []string, c *columnize.Config) string { return columnOutput(list, c) } -// parseArgsData parses the given args in the format key=value into a map of -// the provided arguments. The given reader can also supply key=value pairs. -func parseArgsData(stdin io.Reader, args []string) (map[string]interface{}, error) { - builder := &kvbuilder.Builder{Stdin: stdin} - if err := builder.Add(args...); err != nil { - return nil, err - } - - return builder.Map(), nil -} - -// parseArgsDataString parses the args data and returns the values as strings. -// If the values cannot be represented as strings, an error is returned. -func parseArgsDataString(stdin io.Reader, args []string) (map[string]string, error) { - raw, err := parseArgsData(stdin, args) - if err != nil { - return nil, err - } - - var result map[string]string - if err := mapstructure.WeakDecode(raw, &result); err != nil { - return nil, errors.Wrap(err, "failed to convert values to strings") - } - if result == nil { - result = make(map[string]string) - } - return result, nil -} - -// parseArgsDataStringLists parses the args data and returns the values as -// string lists. If the values cannot be represented as strings, an error is -// returned. -func parseArgsDataStringLists(stdin io.Reader, args []string) (map[string][]string, error) { - raw, err := parseArgsData(stdin, args) - if err != nil { - return nil, err - } - - var result map[string][]string - if err := mapstructure.WeakDecode(raw, &result); err != nil { - return nil, errors.Wrap(err, "failed to convert values to strings") - } - return result, nil -} - -// truncateToSeconds truncates the given duration to the number of seconds. If -// the duration is less than 1s, it is returned as 0. The integer represents -// the whole number unit of seconds for the duration. -func truncateToSeconds(d time.Duration) int { - d = d.Truncate(1 * time.Second) - - // Handle the case where someone requested a ridiculously short increment - - // increments must be larger than a second. - if d < 1*time.Second { - return 0 - } - - return int(d.Seconds()) -} - -// expandPath takes a filepath and returns the full expanded path, accounting -// for user-relative things like ~/. -func expandPath(s string) string { - if s == "" { - return "" - } - - e, err := homedir.Expand(s) - if err != nil { - return s - } - return e -} - // WrapAtLengthWithPadding wraps the given text at the maxLineLength, taking // into account any provided left padding. func WrapAtLengthWithPadding(s string, pad int) string { @@ -198,22 +75,6 @@ func WrapAtLength(s string) string { return WrapAtLengthWithPadding(s, 0) } -// ttlToAPI converts a user-supplied ttl into an API-compatible string. If -// the TTL is 0, this returns the empty string. If the TTL is negative, this -// returns "system" to indicate to use the system values. Otherwise, the -// time.Duration ttl is used. -func ttlToAPI(d time.Duration) string { - if d == 0 { - return "" - } - - if d < 0 { - return "system" - } - - return d.String() -} - // humanDuration prints the time duration without those pesky zeros. func humanDuration(d time.Duration) string { if d == 0 { @@ -233,13 +94,13 @@ func humanDuration(d time.Duration) string { // humanDurationInt prints the given int as if it were a time.Duration number // of seconds. func humanDurationInt(i interface{}) interface{} { - switch i.(type) { + switch t := i.(type) { case int: - return humanDuration(time.Duration(i.(int)) * time.Second) + return humanDuration(time.Duration(t) * time.Second) case int64: - return humanDuration(time.Duration(i.(int64)) * time.Second) + return humanDuration(time.Duration(t) * time.Second) case json.Number: - if i, err := i.(json.Number).Int64(); err == nil { + if i, err := t.Int64(); err == nil { return humanDuration(time.Duration(i) * time.Second) } } diff --git a/internal/cmd/base/listener.go b/internal/cmd/base/listener.go index 6e4e3bb6b9..c77b7221ce 100644 --- a/internal/cmd/base/listener.go +++ b/internal/cmd/base/listener.go @@ -179,12 +179,16 @@ type TCPKeepAliveListener struct { *net.TCPListener } -func (ln TCPKeepAliveListener) Accept() (c net.Conn, err error) { +func (ln TCPKeepAliveListener) Accept() (net.Conn, error) { tc, err := ln.AcceptTCP() if err != nil { - return + return nil, err + } + if err := tc.SetKeepAlive(true); err != nil { + return nil, err + } + if err := tc.SetKeepAlivePeriod(3 * time.Minute); err != nil { + return nil, err } - tc.SetKeepAlive(true) - tc.SetKeepAlivePeriod(3 * time.Minute) return tc, nil } diff --git a/internal/cmd/base/servers.go b/internal/cmd/base/servers.go index 18dac24e68..8a54cf749a 100644 --- a/internal/cmd/base/servers.go +++ b/internal/cmd/base/servers.go @@ -287,7 +287,7 @@ func (b *Server) SetupListeners(ui cli.Ui, config *configutil.SharedConfig) erro if lnConfig.MaxRequestDuration == 0 { lnConfig.MaxRequestDuration = globals.DefaultMaxRequestDuration } - props["max_request_duration"] = fmt.Sprintf("%s", lnConfig.MaxRequestDuration.String()) + props["max_request_duration"] = lnConfig.MaxRequestDuration.String() b.Listeners = append(b.Listeners, &ServerListener{ Mux: lnMux, @@ -387,7 +387,9 @@ func (b *Server) CreateDevDatabase(dialect string) error { // In case of an error, run the cleanup function. If we pass all errors, c should be set to a noop // function before returning from this method defer func() { - c() + if err := c(); err != nil { + b.Logger.Error("error cleaning up docker container", "error", err) + } }() if err != nil { @@ -495,7 +497,7 @@ func (b *Server) CreateDevDatabase(dialect string) error { if amId == "" { amId = "paum_1234567890" } - authMethod, err = pwRepo.CreateAuthMethod(ctx, authMethod, password.WithPublicId(amId)) + _, err = pwRepo.CreateAuthMethod(ctx, authMethod, password.WithPublicId(amId)) if err != nil { return fmt.Errorf("error saving auth method to the db: %w", err) }