/
jm.go
756 lines (676 loc) · 18.4 KB
/
jm.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
// Copyright 2017 Javier Arevalo <jare@iguanademos.com>
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"sort"
"strings"
"unicode"
"github.com/mattn/go-runewidth"
"github.com/mitchellh/go-homedir"
"github.com/nsf/termbox-go"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"code.cloudfoundry.org/bytefmt"
)
// ------------------
const version = "1.1.1"
// ------------------
func tbprint(x, y int, fg, bg termbox.Attribute, msg string) int {
for _, c := range msg {
termbox.SetCell(x, y, c, fg, bg)
x += runewidth.RuneWidth(c)
}
return x
}
func tbprintw(x, y, w int, fg, bg termbox.Attribute, msg string) int {
w += x
for _, c := range msg {
termbox.SetCell(x, y, c, fg, bg)
x += runewidth.RuneWidth(c)
if x >= w {
break
}
}
return x
}
func fill(x, y, w, h int, cell termbox.Cell) {
for ly := 0; ly < h; ly++ {
for lx := 0; lx < w; lx++ {
termbox.SetCell(x+lx, y+ly, cell.Ch, cell.Fg, cell.Bg)
}
}
}
// ------------------
// ByFolderThenName implements sort.Interface for []os.FileInfo based on
// leaving folders first and otherwise sorting by case-insensitive name
type ByFolderThenName []os.FileInfo
func (a ByFolderThenName) Len() int { return len(a) }
func (a ByFolderThenName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByFolderThenName) Less(i, j int) bool {
if a[i].IsDir() == a[j].IsDir() {
return strings.ToLower(a[i].Name()) < strings.ToLower(a[j].Name())
}
return a[i].IsDir()
}
// Panel contains the state of one panel
type Panel struct {
Cwd string
Entries []os.FileInfo
Top int
Cursor int
Selected map[int]bool
}
// NewPanel creates and initializes a new panel given a directory
// and an entry to set the cursor at
func NewPanel(cwd string, cursor string) (*Panel, error) {
p := &Panel{}
err := p.Reset(cwd, cursor)
return p, err
}
// Reset reinitializes a panel to given a directory
// and an entry to set the cursor at
func (p *Panel) Reset(cwd string, cursor string) error {
entries, err := ioutil.ReadDir(cwd)
sort.Sort(ByFolderThenName(entries))
p.Cwd = cwd
p.Entries = entries
p.Top = 0
p.Cursor = 0
p.Selected = make(map[int]bool)
for i, v := range p.Entries {
if v.Name() == cursor {
p.Cursor = i
break
}
}
return err
}
// Refresh reinitializes a panel with its directory's contents,
// keeping the current cursor and selection if possible
func (p *Panel) Refresh() error {
if len(p.Entries) == 0 {
return p.Reset(p.Cwd, "")
}
var selection = make(map[string]bool)
top := p.Top
cursor := p.Entries[p.Cursor].Name()
cursorIndex := p.Cursor
for k := range p.Selected {
selection[p.Entries[k].Name()] = true
}
err := p.Reset(p.Cwd, cursor)
if len(p.Entries) == 0 {
return err
}
p.Top = top
if err != nil {
return err
}
if p.Entries[p.Cursor].Name() != cursor {
if cursorIndex < len(p.Entries) {
p.Cursor = cursorIndex
} else {
p.Cursor = len(p.Entries) - 1
}
}
for k, v := range p.Entries {
if selection[v.Name()] {
p.Selected[k] = true
}
}
return nil
}
func permc(c string, mode os.FileMode) string {
if mode != 0 {
return c[:1]
}
if len(c) > 1 {
return c[1:]
}
return " "
}
func permissions(mode os.FileMode) string {
s := ""
//No hidden for you s = s + permc("H.", mode&os.ModeHidden)
s = s + permc("d.", mode&os.ModeDir)
s = s + permc("r-", mode&(1<<8))
s = s + permc("w-", mode&(1<<7))
s = s + permc("x-", mode&(1<<6))
s = s + permc("r-", mode&(1<<5))
s = s + permc("w-", mode&(1<<4))
s = s + permc("x-", mode&(1<<3))
s = s + permc("r-", mode&(1<<2))
s = s + permc("w-", mode&(1<<1))
s = s + permc("x-", mode&1)
return s
}
// Render draws a panel at the given position, restricted
// to the given dimensions, and with different colors if it's
// the active panel
func (p *Panel) Render(x, w, h int, active bool) {
for i := 0; i < (len(p.Entries)-p.Top) && i < h; i++ {
var fg, bg termbox.Attribute = termbox.ColorDefault, termbox.ColorDefault
n := i + p.Top
e := p.Entries[n]
if active {
if p.Selected[n] {
if n == p.Cursor {
bg = termbox.ColorCyan
fg = termbox.ColorBlack
} else {
bg = termbox.ColorBlue
}
} else if n == p.Cursor {
bg = termbox.ColorGreen
fg = termbox.ColorBlack
}
} else {
if p.Selected[n] {
bg = termbox.ColorBlue
}
}
if e.IsDir() {
if fg == termbox.ColorBlack {
fg = termbox.ColorBlue
} else {
fg = termbox.ColorYellow
}
}
fn := e.Name()
if e.IsDir() {
fn = fn + string(os.PathSeparator)
}
if p.Selected[n] {
fn = "*" + fn
}
if w > 50 {
fn = fmt.Sprintf("%-*.*s %-*.*s %*.*s", w-32, w-32, fn, 20, 20, e.ModTime().Format("02 Jan 2006 15:04:05"), 10, 10, bytefmt.ByteSize(uint64(e.Size())))
} else if w > 30 {
fn = fmt.Sprintf("%-*.*s %*.*s", w-11, w-11, fn, 10, 10, bytefmt.ByteSize(uint64(e.Size())))
}
tbprintw(x, i, w, fg, bg, fn)
}
nx := tbprintw(x, h, w, termbox.ColorWhite, termbox.ColorRed, p.Cwd)
if p.Cursor < len(p.Entries) {
e := p.Entries[p.Cursor]
fn := fmt.Sprintf("%s %s %d %s", permissions(e.Mode()), e.ModTime().Format("Mon, 02 Jan 2006 15:04:05"), e.Size(), e.Name())
tbprintw(nx+1, h, w-(nx+1-x), termbox.ColorYellow, termbox.ColorRed, fn)
}
}
// ClampPos limits the state of the panel to valid values
func (p *Panel) ClampPos(h int) {
if p.Cursor >= len(p.Entries) {
p.Cursor = len(p.Entries) - 1
}
if p.Cursor < 0 {
p.Cursor = 0
}
if p.Cursor < p.Top {
p.Top = p.Cursor
} else if p.Cursor >= p.Top+h {
p.Top = p.Cursor - h + 1
}
if p.Top >= len(p.Entries) {
p.Top = len(p.Entries) - 1
}
if p.Top < 0 {
p.Top = 0
}
}
// ------------------
var cursorCache = make(map[string]string)
func getCachedCursor(key string) string {
if runtime.GOOS != "unix" {
key = strings.ToLower(key)
}
return cursorCache[key]
}
func setCachedCursor(key string, val string) {
if runtime.GOOS != "unix" {
key = strings.ToLower(key)
}
cursorCache[key] = val
}
// ------------------
var lp, rp *Panel
var ap, op *Panel
var status string
var bookmarks map[string]string
// ClipboardType is an enum for the type of files that are stored in the clipboard
type clipboardMode int
const (
clipboardModeNone clipboardMode = iota
clipboardModeCopy
clipboardModeCut
)
// Clipboard manages the yanked/cut files
type Clipboard struct {
Files []string
Mode clipboardMode
}
// IsEmpty returns true if the clipboard contains no data
func (c *Clipboard) IsEmpty() bool {
return len(c.Files) == 0
}
// Reset clears all contents of the clipboard
func (c *Clipboard) Reset() {
c.Files = nil
c.Mode = clipboardModeNone
}
// BeginCut clears the clipbaord and initiates a Cut operation
func (c *Clipboard) BeginCut() {
c.Files = nil
c.Mode = clipboardModeCut
}
// BeginCopy clears the clipbaord and initiates a Copy operation
func (c *Clipboard) BeginCopy() {
c.Files = nil
c.Mode = clipboardModeCopy
}
// Add adds a new file to the clipboard if it isn't already in there
func (c *Clipboard) Add(file string) {
for _, s := range c.Files {
if s == file {
return
}
}
c.Files = append(c.Files, file)
}
var clipboard = Clipboard{}
func redrawAll() int {
const coldef = termbox.ColorDefault
termbox.Clear(coldef, coldef)
w, h := termbox.Size()
midx := w / 2
lp.ClampPos(h - 2)
rp.ClampPos(h - 2)
fill(midx, 0, 1, h-2, termbox.Cell{Ch: ' ', Bg: termbox.ColorRed})
fill(0, h-2, w, 1, termbox.Cell{Ch: ' ', Bg: termbox.ColorRed})
lp.Render(0, midx, h-2, lp == ap)
rp.Render(midx+1, w-midx-1, h-2, rp == ap)
// HACK:
// Some terminals can't hide the cursor, which may mean that writing to the bottom rightmost
// character will cause the cursor to wrap to the next line and make the terminal scroll
// one line. This ruins the display! So use w-1 to prevent writing to that last char.
if status != "" {
tbprintw(0, h-1, w-1, termbox.ColorMagenta, coldef, status)
} else {
var s = "[ESC,q quit] [TAB switch] [SPC select] [ARROWS nav] [r refresh] [c Copy] [m Move] [DD Delete] [: Shell] [b/B Bookmarks] [y/Y Yank] [x/X Cut]"
if !clipboard.IsEmpty() {
if clipboard.Mode == clipboardModeCopy {
s = s + fmt.Sprintf(" [p Copy %d files]", len(clipboard.Files))
} else if clipboard.Mode == clipboardModeCut {
s = s + fmt.Sprintf(" [p Move %d files]", len(clipboard.Files))
}
}
tbprintw(0, h-1, w-1, coldef, coldef, s)
}
status = ""
termbox.Flush()
return h - 2
}
func redrawStatus(status string) {
const coldef = termbox.ColorDefault
w, h := termbox.Size()
fill(0, h-1, w, 1, termbox.Cell{Ch: ' '})
tbprint(0, h-1, coldef, coldef, status)
termbox.Flush()
}
func runShell() string {
termbox.Close()
err := RunShell(ap.Cwd)
termbox.Init()
if err == nil {
return ""
}
return err.Error()
}
// ------------------
var configFile string
type config struct {
LeftPath string
RightPath string
CursorCache map[string]string
Bookmarks map[string]string
}
func writeConfig() error {
var c config
c.LeftPath = lp.Cwd
c.RightPath = rp.Cwd
c.CursorCache = cursorCache
c.Bookmarks = bookmarks
b, err := json.MarshalIndent(c, "", " ")
if err != nil {
return err
}
f, err := os.Create(configFile)
if err != nil {
return err
}
defer f.Close()
f.WriteString(string(b))
return nil
}
// ------------------
func getCommandArguments() ([]string, string) {
var src []string
dst := op.Cwd
if len(ap.Selected) > 0 {
for k := range ap.Selected {
f := ap.Entries[k]
src = append(src, filepath.Join(ap.Cwd, f.Name()))
}
} else if len(ap.Entries) > 0 {
f := ap.Entries[ap.Cursor]
src = append(src, filepath.Join(ap.Cwd, f.Name()))
}
return src, dst
}
func run(ld, rd string) {
err := termbox.Init()
if err != nil {
panic(err)
}
defer termbox.Close()
termbox.SetInputMode(termbox.InputEsc)
lp, _ = NewPanel(ld, getCachedCursor(ld))
rp, _ = NewPanel(rd, getCachedCursor(rd))
ap, op = lp, rp
pagesize := redrawAll()
// Used by commands that work with multiple keystrokes, eg DD
prefixCommand := ""
mainloop:
for {
newCommand := ""
switch ev := termbox.PollEvent(); ev.Type {
case termbox.EventKey:
// Prefix commands
if prefixCommand == "b" {
drives, _ := GetDrives()
drive := unicode.ToUpper(ev.Ch)
newCwd := ""
if drives[drive] {
newCwd = string(drive) + `:\`
} else if strings.IndexRune("0123456789", ev.Ch) >= 0 {
newCwd = bookmarks[string(ev.Ch)]
} else if ev.Ch == '/' {
newCwd = filepath.VolumeName(ap.Cwd) + string(os.PathSeparator)
} else if ev.Ch == '~' {
newCwd, _ = homedir.Dir()
}
if newCwd != "" {
ap.Reset(newCwd, getCachedCursor(newCwd))
}
break
} else if prefixCommand == "B" {
if strings.IndexRune("0123456789", ev.Ch) >= 0 {
bookmarks[string(ev.Ch)] = ap.Cwd
}
break
} else if prefixCommand == "D" {
clipboard.Reset()
if ev.Ch == 'D' {
src, _ := getCommandArguments()
for i, s := range src {
redrawStatus(fmt.Sprintf("Deleting file %d/%d: %s", i+1, len(src), s))
err := CommandDelete(s)
if err != nil {
status = status + " " + err.Error()
}
}
ap.Refresh()
if ap.Cwd == op.Cwd {
op.Refresh()
}
}
break
}
// Regular commands (or detecting prefixes)
if ev.Key == termbox.KeyEsc || ev.Ch == 'q' || ev.Ch == 'Q' {
if prefixCommand == "" {
break mainloop
}
} else if ev.Key == termbox.KeyTab {
ap, op = op, ap
} else if ev.Key == termbox.KeyArrowUp || ev.Ch == 'k' {
ap.Cursor--
} else if ev.Key == termbox.KeyArrowDown || ev.Ch == 'j' {
ap.Cursor++
} else if ev.Key == termbox.KeyPgup || ev.Ch == 'u' {
ap.Cursor -= pagesize
} else if ev.Key == termbox.KeyPgdn || ev.Ch == 'i' {
ap.Cursor += pagesize
} else if ev.Key == termbox.KeyHome || ev.Ch == 'U' {
ap.Cursor = 0
} else if ev.Key == termbox.KeyEnd || ev.Ch == 'I' {
if len(ap.Entries) > 0 {
ap.Cursor = len(ap.Entries) - 1
}
} else if ev.Key == termbox.KeyArrowLeft || ev.Ch == 'h' {
if ap.Cursor < len(ap.Entries) {
setCachedCursor(ap.Cwd, ap.Entries[ap.Cursor].Name())
setCachedCursor(filepath.Dir(ap.Cwd), filepath.Base(ap.Cwd))
}
ap.Reset(filepath.Dir(ap.Cwd), getCachedCursor(filepath.Dir(ap.Cwd)))
} else if ev.Key == termbox.KeyArrowRight || ev.Ch == 'l' {
if ap.Cursor < len(ap.Entries) && ap.Entries[ap.Cursor].IsDir() {
setCachedCursor(ap.Cwd, ap.Entries[ap.Cursor].Name())
n := filepath.Join(ap.Cwd, ap.Entries[ap.Cursor].Name())
ap.Reset(n, getCachedCursor(n))
}
} else if ev.Key == termbox.KeySpace {
if ap.Cursor < len(ap.Entries) {
if ap.Selected[ap.Cursor] {
delete(ap.Selected, ap.Cursor)
} else {
ap.Selected[ap.Cursor] = true
}
}
} else if ev.Ch == 'a' {
if len(ap.Selected) == len(ap.Entries) {
ap.Selected = make(map[int]bool)
} else {
for i := range ap.Entries {
ap.Selected[i] = true
}
}
} else if ev.Key == termbox.KeyF5 || ev.Ch == 'r' {
ap.Refresh()
op.Refresh()
} else if ev.Ch == ':' {
status = runShell()
ap.Refresh()
op.Refresh()
} else if ev.Ch == 'b' {
if prefixCommand == "" {
if runtime.GOOS == "windows" {
status = "Press a drive letter or bookmark to cd to"
} else {
status = "Press a bookmark to cd to"
}
newCommand = "b"
}
} else if ev.Ch == 'B' {
if prefixCommand == "" {
status = "Press digit to bookmark to"
newCommand = "B"
}
} else if ev.Ch == 'c' {
clipboard.Reset()
if ap.Cwd == op.Cwd {
// Maybe add a way to duplicate files?
break
}
src, dst := getCommandArguments()
for i, s := range src {
redrawStatus(fmt.Sprintf("Copying file %d/%d: %s", i+1, len(src), s))
err := CommandCopy(s, dst)
if err != nil {
status = status + " " + err.Error()
}
}
op.Refresh()
if ap.Cwd == op.Cwd {
ap.Refresh()
}
} else if ev.Ch == 'm' {
clipboard.Reset()
if ap.Cwd == op.Cwd {
break
}
src, dst := getCommandArguments()
for i, s := range src {
redrawStatus(fmt.Sprintf("Moving file %d/%d: %s", i+1, len(src), s))
err := CommandMove(s, dst)
if err != nil {
status = status + " " + err.Error()
}
}
ap.Refresh()
op.Refresh()
} else if ev.Ch == 'x' || ev.Ch == 'X' {
if ev.Ch == 'x' || clipboard.Mode != clipboardModeCut {
clipboard.BeginCut()
}
src, _ := getCommandArguments()
for _, s := range src {
clipboard.Add(s)
}
} else if ev.Ch == 'y' || ev.Ch == 'Y' {
if ev.Ch == 'y' || clipboard.Mode != clipboardModeCopy {
clipboard.BeginCopy()
}
src, _ := getCommandArguments()
for _, s := range src {
clipboard.Add(s)
}
} else if ev.Ch == 'p' {
if clipboard.IsEmpty() {
break
}
dst := ap.Cwd
var newClipboard []string
for i, s := range clipboard.Files {
var err error
if clipboard.Mode == clipboardModeCopy {
redrawStatus(fmt.Sprintf("Copying file %d/%d: %s", i+1, len(clipboard.Files), s))
err = CommandCopy(s, dst)
} else {
redrawStatus(fmt.Sprintf("Moving file %d/%d: %s", i+1, len(clipboard.Files), s))
err = CommandMove(s, dst)
}
if err != nil {
status = status + " " + err.Error()
newClipboard = append(newClipboard, s)
} else {
file := filepath.Base(s)
newClipboard = append(newClipboard, filepath.Join(dst, file))
}
}
clipboard.Files = nil
for _, s := range newClipboard {
clipboard.Add(s)
}
ap.Refresh()
op.Refresh()
} else if ev.Ch == 'D' {
src, _ := getCommandArguments()
if len(src) > 0 {
status = fmt.Sprintf("Press D again to confirm deleting %d files (%s)", len(src), strings.Join(src, " "))
newCommand = "D"
}
}
case termbox.EventError:
panic(ev.Err)
}
pagesize = redrawAll()
// Keep prefix if one was stored by a command
if newCommand != "" {
prefixCommand = newCommand
} else {
prefixCommand = ""
}
}
writeConfig()
}
// ------------------
var showVersion = false
var logVerbose = false
var logFile io.Writer
// Logf writes to the log file if it is enabled
func Logf(args ...interface{}) {
if logFile != nil {
fmt.Fprintf(logFile, args[0].(string), args[1:]...)
}
}
var rootCmd = &cobra.Command{
Use: "jm [left path] [right path]",
Short: "jm is a small terminal-based file manager",
Long: `A simple and small terminal-based file manager that tries to be friendly`,
Run: func(cmd *cobra.Command, args []string) {
if showVersion {
fmt.Println("jm version " + version)
os.Exit(0)
}
if logVerbose {
home, _ := homedir.Dir()
logFile, _ = os.Create(filepath.Join(home, ".jm-log"))
}
// Read config file
viper.SetConfigFile(configFile)
viper.SetConfigType("json")
// Ignore errors if config file does not exist.
viper.ReadInConfig()
cursorCache = viper.GetStringMapString("CursorCache")
bookmarks = viper.GetStringMapString("Bookmarks")
// Precedence to paths from the command line
// Cwd and $HOME as last resort defaults
if len(args) < 2 {
d := viper.GetString("LeftPath")
if d == "" {
d, _ = os.Getwd()
}
args = append(args, d)
}
if len(args) < 2 {
d := viper.GetString("RightPath")
if d == "" {
d, _ = homedir.Dir()
}
args = append(args, d)
}
args[0], _ = filepath.Abs(args[0])
args[1], _ = filepath.Abs(args[1])
run(args[0], args[1])
},
}
func main() {
viper.SetDefault("LeftPath", "")
viper.SetDefault("RightPath", "")
viper.SetDefault("CursorCache", map[string]string{})
viper.SetDefault("Bookmarks", map[string]string{})
home, _ := homedir.Dir()
configFile = filepath.Join(home, ".jm")
rootCmd.PersistentFlags().StringVarP(&configFile, "config", "c", configFile, "config file")
rootCmd.PersistentFlags().BoolVarP(&showVersion, "version", "V", showVersion, "print version")
rootCmd.PersistentFlags().BoolVarP(&logVerbose, "log", "l", logVerbose, `verbose log (to file "`+filepath.Join(home, ".jm-log")+`")`)
if err := rootCmd.Execute(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(-1)
}
}