Skip to content

Commit

Permalink
Improved log output, somewhat
Browse files Browse the repository at this point in the history
  • Loading branch information
David Moles committed Jan 15, 2019
1 parent 1062ec5 commit 82a530f
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 33 deletions.
22 changes: 15 additions & 7 deletions cmd/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cmd

import (
"fmt"
"regexp"

"github.com/dmolesUC3/cos/internal/logging"
"github.com/dmolesUC3/cos/internal/objects"
Expand Down Expand Up @@ -57,20 +56,29 @@ 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'}",
f.Verbose, f.Expected, f.Algorithm, f.Endpoint, f.Region,
)
}



// ------------------------------------------------------------
// 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)
Expand Down Expand Up @@ -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)
},
Expand Down
19 changes: 4 additions & 15 deletions internal/logging/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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

Expand Down Expand Up @@ -125,4 +113,5 @@ func (l verboseLogger) Verbose() bool {

func (l verboseLogger) String() string {
return "verbose"
}
}

20 changes: 20 additions & 0 deletions internal/logging/pretty.go
Original file line number Diff line number Diff line change
@@ -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)
}

17 changes: 16 additions & 1 deletion internal/objects/swift_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down
38 changes: 28 additions & 10 deletions internal/protocols/swift_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package protocols
import (
"fmt"
"net/url"

"github.com/dmolesUC3/cos/internal/logging"
)

// The SwiftConnectionParams struct encapsulates the authentication parameters
Expand All @@ -14,22 +16,38 @@ type SwiftConnectionParams struct {
}

func (p SwiftConnectionParams) String() string {
var authURLStr string
if p.AuthURL == nil {
authURLStr = "<nil>"
} 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 = "<not set>"
} else {
apiKeyStr = "<hidden>"
}
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 = "<nil>"
} else {
authURLStr = p.AuthURL.String()
}
return authURLStr
}

0 comments on commit 82a530f

Please sign in to comment.