-
Notifications
You must be signed in to change notification settings - Fork 111
/
start.go
238 lines (210 loc) · 6.97 KB
/
start.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package start
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/joho/godotenv"
"github.com/rilldata/rill/cli/pkg/cmdutil"
"github.com/rilldata/rill/cli/pkg/gitutil"
"github.com/rilldata/rill/cli/pkg/local"
"github.com/rilldata/rill/runtime/compilers/rillv1beta"
"github.com/spf13/cobra"
)
// maxProjectFiles is the maximum number of files that can be in a project directory.
// It corresponds to the file watcher limit in runtime/drivers/file/repo.go.
const maxProjectFiles = 1000
// StartCmd represents the start command
func StartCmd(ch *cmdutil.Helper) *cobra.Command {
var olapDriver string
var olapDSN string
var httpPort int
var grpcPort int
var verbose bool
var debug bool
var readonly bool
var reset bool
var noUI bool
var noOpen bool
var logFormat string
var env []string
var vars []string
var tlsCertPath string
var tlsKeyPath string
startCmd := &cobra.Command{
Use: "start [<path>]",
Short: "Build project and start web app",
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
var projectPath string
if len(args) > 0 {
projectPath = args[0]
if strings.HasSuffix(projectPath, ".git") {
repoName, err := gitutil.CloneRepo(projectPath)
if err != nil {
return fmt.Errorf("clone repo error: %w", err)
}
projectPath = repoName
}
} else if !rillv1beta.HasRillProject("") {
if !ch.Interactive {
return fmt.Errorf("required arg <path> missing")
}
currentDir, err := filepath.Abs("")
if err != nil {
return err
}
projectPath = currentDir
homeDir, err := os.UserHomeDir()
if err != nil {
return err
}
displayPath := currentDir
defval := true
if strings.HasPrefix(currentDir, homeDir) {
displayPath = strings.Replace(currentDir, homeDir, "~", 1)
if currentDir == homeDir {
defval = false
displayPath = "~/"
}
}
msg := fmt.Sprintf("Rill will create project files in %q. Do you want to continue?", displayPath)
confirm, err := cmdutil.ConfirmPrompt(msg, "", defval)
if err != nil {
return err
}
if !confirm {
ch.PrintfWarn("Aborted\n")
return nil
}
}
// Check that projectPath doesn't have an excessive number of files
n, err := countFilesInDirectory(projectPath)
if err != nil {
return err
}
if n > maxProjectFiles {
ch.PrintfError("The project directory exceeds the limit of %d files (found %d files). Please open Rill against a directory with fewer files.\n", maxProjectFiles, n)
return nil
}
parsedLogFormat, ok := local.ParseLogFormat(logFormat)
if !ok {
return fmt.Errorf("invalid log format %q", logFormat)
}
// Backwards compatibility for --env (see comment on the flag definition for details)
environment := "dev"
for _, v := range env {
if strings.Contains(v, "=") {
vars = append(vars, v)
} else {
environment = v
}
}
// Parser variables from "a=b" format to map
varsMap, err := parseVariables(vars)
if err != nil {
return err
}
// If keypath or certpath provided, but not the other, display error
// If keypath and certpath provided, check if the file exists
if (tlsCertPath != "" && tlsKeyPath == "") || (tlsCertPath == "" && tlsKeyPath != "") {
return fmt.Errorf("both --tls-cert and --tls-key must be provided")
} else if tlsCertPath != "" && tlsKeyPath != "" {
// Check to ensure the paths are valid
if _, err := os.Stat(tlsCertPath); os.IsNotExist(err) {
return fmt.Errorf("certificate not found: %s", tlsCertPath)
}
if _, err := os.Stat(tlsKeyPath); os.IsNotExist(err) {
return fmt.Errorf("key not found: %s", tlsKeyPath)
}
}
app, err := local.NewApp(cmd.Context(), &local.AppOptions{
Version: ch.Version,
Verbose: verbose,
Debug: debug,
Reset: reset,
Environment: environment,
OlapDriver: olapDriver,
OlapDSN: olapDSN,
ProjectPath: projectPath,
LogFormat: parsedLogFormat,
Variables: varsMap,
Activity: ch.Telemetry(cmd.Context()),
AdminURL: ch.AdminURL,
AdminToken: ch.AdminToken(),
CMDHelper: ch,
})
if err != nil {
return err
}
defer app.Close()
userID, _ := ch.CurrentUserID(cmd.Context())
err = app.Serve(httpPort, grpcPort, !noUI, !noOpen, readonly, userID, tlsCertPath, tlsKeyPath)
if err != nil {
return fmt.Errorf("serve: %w", err)
}
return nil
},
}
startCmd.Flags().SortFlags = false
startCmd.Flags().BoolVar(&noOpen, "no-open", false, "Do not open browser")
startCmd.Flags().IntVar(&httpPort, "port", 9009, "Port for HTTP")
startCmd.Flags().IntVar(&grpcPort, "port-grpc", 49009, "Port for gRPC (internal)")
startCmd.Flags().BoolVar(&readonly, "readonly", false, "Show only dashboards in UI")
startCmd.Flags().BoolVar(&noUI, "no-ui", false, "Serve only the backend")
startCmd.Flags().BoolVar(&verbose, "verbose", false, "Sets the log level to debug")
startCmd.Flags().BoolVar(&debug, "debug", false, "Collect additional debug info")
startCmd.Flags().BoolVar(&reset, "reset", false, "Clear and re-ingest source data")
startCmd.Flags().StringVar(&logFormat, "log-format", "console", "Log format (options: \"console\", \"json\")")
startCmd.Flags().StringVar(&tlsCertPath, "tls-cert", "", "Path to TLS certificate")
startCmd.Flags().StringVar(&tlsKeyPath, "tls-key", "", "Path to TLS key file")
// --env was previously used for variables, but is now used to set the environment name. We maintain backwards compatibility by keeping --env as a slice var, and setting any value containing an equals sign as a variable.
startCmd.Flags().StringSliceVarP(&env, "env", "e", []string{}, `Environment name (default "dev")`)
startCmd.Flags().StringSliceVarP(&vars, "var", "v", []string{}, "Set project variables")
// We have deprecated the ability configure the OLAP database via the CLI. This should now be done via rill.yaml.
// Keeping these for backwards compatibility for a while.
startCmd.Flags().StringVar(&olapDSN, "db", local.DefaultOLAPDSN, "Database DSN")
startCmd.Flags().StringVar(&olapDriver, "db-driver", local.DefaultOLAPDriver, "Database driver")
if err := startCmd.Flags().MarkHidden("db"); err != nil {
panic(err)
}
if err := startCmd.Flags().MarkHidden("db-driver"); err != nil {
panic(err)
}
return startCmd
}
func countFilesInDirectory(path string) (int, error) {
var fileCount int
if path == "" {
path = "."
}
err := filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
fileCount++
}
return nil
})
if err != nil {
if os.IsNotExist(err) {
return 0, nil
}
return 0, err
}
return fileCount, nil
}
func parseVariables(vals []string) (map[string]string, error) {
res := make(map[string]string)
for _, v := range vals {
v, err := godotenv.Unmarshal(v)
if err != nil {
return nil, fmt.Errorf("failed to parse variable %q: %w", v, err)
}
for k, v := range v {
res[k] = v
}
}
return res, nil
}