-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
82 lines (68 loc) · 1.5 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
package main
import (
"errors"
"fmt"
"os"
tea "github.com/charmbracelet/bubbletea"
gap "github.com/muesli/go-app-paths"
"github.com/nirav24/gituser/user"
)
type configMode string
const (
globalConfig configMode = "Global"
localConfig configMode = "Local"
)
type view int
const (
listView view = iota
addView
)
const appName = "gituser"
func main() {
scope := gap.NewScope(gap.User, appName)
dirs, err := scope.DataDirs()
if err != nil {
fmt.Println("Error creating user store: ", err)
os.Exit(1)
}
dir := dirs[0]
fileName := "/data.json"
if _, err := os.Stat(dir + fileName); errors.Is(err, os.ErrNotExist) {
err := createFile(dir, fileName)
if err != nil {
fmt.Printf("Error creating datafile at %s%s, error %+v\n", dir, fileName, err)
os.Exit(1)
}
}
store, err := user.NewStore(dir + fileName)
if err != nil {
fmt.Println("Error creating user store: ", err)
os.Exit(1)
}
defer func() {
if err := store.Close(); err != nil {
fmt.Println("Error updating users: ", err)
os.Exit(1)
}
}()
m := initialModel(store, localConfig, listView)
program := tea.NewProgram(m)
if _, err := program.Run(); err != nil {
fmt.Println("Error running program:", err)
os.Exit(1)
}
}
func createFile(dir, name string) error {
err := os.MkdirAll(dir, 0700)
if err != nil {
return err
}
file, err := os.OpenFile(dir+name, os.O_CREATE|os.O_RDWR, 0600)
if err != nil {
return err
}
if _, err := file.WriteString("[]"); err != nil {
return err
}
return file.Close()
}