forked from dnote/dnote
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
95 lines (78 loc) · 1.95 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
package main
import (
"fmt"
"os"
"os/user"
"github.com/dnote-io/cli/cmd/root"
"github.com/dnote-io/cli/core"
"github.com/dnote-io/cli/infra"
"github.com/dnote-io/cli/log"
"github.com/pkg/errors"
// commands
"github.com/dnote-io/cli/cmd/add"
"github.com/dnote-io/cli/cmd/edit"
"github.com/dnote-io/cli/cmd/login"
"github.com/dnote-io/cli/cmd/ls"
"github.com/dnote-io/cli/cmd/remove"
"github.com/dnote-io/cli/cmd/sync"
"github.com/dnote-io/cli/cmd/upgrade"
"github.com/dnote-io/cli/cmd/version"
)
// apiEndpoint is populated during link time
var apiEndpoint string
func main() {
ctx, err := newCtx()
if err != nil {
panic(errors.Wrap(err, "Failed to initialize the dnote context"))
}
err = root.Prepare(ctx)
if err != nil {
panic(errors.Wrap(err, "Failed to prepare dnote run"))
}
root.Register(remove.NewCmd(ctx))
root.Register(edit.NewCmd(ctx))
root.Register(login.NewCmd(ctx))
root.Register(add.NewCmd(ctx))
root.Register(ls.NewCmd(ctx))
root.Register(sync.NewCmd(ctx))
root.Register(version.NewCmd(ctx))
root.Register(upgrade.NewCmd(ctx))
if err := root.Execute(); err != nil {
log.Errorf("%s\n", err.Error())
os.Exit(1)
}
}
func newCtx() (infra.DnoteCtx, error) {
homeDir, err := getHomeDir()
if err != nil {
return infra.DnoteCtx{}, errors.Wrap(err, "Failed to get home dir")
}
dnoteDir := getDnoteDir(homeDir)
ret := infra.DnoteCtx{
HomeDir: homeDir,
DnoteDir: dnoteDir,
APIEndpoint: apiEndpoint,
}
return ret, nil
}
func getDnoteDir(homeDir string) string {
var ret string
dnoteDirEnv := os.Getenv("DNOTE_DIR")
if dnoteDirEnv == "" {
ret = fmt.Sprintf("%s/%s", homeDir, core.DnoteDirName)
} else {
ret = dnoteDirEnv
}
return ret
}
func getHomeDir() (string, error) {
homeDirEnv := os.Getenv("DNOTE_HOME_DIR")
if homeDirEnv != "" {
return homeDirEnv, nil
}
usr, err := user.Current()
if err != nil {
return "", errors.Wrap(err, "Failed to get current user")
}
return usr.HomeDir, nil
}