diff options
author | alabid <alabidan@gmail.com> | 2014-10-21 15:53:59 -0400 |
---|---|---|
committer | alabid <alabidan@gmail.com> | 2014-10-21 16:15:33 -0400 |
commit | 2221a861196ab6c61b215913055cfdd079794a99 (patch) | |
tree | 81d5dbc624c0109b94253547d35a4a55262bd098 | |
parent | 1aae0ae4951c882684983b36297ea0388802fd85 (diff) | |
download | mongo-2221a861196ab6c61b215913055cfdd079794a99.tar.gz |
TOOLS-299 deprecate util fmt functions
Former-commit-id: 3c30e352c2f57f098cf4ca80e06b648fb8055a57
-rw-r--r-- | bsondump/main/bsondump.go | 4 | ||||
-rw-r--r-- | common/log/tool_logger.go | 9 | ||||
-rw-r--r-- | common/log/tool_logger_test.go | 3 | ||||
-rw-r--r-- | common/options/options.go | 3 | ||||
-rw-r--r-- | common/util/fmt.go | 41 | ||||
-rw-r--r-- | mongodump/main/mongodump.go | 13 | ||||
-rw-r--r-- | mongorestore/metadata.go | 3 | ||||
-rw-r--r-- | mongotop/main/mongotop.go | 13 | ||||
-rw-r--r-- | mongotop/mongotop.go | 3 |
9 files changed, 27 insertions, 65 deletions
diff --git a/bsondump/main/bsondump.go b/bsondump/main/bsondump.go index b3141f155f9..42fd7b1d125 100644 --- a/bsondump/main/bsondump.go +++ b/bsondump/main/bsondump.go @@ -6,7 +6,6 @@ import ( "github.com/mongodb/mongo-tools/bsondump" "github.com/mongodb/mongo-tools/bsondump/options" commonopts "github.com/mongodb/mongo-tools/common/options" - "github.com/mongodb/mongo-tools/common/util" "os" ) @@ -18,7 +17,8 @@ func main() { extra, err := opts.Parse() if err != nil { - util.Panicf("error parsing command line options: %v", err) + log.Logf(log.Always, "error parsing command line options: %v", err) + os.Exit(1) } // print help, if specified diff --git a/common/log/tool_logger.go b/common/log/tool_logger.go index 91cf0795530..8f7577b0f34 100644 --- a/common/log/tool_logger.go +++ b/common/log/tool_logger.go @@ -3,14 +3,13 @@ package log import ( "fmt" "github.com/mongodb/mongo-tools/common/options" - "github.com/mongodb/mongo-tools/common/util" "io" "os" "sync" "time" ) -// Tool Logger constants +// Tool Logger verbosity constants const ( Always = iota Info @@ -18,6 +17,10 @@ const ( DebugHigh ) +const ( + ToolTimeFormat = "2006-01-02T15:04:05.000-0700" +) + //// Tool Logger Definition type ToolLogger struct { @@ -75,7 +78,7 @@ func NewToolLogger(verbosity *options.Verbosity) *ToolLogger { tl := &ToolLogger{ mutex: &sync.Mutex{}, writer: os.Stderr, // default to stderr - format: util.ToolTimeFormat, + format: ToolTimeFormat, } tl.SetVerbosity(verbosity) return tl diff --git a/common/log/tool_logger_test.go b/common/log/tool_logger_test.go index 308105100a5..a3cb7a618b0 100644 --- a/common/log/tool_logger_test.go +++ b/common/log/tool_logger_test.go @@ -3,7 +3,6 @@ package log import ( "bytes" "github.com/mongodb/mongo-tools/common/options" - "github.com/mongodb/mongo-tools/common/util" . "github.com/smartystreets/goconvey/convey" "os" "strings" @@ -53,7 +52,7 @@ func TestBasicToolLoggerFunctionality(t *testing.T) { So(l2, ShouldContainSubstring, "\t") timestamp := l2[:strings.Index(l2, "\t")] So(len(timestamp), ShouldBeGreaterThan, 1) - parsedTime, err := time.Parse(util.ToolTimeFormat, timestamp) + parsedTime, err := time.Parse(ToolTimeFormat, timestamp) So(err, ShouldBeNil) So(parsedTime, ShouldHappenOnOrAfter, oldTime) }) diff --git a/common/options/options.go b/common/options/options.go index 4e1c77c1718..dd21378e259 100644 --- a/common/options/options.go +++ b/common/options/options.go @@ -5,7 +5,6 @@ package options import ( "fmt" "github.com/jessevdk/go-flags" - "github.com/mongodb/mongo-tools/common/util" "os" ) @@ -140,7 +139,7 @@ func (self *ToolOptions) PrintHelp(force bool) bool { // is specified. func (self *ToolOptions) PrintVersion() bool { if self.Version { - util.Printlnf("%v version: %v", self.AppName, self.VersionStr) + fmt.Printf("%v version: %v\n", self.AppName, self.VersionStr) } return self.Version } diff --git a/common/util/fmt.go b/common/util/fmt.go deleted file mode 100644 index 1bedac97339..00000000000 --- a/common/util/fmt.go +++ /dev/null @@ -1,41 +0,0 @@ -package util - -import ( - "fmt" - "os" - "time" -) - -const ( - ToolTimeFormat = "2006-01-02T15:04:05.000-0700" -) - -// Panic with a formatted string -func Panicf(s string, args ...interface{}) { - panic(fmt.Sprintf(s, args...)) -} - -// Exitf printfs the string and args on one line, then exits with -// the supplied exit code -func Exitf(code int, s string, args ...interface{}) { - fmt.Println(fmt.Sprintf(s, args...)) - os.Exit(code) -} - -// Println a formatted string -func Printlnf(s string, args ...interface{}) (int, error) { - return fmt.Println(fmt.Sprintf(s, args...)) -} - -// PrintlnTimeStamped a formatted string along side the timestamp -func PrintlnTimeStamped(s string, args ...interface{}) (int, error) { - return fmt.Println(time.Now().Format(ToolTimeFormat), - fmt.Sprintf(s, args...)) -} - -// PrintfTimeStamped a formatted string along side the timestamp -func PrintfTimeStamped(s string, args ...interface{}) (int, error) { - line := fmt.Sprintf(s, args...) - return fmt.Printf(fmt.Sprintf("%v %v", time.Now().Format(ToolTimeFormat), - line)) -} diff --git a/mongodump/main/mongodump.go b/mongodump/main/mongodump.go index 44103611582..fb1b8e40c1f 100644 --- a/mongodump/main/mongodump.go +++ b/mongodump/main/mongodump.go @@ -4,7 +4,6 @@ import ( "fmt" "github.com/mongodb/mongo-tools/common/log" commonopts "github.com/mongodb/mongo-tools/common/options" - "github.com/mongodb/mongo-tools/common/util" "github.com/mongodb/mongo-tools/mongodump" "github.com/mongodb/mongo-tools/mongodump/options" "os" @@ -21,9 +20,9 @@ func main() { _, err := opts.Parse() if err != nil { - fmt.Printf("error parsing command line options: %v\n\n", err) - fmt.Printf("try 'mongodump --help' for more information\n") - os.Exit(2) + log.Logf(log.Always, "error parsing command line options: %v\n\n", err) + opts.PrintHelp(true) + return } // print help, if specified @@ -47,12 +46,14 @@ func main() { err = dump.Init() if err != nil { - util.Exitf(1, "%v", err) + log.Logf(log.Always, "%v", err) + os.Exit(1) } err = dump.Dump() if err != nil { - util.Exitf(1, "%v", err) + log.Logf(log.Always, "%v", err) + os.Exit(1) } } diff --git a/mongorestore/metadata.go b/mongorestore/metadata.go index af3714f2a36..8cb85990850 100644 --- a/mongorestore/metadata.go +++ b/mongorestore/metadata.go @@ -227,8 +227,7 @@ func (restore *MongoRestore) RestoreUsersOrRoles(collectionType string, intent * tempCol = restore.tempRolesCol tempColCommandField = "tempRolesCollection" default: - // panic should be fine here, since this implies a programmer (not user) error - util.Panicf("cannot use %v as a collection type in RestoreUsersOrRoles", collectionType) + return fmt.Errorf("cannot use %v as a collection type in RestoreUsersOrRoles", collectionType) } rawFile, err := os.Open(intent.BSONPath) diff --git a/mongotop/main/mongotop.go b/mongotop/main/mongotop.go index c40c0abad37..1e5fb05a055 100644 --- a/mongotop/main/mongotop.go +++ b/mongotop/main/mongotop.go @@ -4,7 +4,6 @@ package main import ( "github.com/mongodb/mongo-tools/common/db" commonopts "github.com/mongodb/mongo-tools/common/options" - "github.com/mongodb/mongo-tools/common/util" "github.com/mongodb/mongo-tools/mongotop" "github.com/mongodb/mongo-tools/mongotop/options" "github.com/mongodb/mongo-tools/mongotop/output" @@ -28,7 +27,8 @@ func main() { extra, err := opts.Parse() if err != nil { - util.Panicf("error parsing command line options: %v", err) + log.Logf(log.Always, "error parsing command line options: %v", err) + os.Exit(1) } // print help, if specified @@ -47,14 +47,16 @@ func main() { if len(extra) > 0 { sleeptime, err = strconv.Atoi(extra[0]) if err != nil { - util.Panicf("bad sleep time: %v", extra[0]) + log.Logf(log.Always, "bad sleep time: %v", extra[0]) + os.Exit(1) } } // create a session provider to connect to the db sessionProvider, err := db.InitSessionProvider(*opts) if err != nil { - util.Panicf("error initializing database session: %v", err) + log.Logf(log.Always, "error initializing database session: %v", err) + os.Exit(1) } // instantiate a mongotop instance @@ -69,6 +71,7 @@ func main() { // kick it off if err := top.Run(); err != nil { - util.Panicf("error running mongotop: %v", err) + log.Logf(log.Always, "error running mongotop: %v", err) + os.Exit(1) } } diff --git a/mongotop/mongotop.go b/mongotop/mongotop.go index 735d484d11a..0ce597b41e3 100644 --- a/mongotop/mongotop.go +++ b/mongotop/mongotop.go @@ -6,7 +6,6 @@ import ( "fmt" "github.com/mongodb/mongo-tools/common/db" commonopts "github.com/mongodb/mongo-tools/common/options" - "github.com/mongodb/mongo-tools/common/util" "github.com/mongodb/mongo-tools/mongotop/command" "github.com/mongodb/mongo-tools/mongotop/options" "github.com/mongodb/mongo-tools/mongotop/output" @@ -49,7 +48,7 @@ func (self *MongoTop) Run() error { if self.Options.Port != "" { connUrl = connUrl + ":" + self.Options.Port } - util.Printlnf("connected to: %v", connUrl) + fmt.Printf("connected to: %v\n", connUrl) // the results used to be compared to each other var previousResults command.Command |