From 3c22c6227b726d4ec9843faf4c26801a9eabf9c1 Mon Sep 17 00:00:00 2001 From: Andrew Slotin Date: Thu, 12 Oct 2017 11:46:12 +0700 Subject: [PATCH 1/3] Add *.db to .gitignore To avoid committing BoltDB database files by accident. --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 68162ee..c407834 100644 --- a/.gitignore +++ b/.gitignore @@ -28,3 +28,4 @@ boltbrowser # Test Database test.db +*.db From 46e67d58481dc14277e701947c462dbca08d7549 Mon Sep 17 00:00:00 2001 From: Andrew Slotin Date: Thu, 12 Oct 2017 11:47:00 +0700 Subject: [PATCH 2/3] Add --help flag support Currently executing `boltbrowser --help` creates a file with name `--help` in the current working directory, which is counterintuitive. This commit adds command-line flags support to boltbrowser and changes it to print out the usage message instead. --- boltbrowser.go | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/boltbrowser.go b/boltbrowser.go index 3fc886e..0dcd2e1 100644 --- a/boltbrowser.go +++ b/boltbrowser.go @@ -1,6 +1,7 @@ package main import ( + "flag" "fmt" "os" @@ -19,11 +20,19 @@ var memBolt *BoltDB var currentFilename string +func init() { + flag.Usage = func() { + fmt.Fprintf(os.Stdout, "Usage: %s \n", ProgramName) + } +} + func main() { var err error - if len(os.Args) < 2 { - fmt.Printf("Usage: %s \n", ProgramName) + flag.Parse() + + if flag.NArg() == 0 { + flag.Usage() os.Exit(1) } @@ -35,7 +44,7 @@ func main() { style := defaultStyle() termbox.SetOutputMode(termbox.Output256) - databaseFiles := os.Args[1:] + databaseFiles := flag.Args() for _, databaseFile := range databaseFiles { currentFilename = databaseFile db, err = bolt.Open(databaseFile, 0600, nil) From 6bfd6215e6eec4d9b8a07454bc65a9b9b27bcc10 Mon Sep 17 00:00:00 2001 From: Andrew Slotin Date: Thu, 12 Oct 2017 11:52:33 +0700 Subject: [PATCH 3/3] Exit with status code 2 if no files were given By convention exit(1) is reserved for general errors, while exit(2) means command misuse. Stdlib `flags` package invokes `os.Exit(2)` after printing out the usage message. This commit changes the empty files list exit code to be aligned with this behavior. --- boltbrowser.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/boltbrowser.go b/boltbrowser.go index 0dcd2e1..f2ddbb8 100644 --- a/boltbrowser.go +++ b/boltbrowser.go @@ -33,7 +33,7 @@ func main() { if flag.NArg() == 0 { flag.Usage() - os.Exit(1) + os.Exit(2) } err = termbox.Init()