-
Notifications
You must be signed in to change notification settings - Fork 1
/
start.go
67 lines (61 loc) · 2.11 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
// Copyright 2019 The go-ultiledger Authors
//
// 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 app
import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/ultiledger/go-ultiledger/log"
"github.com/ultiledger/go-ultiledger/node"
)
var startCmd = &cobra.Command{
Use: "start",
Short: "Start the ULT node with config",
Long: `Start a Ultiledger node with specified configuration, the program will
try to recover from latest checkpoint if the --newnode command argument
is not specified or it will bootstrap a completely new node.`,
Run: func(cmd *cobra.Command, args []string) {
// Read in configuration.
if cfgFile == "" {
log.Fatal("config file not provided")
}
v := viper.New()
v.SetConfigFile(cfgFile)
if err := v.ReadInConfig(); err != nil {
log.Fatalf("read in configuration failed: %v", err)
}
// Initialize node configuration.
config, err := node.NewConfig(v)
if err != nil {
log.Fatal(err)
}
log.Initialize(config.LogFile)
// Whether switch on the debug mode of the logger.
if viper.GetBool("debug") {
log.OpenDebug()
}
// Start the ULT node.
n := node.NewNode(config)
n.Start(viper.GetBool("newnode"))
},
}
var cfgFile string
func init() {
startCmd.Flags().StringVarP(&cfgFile, "config", "c", "", "configuration for node")
startCmd.MarkFlagRequired("config")
startCmd.Flags().BoolP("newnode", "n", false, "bootstrap a new node")
startCmd.Flags().BoolP("debug", "d", false, "toggle the debug mode")
viper.BindPFlag("newnode", startCmd.Flags().Lookup("newnode"))
viper.BindPFlag("debug", startCmd.Flags().Lookup("debug"))
rootCmd.AddCommand(startCmd)
}