-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathcli.go
117 lines (112 loc) · 3.19 KB
/
cli.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
package cli
import (
"errors"
"os"
"github.com/enchant97/note-mark/backend/config"
"github.com/urfave/cli/v2"
)
func Entrypoint(appVersion string) error {
// Parse config
var appConfig config.AppConfig
if err := appConfig.ParseConfig(); err != nil {
return err
}
app := &cli.App{
Version: appVersion,
Usage: "Backend API app for Note Mark",
EnableBashCompletion: true,
Commands: []*cli.Command{
{
Name: "serve",
Usage: "run the api server",
Action: func(ctx *cli.Context) error {
return commandServe(appConfig)
},
},
{
Name: "clean",
Usage: "cleans deleted and unused data",
Action: func(ctx *cli.Context) error {
return commandClean(appConfig)
},
},
{
Name: "user",
Usage: "user management",
Subcommands: []*cli.Command{
{
Name: "add",
Usage: "add a new user",
Flags: []cli.Flag{
&cli.StringFlag{Name: "username", Aliases: []string{"u"}, Required: true},
&cli.StringFlag{Name: "password", Aliases: []string{"p"}, Required: true},
},
Action: func(ctx *cli.Context) error {
username := ctx.String("username")
password := ctx.String("password")
return commandUserAdd(appConfig, username, password)
},
},
{
Name: "remove",
Usage: "remove a existing user",
Flags: []cli.Flag{
&cli.StringFlag{Name: "username", Aliases: []string{"u"}, Required: true},
},
Action: func(ctx *cli.Context) error {
username := ctx.String("username")
return commandUserRemove(appConfig, username)
},
},
{
Name: "set-password",
Usage: "set a existing users password",
Flags: []cli.Flag{
&cli.StringFlag{Name: "username", Aliases: []string{"u"}, Required: true},
&cli.StringFlag{Name: "password", Aliases: []string{"p"}, Required: true},
},
Action: func(ctx *cli.Context) error {
username := ctx.String("username")
password := ctx.String("password")
return commandUserSetPassword(appConfig, username, password)
},
},
},
},
{
Name: "migrate",
Usage: "import and export data out of the app",
Subcommands: []*cli.Command{
{
Name: "import",
Aliases: []string{"i"},
Usage: "import data into the app",
Flags: []cli.Flag{
&cli.StringFlag{Name: "import-dir", Aliases: []string{"d"}, Required: true},
},
Action: func(ctx *cli.Context) error {
importDir := ctx.String("import-dir")
if _, err := os.Stat(importDir); errors.Is(err, os.ErrNotExist) {
return cli.Exit("import-dir does not exist, or has no read permissions", 1)
}
return commandMigrateImportData(appConfig, importDir)
},
},
{
Name: "export",
Aliases: []string{"e"},
Usage: "export data from the app",
Flags: []cli.Flag{
&cli.StringFlag{Name: "export-dir", Aliases: []string{"d"}, Required: true},
},
Action: func(ctx *cli.Context) error {
exportDir := ctx.String("export-dir")
return commandMigrateExportData(appConfig, exportDir)
},
},
},
},
},
}
return app.Run(os.Args)
}