forked from zenon-network/go-zenon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.go
111 lines (92 loc) · 2.24 KB
/
cli.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
package app
import (
"fmt"
"net/http"
_ "net/http/pprof"
"os"
"path/filepath"
"runtime"
"sort"
"time"
"gopkg.in/urfave/cli.v1"
"github.com/zenon-network/go-zenon/common"
"github.com/zenon-network/go-zenon/metadata"
)
var (
log = common.ZenonLogger.New()
app = cli.NewApp()
nodeManager *Manager
)
func Run() {
err := app.Run(os.Args)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func Stop() {
err := nodeManager.Stop()
common.DealWithErr(err)
fmt.Println("znnd successfully stopped")
}
func init() {
app.Name = filepath.Base(os.Args[0])
app.HideVersion = false
app.Version = metadata.Version
app.Compiled = time.Now()
app.Authors = []cli.Author{
{
Name: "The Zenon Developers",
Email: "portal@zenon.network",
},
}
app.Copyright = "Copyright 2021, Zenon"
app.Usage = "znnd Node"
//Import: Please add the New command here
app.Commands = []cli.Command{
versionCommand,
licenseCommand,
}
sort.Sort(cli.CommandsByName(app.Commands))
app.Flags = AllFlags
app.Before = beforeAction
app.Action = action
app.After = afterAction
}
func beforeAction(ctx *cli.Context) error {
max := runtime.NumCPU()
fmt.Printf("Starting znnd.\n")
fmt.Printf("current time is %v\n", time.Now().Format("2006-01-02 15:04:05"))
fmt.Printf("version: %v\n", metadata.Version)
fmt.Printf("git-commit-hash: %v\n", metadata.GitCommit)
fmt.Printf("znnd will use at most %v cpu-cores\n", max)
runtime.GOMAXPROCS(max)
// pprof server
if ctx.GlobalIsSet(PprofFlag.Name) {
listenHost := ctx.String(PprofAddrFlag.Name)
port := ctx.Int(PprofPortFlag.Name)
address := fmt.Sprintf("%s:%d", listenHost, port)
log.Info("Starting pprof server", "addr", fmt.Sprintf("http://%s/debug/pprof", address))
go func() {
if err := http.ListenAndServe(address, nil); err != nil {
log.Error("Failure in running pprof server", "err", err)
}
}()
}
return nil
}
func action(ctx *cli.Context) error {
//Make sure No subCommands were entered,Only the flags
if args := ctx.Args(); len(args) > 0 {
return fmt.Errorf("invalid command: %q", args[0])
}
var err error
nodeManager, err = NewNodeManager(ctx)
if err != nil {
return err
}
return nodeManager.Start()
}
func afterAction(*cli.Context) error {
return nil
}