forked from dgraph-io/dgraph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
root.go
90 lines (82 loc) · 2.96 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
/*
* Copyright (C) 2017 Dgraph Labs, Inc. and Contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cmd
import (
"fmt"
"os"
"github.com/dgraph-io/dgraph/dgraph/cmd/bulk"
"github.com/dgraph-io/dgraph/dgraph/cmd/live"
"github.com/dgraph-io/dgraph/dgraph/cmd/server"
"github.com/dgraph-io/dgraph/dgraph/cmd/zero"
"github.com/dgraph-io/dgraph/x"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
// RootCmd represents the base command when called without any subcommands
var RootCmd = &cobra.Command{
Use: "dgraph",
Short: "Dgraph: Distributed Graph Database",
Long: `
Dgraph is an open source, horizontally scalable and distributed graph database,
providing ACID transactions, consistent replication and linearizable reads.
It's built from ground up to perform for a rich set of queries. Being a native
graph database, it tightly controls how the data is arranged on disk to optimize
for query performance and throughput, reducing disk seeks and network calls in a
cluster.
` + x.BuildDetails(),
PersistentPreRunE: cobra.NoArgs,
}
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
if err := RootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
var rootConf = viper.New()
func init() {
RootCmd.PersistentFlags().String("profile_mode", "",
"Enable profiling mode, one of [cpu, mem, mutex, block]")
RootCmd.PersistentFlags().Int("block_rate", 0,
"Block profiling rate. Must be used along with block profile_mode")
RootCmd.PersistentFlags().String("config", "",
"Configuration file. Takes precedence over default values, but is "+
"overridden to values set with environment variables and flags.")
rootConf.BindPFlags(RootCmd.PersistentFlags())
var subcommands = []*x.SubCommand{
&bulk.Bulk, &live.Live, &server.Server, &zero.Zero,
}
for _, sc := range subcommands {
RootCmd.AddCommand(sc.Cmd)
sc.Conf = viper.New()
sc.Conf.BindPFlags(sc.Cmd.Flags())
sc.Conf.BindPFlags(RootCmd.PersistentFlags())
sc.Conf.AutomaticEnv()
sc.Conf.SetEnvPrefix(sc.EnvPrefix)
}
cobra.OnInitialize(func() {
cfg := rootConf.GetString("config")
if cfg == "" {
return
}
for _, sc := range subcommands {
sc.Conf.SetConfigFile(cfg)
x.Check(x.Wrapf(sc.Conf.ReadInConfig(), "reading config"))
}
})
}