-
Notifications
You must be signed in to change notification settings - Fork 91
/
main.go
144 lines (128 loc) · 3.14 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package main
import (
"errors"
"fmt"
"os"
"strings"
"time"
"github.com/nsf/termbox-go"
"go.etcd.io/bbolt"
)
var ProgramName = "boltbrowser"
var VersionNum = 2.0
var databaseFiles []string
var db *bbolt.DB
var memBolt *BoltDB
var currentFilename string
const DefaultDBOpenTimeout = time.Second
var AppArgs struct {
DBOpenTimeout time.Duration
ReadOnly bool
NoValue bool
}
func init() {
AppArgs.DBOpenTimeout = DefaultDBOpenTimeout
AppArgs.ReadOnly = false
}
func parseArgs() {
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] <filename(s)>\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)
}
defer termbox.Close()
style := defaultStyle()
termbox.SetOutputMode(termbox.Output256)
for _, databaseFile := range databaseFiles {
currentFilename = databaseFile
db, err = bbolt.Open(databaseFile, 0600, &bbolt.Options{Timeout: AppArgs.DBOpenTimeout})
if err == bbolt.ErrTimeout {
termbox.Close()
fmt.Printf("File %s is locked. Make sure it's not used by another app and try again\n", databaseFile)
os.Exit(1)
} else if err != nil {
if len(databaseFiles) > 1 {
mainLoop(nil, style)
continue
} else {
termbox.Close()
fmt.Printf("Error reading file: %q\n", err.Error())
os.Exit(1)
}
}
// First things first, load the database into memory
memBolt.refreshDatabase()
if AppArgs.ReadOnly {
// If we're opening it in readonly mode, close it now
db.Close()
}
// Kick off the UI loop
mainLoop(memBolt, style)
defer db.Close()
}
}