/
root.go
107 lines (85 loc) · 2.91 KB
/
root.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
/*
Copyright 2022 Codenotary Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package immudb
import (
c "github.com/codenotary/immudb/cmd/helper"
"github.com/codenotary/immudb/pkg/logger"
"github.com/codenotary/immudb/pkg/server"
"github.com/spf13/cobra"
"github.com/spf13/viper"
daem "github.com/takama/daemon"
)
func (cl *Commandline) NewRootCmd(immudbServer server.ImmuServerIf) (*cobra.Command, error) {
cmd := &cobra.Command{
Use: "immudb",
Short: "immudb - the lightweight, high-speed immutable database for systems and applications",
Long: `immudb - the lightweight, high-speed immutable database for systems and applications.
immudb documentation: https://docs.immudb.io/
Setting the logging level and other options through environment variables:
- Logging level: LOG_LEVEL={debug|info|warning|error}
- The environment variable names for other settings are derived by prefixing flag names with "IMMUDB_"
e.g IMMUDB_PORT=3323 ./immudb.
Note: flags take precedence over environment variables.
`,
DisableAutoGenTag: true,
RunE: cl.Immudb(immudbServer),
PersistentPreRunE: cl.ConfigChain(nil),
}
cl.setupFlags(cmd, server.DefaultOptions())
if err := viper.BindPFlags(cmd.Flags()); err != nil {
return nil, err
}
setupDefaults(server.DefaultOptions())
return cmd, nil
}
// Immudb ...
func (cl *Commandline) Immudb(immudbServer server.ImmuServerIf) func(*cobra.Command, []string) error {
return func(cmd *cobra.Command, args []string) (err error) {
var options *server.Options
if options, err = parseOptions(); err != nil {
return err
}
immudbServer := immudbServer.WithOptions(options)
// initialize logger for immudb
ilogger, err := logger.NewLogger(&logger.Options{
Name: "immudb",
LogFormat: options.LogFormat,
LogFile: options.Logfile,
Level: logger.LogLevelFromEnvironment(),
})
if err != nil {
c.QuitToStdErr(err)
}
defer ilogger.Close()
immudbServer.WithLogger(ilogger)
// check if immudb needs to be run in detached mode
if options.Detached {
if err := cl.P.Detached(); err == nil {
return nil
}
}
// check if immudb needs to run in daemon mode
var d daem.Daemon
if d, err = daem.New("immudb", "immudb", "immudb"); err != nil {
c.QuitToStdErr(err)
}
if err = immudbServer.Initialize(); err != nil {
return err
}
service := server.Service{
ImmuServerIf: immudbServer,
}
d.Run(service)
return nil
}
}