-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
83 lines (69 loc) · 2.05 KB
/
Copy pathmain.go
File metadata and controls
83 lines (69 loc) · 2.05 KB
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
package main
import (
"bufio"
"flag"
"fmt"
"log"
"log/slog"
"os"
"strings"
tea "charm.land/bubbletea/v2"
"hostettler.dev/dnc/util"
)
func main() {
demo := flag.Bool("demo", false, "start with a temporary demo database")
backup := flag.String("backup", "", "copy database to specified file path")
restore := flag.String("restore", "", "overwrite database with specified file path")
flag.Parse()
cfgDir := util.DefaultConfigDir()
logCleanup, err := util.InitLogger(cfgDir, 5*1024*1024)
if err != nil {
log.Fatal("failed to initialise log file: ", err)
}
defer logCleanup()
dbPath := util.DefaultConfig(cfgDir).DatabasePath
if *backup != "" {
slog.Info("backup requested", "src", dbPath, "dst", *backup)
if err := util.CopyFile(dbPath, *backup); err != nil {
log.Fatal("backup failed: ", err)
}
fmt.Printf("Database backed up to %s\n", *backup)
os.Exit(0)
}
if *restore != "" {
confirmation_string := "I am aware that this action overwrites all my current data"
fmt.Println("WARNING: This will overwrite all current data in your database.")
fmt.Printf("Type the following to confirm: %s\n> ", confirmation_string)
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
input := strings.TrimRight(scanner.Text(), "\r\n")
if input != confirmation_string {
fmt.Println("Aborted.")
os.Exit(1)
}
slog.Info("restore requested", "src", *restore, "dst", dbPath)
if err := util.CopyFile(*restore, dbPath); err != nil {
log.Fatal("restore failed: ", err)
}
fmt.Printf("Database restored from %s\n", *restore)
os.Exit(0)
}
slog.Info("dnc starting", "demo", *demo)
config, cleanup, err := util.GetConfig(cfgDir, *demo)
if err != nil {
slog.Error("failed to load config", "error", err)
log.Fatal(err)
}
app, err := NewApp(config, cleanup)
if err != nil {
slog.Error("failed to initialise app", "error", err)
log.Fatal(err)
}
defer app.Close()
p := tea.NewProgram(app)
if _, err := p.Run(); err != nil {
slog.Error("program exited with error", "error", err)
log.Fatal(err)
}
slog.Info("dnc exited cleanly")
}