From 82a530f01de6aadb60552446c68feafaff16cf7d Mon Sep 17 00:00:00 2001 From: David Moles Date: Mon, 14 Jan 2019 16:05:37 -0800 Subject: [PATCH] Improved log output, somewhat --- cmd/check.go | 22 ++++++++++++------ internal/logging/logger.go | 19 ++++------------ internal/logging/pretty.go | 20 ++++++++++++++++ internal/objects/swift_object.go | 17 +++++++++++++- internal/protocols/swift_utils.go | 38 +++++++++++++++++++++++-------- 5 files changed, 83 insertions(+), 33 deletions(-) diff --git a/cmd/check.go b/cmd/check.go index 0c5b606..40c937a 100644 --- a/cmd/check.go +++ b/cmd/check.go @@ -2,7 +2,6 @@ package cmd import ( "fmt" - "regexp" "github.com/dmolesUC3/cos/internal/logging" "github.com/dmolesUC3/cos/internal/objects" @@ -57,6 +56,17 @@ type checkFlags struct { Region string } +func (f checkFlags) Pretty() string { + format := ` + verbose: %v + expected: %x + algorithm: '%v' + endpoint: '%v' + region: '%v'` + format = logging.Untabify(format, " ") + return fmt.Sprintf(format, f.Verbose, f.Expected, f.Algorithm, f.Endpoint, f.Region) +} + func (f checkFlags) String() string { return fmt.Sprintf( "checkFlags{ verbose: %v, expected: %x, algorithm: '%v', endpoint: '%v', region: '%v'}", @@ -64,13 +74,11 @@ func (f checkFlags) String() string { ) } + + // ------------------------------------------------------------ // Functions -func formatHelp(text string, indent string) string { - return regexp.MustCompile(`(?m)^[\t ]+`).ReplaceAllString(text, indent) -} - func runWith(objURLStr string, flags checkFlags) error { var logger = logging.NewLogger(flags.Verbose) logger.Detailf("flags: %v\n", flags) @@ -108,11 +116,11 @@ func init() { cmd := &cobra.Command{ Use: usage, Short: shortDescription, - Long: formatHelp(longDescription, ""), + Long: logging.Untabify(longDescription, ""), Args: cobra.ExactArgs(1), SilenceUsage: true, SilenceErrors: true, - Example: formatHelp(example, " "), + Example: logging.Untabify(example, " "), RunE: func(cmd *cobra.Command, args []string) error { return runWith(args[0], flags) }, diff --git a/internal/logging/logger.go b/internal/logging/logger.go index 6637ecd..bef6b1f 100644 --- a/internal/logging/logger.go +++ b/internal/logging/logger.go @@ -45,7 +45,7 @@ type infoLogger struct { // Logger.Info() implementation: log to stderr func (l infoLogger) Info(a ...interface{}) { - pretty := prettify(a...) + pretty := Prettify(a...) _, err := fmt.Fprintln(l.out, pretty...) if err != nil { logFatal(err) @@ -54,25 +54,13 @@ func (l infoLogger) Info(a ...interface{}) { // Logger.Infof() implementation: log to stderr func (l infoLogger) Infof(format string, a ...interface{}) { - pretty := prettify(a...) + pretty := Prettify(a...) _, err := fmt.Fprintf(l.out, format, pretty...) if err != nil { logFatal(err) } } -func prettify(a ...interface{}) []interface{} { - var pretty []interface{} - for _, v := range a { - if p, ok := v.(Pretty); ok { - pretty = append(pretty, p.Pretty()) - } else { - pretty = append(pretty, v) - } - } - return pretty -} - // ------------------------------ // terseLogger @@ -125,4 +113,5 @@ func (l verboseLogger) Verbose() bool { func (l verboseLogger) String() string { return "verbose" -} \ No newline at end of file +} + diff --git a/internal/logging/pretty.go b/internal/logging/pretty.go index bbc6b63..a56498e 100644 --- a/internal/logging/pretty.go +++ b/internal/logging/pretty.go @@ -1,5 +1,25 @@ package logging +import "regexp" + type Pretty interface { Pretty() string } + +func Prettify(a ...interface{}) []interface{} { + var pretty []interface{} + for _, v := range a { + if p, ok := v.(Pretty); ok { + pretty = append(pretty, p.Pretty()) + } else { + pretty = append(pretty, v) + } + } + return pretty +} + +func Untabify(text string, indent string) string { + // TODO: support multi-level indent + return regexp.MustCompile(`(?m)^[\t ]+`).ReplaceAllString(text, indent) +} + diff --git a/internal/objects/swift_object.go b/internal/objects/swift_object.go index 21e12ec..8a0354f 100644 --- a/internal/objects/swift_object.go +++ b/internal/objects/swift_object.go @@ -23,8 +23,21 @@ type SwiftObject struct { swiftConnection *swift.Connection } +func (obj *SwiftObject) Pretty() string { + format := `SwiftObject { + container: '%v' + objectName: '%v' + cnxParams: %v + logger: %v + swiftConnection: %v + }` + format = logging.Untabify(format, " ") + args := logging.Prettify(obj.container, obj.objectName, obj.cnxParams, obj.logger, obj.swiftConnection) + return fmt.Sprintf(format, args...) +} + func (obj *SwiftObject) String() string { - return fmt.Sprintf("SwiftObject { container: '%v', objectName: '%v', cnxParams: %v, logger: %v, swiftConnection: %v", + return fmt.Sprintf("SwiftObject { container: '%v', objectName: '%v', cnxParams: %v, logger: %v, swiftConnection: %v }", obj.container, obj.objectName, obj.cnxParams, @@ -33,6 +46,8 @@ func (obj *SwiftObject) String() string { ) } + + // Endpoint returns the Swift authentication URL func (obj *SwiftObject) Endpoint() *url.URL { return obj.cnxParams.AuthURL diff --git a/internal/protocols/swift_utils.go b/internal/protocols/swift_utils.go index 4d99d82..a460be7 100644 --- a/internal/protocols/swift_utils.go +++ b/internal/protocols/swift_utils.go @@ -3,6 +3,8 @@ package protocols import ( "fmt" "net/url" + + "github.com/dmolesUC3/cos/internal/logging" ) // The SwiftConnectionParams struct encapsulates the authentication parameters @@ -14,22 +16,38 @@ type SwiftConnectionParams struct { } func (p SwiftConnectionParams) String() string { - var authURLStr string - if p.AuthURL == nil { - authURLStr = "" - } else { - authURLStr = p.AuthURL.String() - } + return fmt.Sprintf( + "SwiftConnectionParams { Username: %v, APIKey: %v, AuthURL: %v }", + p.UserName, p.apiKeyStr(), p.authUrlStr(), + ) +} +func (p SwiftConnectionParams) Pretty() string { + format := `SwiftConnectionParams { + Username: %v + APIKey: %v + AuthURL: %v + }` + format = logging.Untabify(format, " ") + return fmt.Sprintf(format, p.UserName, p.apiKeyStr(), p.authUrlStr()) +} + +func (p SwiftConnectionParams) apiKeyStr() string { var apiKeyStr string if p.APIKey == "" { apiKeyStr = "" } else { apiKeyStr = "" } + return apiKeyStr +} - return fmt.Sprintf( - "SwiftConnectionParams { Username: %v, APIKey: %v, AuthURL: %v }", - p.UserName, apiKeyStr, authURLStr, - ) +func (p SwiftConnectionParams) authUrlStr() string { + var authURLStr string + if p.AuthURL == nil { + authURLStr = "" + } else { + authURLStr = p.AuthURL.String() + } + return authURLStr }