From 218d3b5d63cb00325ddb79333062e1c9d4770849 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cenk=20Alt=C4=B1?= Date: Wed, 10 Dec 2025 22:24:29 -0500 Subject: [PATCH 1/4] Only ignore binary in root We're about to move everything into ./internal/boltbrowser to convert the project into importable boltbrowser package. x --- .gitignore | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index ed2766c..0395e25 100644 --- a/.gitignore +++ b/.gitignore @@ -27,8 +27,8 @@ build # Binaries *.exe -boltbrowser -boltbrowser.* +/boltbrowser +/boltbrowser.* # Test Database test.db From 470d3bb255bace12e148ccec9dc601afd3383d51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cenk=20Alt=C4=B1?= Date: Wed, 10 Dec 2025 22:26:14 -0500 Subject: [PATCH 2/4] Move all go files into an internal package --- bolt_model.go => internal/boltbrowser/bolt_model.go | 0 cursor.go => internal/boltbrowser/cursor.go | 0 main.go => internal/boltbrowser/main.go | 0 mainloop.go => internal/boltbrowser/mainloop.go | 0 mainloop_windows.go => internal/boltbrowser/mainloop_windows.go | 0 screen.go => internal/boltbrowser/screen.go | 0 screen_about.go => internal/boltbrowser/screen_about.go | 0 screen_browser.go => internal/boltbrowser/screen_browser.go | 0 stringify.go => internal/boltbrowser/stringify.go | 0 style.go => internal/boltbrowser/style.go | 0 10 files changed, 0 insertions(+), 0 deletions(-) rename bolt_model.go => internal/boltbrowser/bolt_model.go (100%) rename cursor.go => internal/boltbrowser/cursor.go (100%) rename main.go => internal/boltbrowser/main.go (100%) rename mainloop.go => internal/boltbrowser/mainloop.go (100%) rename mainloop_windows.go => internal/boltbrowser/mainloop_windows.go (100%) rename screen.go => internal/boltbrowser/screen.go (100%) rename screen_about.go => internal/boltbrowser/screen_about.go (100%) rename screen_browser.go => internal/boltbrowser/screen_browser.go (100%) rename stringify.go => internal/boltbrowser/stringify.go (100%) rename style.go => internal/boltbrowser/style.go (100%) diff --git a/bolt_model.go b/internal/boltbrowser/bolt_model.go similarity index 100% rename from bolt_model.go rename to internal/boltbrowser/bolt_model.go diff --git a/cursor.go b/internal/boltbrowser/cursor.go similarity index 100% rename from cursor.go rename to internal/boltbrowser/cursor.go diff --git a/main.go b/internal/boltbrowser/main.go similarity index 100% rename from main.go rename to internal/boltbrowser/main.go diff --git a/mainloop.go b/internal/boltbrowser/mainloop.go similarity index 100% rename from mainloop.go rename to internal/boltbrowser/mainloop.go diff --git a/mainloop_windows.go b/internal/boltbrowser/mainloop_windows.go similarity index 100% rename from mainloop_windows.go rename to internal/boltbrowser/mainloop_windows.go diff --git a/screen.go b/internal/boltbrowser/screen.go similarity index 100% rename from screen.go rename to internal/boltbrowser/screen.go diff --git a/screen_about.go b/internal/boltbrowser/screen_about.go similarity index 100% rename from screen_about.go rename to internal/boltbrowser/screen_about.go diff --git a/screen_browser.go b/internal/boltbrowser/screen_browser.go similarity index 100% rename from screen_browser.go rename to internal/boltbrowser/screen_browser.go diff --git a/stringify.go b/internal/boltbrowser/stringify.go similarity index 100% rename from stringify.go rename to internal/boltbrowser/stringify.go diff --git a/style.go b/internal/boltbrowser/style.go similarity index 100% rename from style.go rename to internal/boltbrowser/style.go From ea7e20341abfc729016cbc60bfe05e5500085ba3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cenk=20Alt=C4=B1?= Date: Wed, 10 Dec 2025 22:38:30 -0500 Subject: [PATCH 3/4] Update internal package name: main -> boltbrowser --- internal/boltbrowser/bolt_model.go | 2 +- internal/boltbrowser/cursor.go | 2 +- internal/boltbrowser/main.go | 2 +- internal/boltbrowser/mainloop.go | 2 +- internal/boltbrowser/mainloop_windows.go | 2 +- internal/boltbrowser/screen.go | 2 +- internal/boltbrowser/screen_about.go | 2 +- internal/boltbrowser/screen_browser.go | 2 +- internal/boltbrowser/stringify.go | 2 +- internal/boltbrowser/style.go | 2 +- 10 files changed, 10 insertions(+), 10 deletions(-) diff --git a/internal/boltbrowser/bolt_model.go b/internal/boltbrowser/bolt_model.go index b0850c6..85775b6 100644 --- a/internal/boltbrowser/bolt_model.go +++ b/internal/boltbrowser/bolt_model.go @@ -1,4 +1,4 @@ -package main +package boltbrowser import ( "errors" diff --git a/internal/boltbrowser/cursor.go b/internal/boltbrowser/cursor.go index b3b5aa4..b4711ec 100644 --- a/internal/boltbrowser/cursor.go +++ b/internal/boltbrowser/cursor.go @@ -1,4 +1,4 @@ -package main +package boltbrowser type Cursor struct { x int diff --git a/internal/boltbrowser/main.go b/internal/boltbrowser/main.go index 9a95ed5..3ef18c7 100644 --- a/internal/boltbrowser/main.go +++ b/internal/boltbrowser/main.go @@ -1,4 +1,4 @@ -package main +package boltbrowser import ( "errors" diff --git a/internal/boltbrowser/mainloop.go b/internal/boltbrowser/mainloop.go index 7fda410..6d9b8b5 100644 --- a/internal/boltbrowser/mainloop.go +++ b/internal/boltbrowser/mainloop.go @@ -1,7 +1,7 @@ //go:build !windows // +build !windows -package main +package boltbrowser import ( "os" diff --git a/internal/boltbrowser/mainloop_windows.go b/internal/boltbrowser/mainloop_windows.go index e8faddf..e286167 100644 --- a/internal/boltbrowser/mainloop_windows.go +++ b/internal/boltbrowser/mainloop_windows.go @@ -1,6 +1,6 @@ //go:build windows -package main +package boltbrowser // Windows doesn't support process backgrounding like *nix. // So we have a separate loop for it. diff --git a/internal/boltbrowser/screen.go b/internal/boltbrowser/screen.go index 7bf5637..c8877cb 100644 --- a/internal/boltbrowser/screen.go +++ b/internal/boltbrowser/screen.go @@ -1,4 +1,4 @@ -package main +package boltbrowser import "github.com/nsf/termbox-go" diff --git a/internal/boltbrowser/screen_about.go b/internal/boltbrowser/screen_about.go index 90a12cd..00dc24a 100644 --- a/internal/boltbrowser/screen_about.go +++ b/internal/boltbrowser/screen_about.go @@ -1,4 +1,4 @@ -package main +package boltbrowser import ( "fmt" diff --git a/internal/boltbrowser/screen_browser.go b/internal/boltbrowser/screen_browser.go index aeb26cf..0f5dd25 100644 --- a/internal/boltbrowser/screen_browser.go +++ b/internal/boltbrowser/screen_browser.go @@ -1,4 +1,4 @@ -package main +package boltbrowser import ( "encoding/json" diff --git a/internal/boltbrowser/stringify.go b/internal/boltbrowser/stringify.go index f7b1298..c2e5cfd 100644 --- a/internal/boltbrowser/stringify.go +++ b/internal/boltbrowser/stringify.go @@ -1,4 +1,4 @@ -package main +package boltbrowser import ( "encoding/binary" diff --git a/internal/boltbrowser/style.go b/internal/boltbrowser/style.go index 26c4311..3d0e371 100644 --- a/internal/boltbrowser/style.go +++ b/internal/boltbrowser/style.go @@ -1,4 +1,4 @@ -package main +package boltbrowser import "github.com/nsf/termbox-go" From f590c62b64ac21b37923924de07a5edfc8d52350 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cenk=20Alt=C4=B1?= Date: Wed, 10 Dec 2025 22:50:37 -0500 Subject: [PATCH 4/4] Update main func --- internal/boltbrowser/main.go | 89 +++++----------------------------- main.go | 88 +++++++++++++++++++++++++++++++++ pkg/boltbrowser/boltbrowser.go | 9 ++++ 3 files changed, 109 insertions(+), 77 deletions(-) create mode 100644 main.go create mode 100644 pkg/boltbrowser/boltbrowser.go diff --git a/internal/boltbrowser/main.go b/internal/boltbrowser/main.go index 3ef18c7..51acab3 100644 --- a/internal/boltbrowser/main.go +++ b/internal/boltbrowser/main.go @@ -1,10 +1,8 @@ package boltbrowser import ( - "errors" "fmt" "os" - "strings" "time" "github.com/nsf/termbox-go" @@ -14,6 +12,7 @@ import ( var ProgramName = "boltbrowser" var VersionNum = 2.0 +var AppArgs Args var databaseFiles []string var db *bbolt.DB var memBolt *BoltDB @@ -22,91 +21,26 @@ var currentFilename string const DefaultDBOpenTimeout = time.Second -var AppArgs struct { +type Args struct { DBOpenTimeout time.Duration ReadOnly bool NoValue bool } -func init() { - AppArgs.DBOpenTimeout = DefaultDBOpenTimeout - AppArgs.ReadOnly = false +func DefaultArgs() Args { + return Args{ + DBOpenTimeout: DefaultDBOpenTimeout, + } } -func parseArgs() { +func Main(args Args, files []string) error { + // Set the global args. This is done to convert main package into a library with minimal changes. + AppArgs, databaseFiles = args, files + var err error - if len(os.Args) == 1 { - printUsage(nil) - } - parms := os.Args[1:] - for i := range parms { - // All 'option' arguments start with "-" - if !strings.HasPrefix(parms[i], "-") { - databaseFiles = append(databaseFiles, parms[i]) - continue - } - if strings.Contains(parms[i], "=") { - // Key/Value pair Arguments - pts := strings.Split(parms[i], "=") - key, val := pts[0], pts[1] - switch key { - case "-timeout": - AppArgs.DBOpenTimeout, err = time.ParseDuration(val) - if err != nil { - // See if we can successfully parse by adding a 's' - AppArgs.DBOpenTimeout, err = time.ParseDuration(val + "s") - } - // If err is still not nil, print usage - if err != nil { - printUsage(err) - } - case "-readonly", "-ro": - if val == "true" { - AppArgs.ReadOnly = true - } - case "-no-value": - if val == "true" { - AppArgs.NoValue = true - } - case "-help": - printUsage(nil) - default: - printUsage(errors.New("Invalid option")) - } - } else { - // Single-word arguments - switch parms[i] { - case "-readonly", "-ro": - AppArgs.ReadOnly = true - case "-no-value": - AppArgs.NoValue = true - case "-help": - printUsage(nil) - default: - printUsage(errors.New("Invalid option")) - } - } - } -} - -func printUsage(err error) { - if err != nil { - fmt.Fprintf(os.Stderr, err.Error()) - } - fmt.Fprintf(os.Stderr, "Usage: %s [OPTIONS] \nOptions:\n", ProgramName) - fmt.Fprintf(os.Stderr, " -timeout=duration\n DB file open timeout (default 1s)\n") - fmt.Fprintf(os.Stderr, " -ro, -readonly \n Open the DB in read-only mode\n") - fmt.Fprintf(os.Stderr, " -no-value \n Do not display a value in left pane\n") -} - -func main() { - var err error - - parseArgs() - err = termbox.Init() if err != nil { - panic(err) + return err } defer termbox.Close() style := defaultStyle() @@ -141,4 +75,5 @@ func main() { mainLoop(memBolt, style) defer db.Close() } + return nil } diff --git a/main.go b/main.go new file mode 100644 index 0000000..a9ba9e1 --- /dev/null +++ b/main.go @@ -0,0 +1,88 @@ +package main + +import ( + "errors" + "fmt" + "os" + "strings" + "time" + + "github.com/br0xen/boltbrowser/pkg/boltbrowser" +) + +func main() { + args := boltbrowser.DefaultArgs() + files := parseArgs(&args) + err := boltbrowser.Main(args, files) + if err != nil { + fmt.Fprintf(os.Stderr, err.Error()) + os.Exit(1) + } +} + +func parseArgs(args *boltbrowser.Args) (databaseFiles []string) { + var err error + if len(os.Args) == 1 { + printUsage(nil) + } + parms := os.Args[1:] + for i := range parms { + // All 'option' arguments start with "-" + if !strings.HasPrefix(parms[i], "-") { + databaseFiles = append(databaseFiles, parms[i]) + continue + } + if strings.Contains(parms[i], "=") { + // Key/Value pair Arguments + pts := strings.Split(parms[i], "=") + key, val := pts[0], pts[1] + switch key { + case "-timeout": + args.DBOpenTimeout, err = time.ParseDuration(val) + if err != nil { + // See if we can successfully parse by adding a 's' + args.DBOpenTimeout, err = time.ParseDuration(val + "s") + } + // If err is still not nil, print usage + if err != nil { + printUsage(err) + } + case "-readonly", "-ro": + if val == "true" { + args.ReadOnly = true + } + case "-no-value": + if val == "true" { + args.NoValue = true + } + case "-help": + printUsage(nil) + default: + printUsage(errors.New("Invalid option")) + } + } else { + // Single-word arguments + switch parms[i] { + case "-readonly", "-ro": + args.ReadOnly = true + case "-no-value": + args.NoValue = true + case "-help": + printUsage(nil) + default: + printUsage(errors.New("Invalid option")) + } + } + } + return +} + +func printUsage(err error) { + if err != nil { + fmt.Fprintf(os.Stderr, err.Error()) + } + fmt.Fprintf(os.Stderr, "Usage: boltbrowser [OPTIONS] \nOptions:\n") + fmt.Fprintf(os.Stderr, " -timeout=duration\n DB file open timeout (default 1s)\n") + fmt.Fprintf(os.Stderr, " -ro, -readonly \n Open the DB in read-only mode\n") + fmt.Fprintf(os.Stderr, " -no-value \n Do not display a value in left pane\n") +} diff --git a/pkg/boltbrowser/boltbrowser.go b/pkg/boltbrowser/boltbrowser.go new file mode 100644 index 0000000..aafa4d8 --- /dev/null +++ b/pkg/boltbrowser/boltbrowser.go @@ -0,0 +1,9 @@ +package boltbrowser + +import internal "github.com/br0xen/boltbrowser/internal/boltbrowser" + +type Args = internal.Args + +var DefaultArgs = internal.DefaultArgs + +var Main = internal.Main