This repository was 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
Expand file tree
/
Copy pathset.go
More file actions
94 lines (79 loc) · 2.42 KB
/
set.go
File metadata and controls
94 lines (79 loc) · 2.42 KB
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
package cmd
import (
"context"
"fmt"
"os"
homedir "github.com/mitchellh/go-homedir"
"github.com/mosuka/blast/client"
"github.com/mosuka/blast/marshaler"
"github.com/mosuka/blast/protobuf"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var (
setCmd = &cobra.Command{
Use: "set ID FIELDS",
Args: cobra.ExactArgs(2),
Short: "Set a document",
Long: "Set a document",
RunE: func(cmd *cobra.Command, args []string) error {
grpcAddress = viper.GetString("grpc_address")
certificateFile = viper.GetString("certificate_file")
commonName = viper.GetString("common_name")
id := args[0]
fields := args[1]
req := &protobuf.SetRequest{}
m := marshaler.BlastMarshaler{}
if err := m.Unmarshal([]byte(fields), req); err != nil {
return err
}
req.Id = id
c, err := client.NewGRPCClientWithContextTLS(grpcAddress, context.Background(), certificateFile, commonName)
if err != nil {
return err
}
defer func() {
_ = c.Close()
}()
if err := c.Set(req); err != nil {
return err
}
return nil
},
}
)
func init() {
rootCmd.AddCommand(setCmd)
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)
}
}
})
setCmd.PersistentFlags().StringVar(&configFile, "config-file", "", "config file. if omitted, blast.yaml in /etc and home directory will be searched")
setCmd.PersistentFlags().StringVar(&grpcAddress, "grpc-address", ":9000", "gRPC server listen address")
setCmd.PersistentFlags().StringVar(&certificateFile, "certificate-file", "", "path to the client server TLS certificate file")
setCmd.PersistentFlags().StringVar(&commonName, "common-name", "", "certificate common name")
_ = viper.BindPFlag("grpc_address", setCmd.PersistentFlags().Lookup("grpc-address"))
_ = viper.BindPFlag("certificate_file", setCmd.PersistentFlags().Lookup("certificate-file"))
_ = viper.BindPFlag("common_name", setCmd.PersistentFlags().Lookup("common-name"))
}