-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
143 lines (125 loc) · 3.17 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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package main
import (
"embed"
"fmt"
"os"
"path/filepath"
"github.com/labstack/echo/v4"
"github.com/urfave/cli/v2"
"go.uber.org/zap"
"nutsh/app/action"
"nutsh/app/buildtime"
)
//go:embed app/frontend/build/*
var frontend embed.FS
//go:embed app/yjs-server/bin/yjs-server
var yjsServer []byte
//go:embed docs/build/*
var docs embed.FS
func main() {
mustSetupLogger()
homeDir, err := os.UserHomeDir()
mustOk(err)
defaultWorkspace := filepath.Join(homeDir, ".nutsh", "workspace")
workspaceFlag := &cli.StringFlag{
Name: "workspace",
Usage: "workspace folder of the application",
Value: defaultWorkspace,
EnvVars: []string{"NUTSH_WORKSPACE"},
Destination: &action.StorageOption.Workspace,
}
app := &cli.App{
Name: "nutsh",
Usage: "Start the nutsh application",
Action: runStart,
Flags: []cli.Flag{
workspaceFlag,
&cli.IntFlag{
Name: "port",
Aliases: []string{"p"},
Usage: "port to listen",
Value: 12346,
EnvVars: []string{"NUTSH_PORT"},
Destination: &action.StartOption.Port,
},
&cli.BoolFlag{
Name: "readonly",
Usage: "toggle readonly mode",
Value: false,
EnvVars: []string{"NUTSH_READONLY"},
Destination: &action.StartOption.Readonly,
},
&cli.StringFlag{
Name: "online-segmentation",
Usage: "address to a online segmenation server",
EnvVars: []string{"NUTSH_ONLINE_SEGMENTATION"},
Destination: &action.StartOption.OnlineSegmentationAddr,
},
&cli.StringFlag{
Name: "track",
Usage: "address to a track server",
EnvVars: []string{"NUTSH_TRACK"},
Destination: &action.StartOption.TrackAddr,
},
&cli.StringFlag{
Name: "data-dir",
Usage: "data directory to serve local files",
EnvVars: []string{"NUTSH_DATA_DIR"},
Destination: &action.StartOption.DataDir,
},
},
Commands: []*cli.Command{
{
Name: "version",
Usage: "Show version",
Action: runShowVersion,
},
{
Name: "import",
Usage: "Import project",
Action: runImport,
Flags: []cli.Flag{
workspaceFlag,
&cli.StringFlag{
Name: "data",
Aliases: []string{"d"},
Usage: "path to the data file",
Required: true,
Destination: &action.ImportOption.DataPath,
},
},
},
},
}
mustOk(app.Run(os.Args))
}
func runStart(ctx *cli.Context) error {
action.StartOption.Frontend = echo.MustSubFS(frontend, "app/frontend/build")
action.StartOption.Doc = echo.MustSubFS(docs, "docs/build")
action.StartOption.YJSServer = yjsServer
return action.Start(ctx.Context)
}
func runShowVersion(ctx *cli.Context) error {
fmt.Printf("%s %s %s\n", buildtime.Version, buildtime.CommitIdentifier, buildtime.CommitTime)
return nil
}
func runImport(ctx *cli.Context) error {
return action.Import(ctx.Context)
}
func mustSetupLogger() *zap.Logger {
logger, err := zap.NewProduction()
mustOk(err)
zap.ReplaceGlobals(logger)
return logger
}
func mustOk(err error) {
if err == nil {
return
}
if os.Getenv("DEBUG") != "" {
fmt.Printf("%+v\n", err)
} else {
fmt.Printf("%v\n", err)
}
os.Exit(1)
}