This repository has been archived by the owner on Dec 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathnode.go
90 lines (75 loc) · 2.26 KB
/
node.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
package cmd
import (
"context"
"encoding/json"
"fmt"
"os"
homedir "github.com/mitchellh/go-homedir"
"github.com/mosuka/blast/client"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var (
nodeCmd = &cobra.Command{
Use: "node",
Short: "Get the node info",
Long: "Get the node info",
RunE: func(cmd *cobra.Command, args []string) error {
grpcAddress = viper.GetString("grpc_address")
certificateFile = viper.GetString("certificate_file")
commonName = viper.GetString("common_name")
c, err := client.NewGRPCClientWithContextTLS(grpcAddress, context.Background(), certificateFile, commonName)
if err != nil {
return err
}
defer func() {
_ = c.Close()
}()
resp, err := c.Node()
if err != nil {
return err
}
respBytes, err := json.Marshal(resp)
if err != nil {
return err
}
fmt.Println(string(respBytes))
return nil
},
}
)
func init() {
rootCmd.AddCommand(nodeCmd)
cobra.OnInitialize(func() {
if configFile != "" {
viper.SetConfigFile(configFile)
} else {
home, err := homedir.Dir()
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
viper.AddConfigPath("/etc")
viper.AddConfigPath(home)
viper.SetConfigName("blast")
}
viper.SetEnvPrefix("BLAST")
viper.AutomaticEnv()
if err := viper.ReadInConfig(); err != nil {
switch err.(type) {
case viper.ConfigFileNotFoundError:
// config file does not found in search path
default:
_, _ = fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
})
nodeCmd.PersistentFlags().StringVar(&configFile, "config-file", "", "config file. if omitted, blast.yaml in /etc and home directory will be searched")
nodeCmd.PersistentFlags().StringVar(&grpcAddress, "grpc-address", ":9000", "gRPC server listen address")
nodeCmd.PersistentFlags().StringVar(&certificateFile, "certificate-file", "", "path to the client server TLS certificate file")
nodeCmd.PersistentFlags().StringVar(&commonName, "common-name", "", "certificate common name")
_ = viper.BindPFlag("grpc_address", nodeCmd.PersistentFlags().Lookup("grpc-address"))
_ = viper.BindPFlag("certificate_file", nodeCmd.PersistentFlags().Lookup("certificate-file"))
_ = viper.BindPFlag("common_name", nodeCmd.PersistentFlags().Lookup("common-name"))
}