-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.go
125 lines (103 loc) · 2.64 KB
/
app.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
package main
import (
"context"
"crypto/sha256"
"database/sql"
"encoding/binary"
"path/filepath"
"github.com/rs/zerolog/log"
"github.com/notebox/nbfm/pkg/config"
"github.com/notebox/nbfm/pkg/editor"
"github.com/notebox/nbfm/pkg/identifier"
"github.com/notebox/nbfm/pkg/local"
"github.com/notebox/nbfm/pkg/local/note"
"github.com/notebox/nbfm/pkg/logger"
"github.com/notebox/nbfm/pkg/nav"
)
// App struct
type App struct {
Editor *editor.Editor
Logger *logger.NBLogger
ctx context.Context
replicaID uint32
db *sql.DB
wd *nav.WorkingDir
config *config.Config
}
func NewApp(config *config.Config) *App {
app := &App{config: config}
logger, err := logger.New(filepath.Join(app.WD().Path, "nbfm.log"))
if err != nil {
log.Fatal().Err(err).Msg("")
}
app.Logger = logger
wd := app.WD()
db, err := local.ConnectDB(filepath.Join(wd.HomeDir, ".nbfm", "local", "sqlite.db"))
if err != nil {
log.Fatal().Err(err).Msg("")
}
err = note.Prepare(db, wd.Path)
if err != nil {
log.Fatal().Err(err).Msg("")
}
app.db = db
macAddr, err := identifier.MacAddrHex()
if err != nil {
log.Fatal().Err(err).Msg("")
}
hash := sha256.Sum256(append([]byte(macAddr), 0))
app.replicaID = binary.BigEndian.Uint32(hash[:4])
app.Editor = editor.NewEditor(config)
return app
}
func (a *App) Startup(ctx context.Context) {
ctx = context.WithValue(ctx, identifier.DB, a.db)
ctx = context.WithValue(ctx, identifier.ReplicaID, a.replicaID)
a.ctx = ctx
}
func (a *App) Shutdown(ctx context.Context) {
a.Editor.Wait()
a.ctx.Done()
a.ctx.Value(identifier.DB).(*sql.DB).Close()
a.Logger.Close()
}
func (a *App) WD() *nav.WorkingDir {
if a.wd != nil {
return a.wd
}
wd, err := nav.NewWorkingDir()
if err != nil {
log.Fatal().Err(err).Msg("")
}
a.wd = wd
return wd
}
func (a *App) ReadDirFiles(path string, hidden bool) ([]*nav.FileInfo, error) {
return nav.ReadDirFiles(path, hidden)
}
func (a *App) Preview(path string, hidden bool) (*nav.PreviewInfo, error) {
a.Editor.Close()
return nav.Preview(path, hidden)
}
func (a *App) NBError(path string, err string) {
a.Editor.Error(path, err)
}
func (a *App) NBConnected(path string, connected bool) {
a.Editor.Connected(a.ctx, path, connected)
}
func (a *App) NBContribute(path string, ctrbs string) {
a.Editor.Contribute(a.ctx, path, ctrbs)
}
/** @category node manipulation */
func (a *App) DeleteFile(path string) error {
return nav.DeleteFile(path)
}
func (a *App) AddFile(path string) error {
return nav.AddFile(path)
}
func (a *App) MoveFile(src, dst string) error {
return nav.MoveFile(src, dst)
}
func (a *App) CopyFile(src, dst string) error {
return nav.CopyFile(src, dst)
}