-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
85 lines (74 loc) · 2.09 KB
/
client.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
package command
import (
"net/http"
"strings"
"connectrpc.com/connect"
"github.com/spf13/cobra"
"github.com/spf13/viper"
v1 "go.octolab.org/template/service/api/rpc/v1"
"go.octolab.org/template/service/api/rpc/v1/v1connect"
"go.octolab.org/template/service/internal/config"
)
// NewClient returns the new client command.
func NewClient(cnf *config.Service) *cobra.Command {
var path string
v := viper.New()
v.AddConfigPath(".")
command := cobra.Command{
Use: "client",
Short: "client to the service",
Long: "Run service commands remotely.",
Args: cobra.NoArgs,
SilenceErrors: false,
SilenceUsage: true,
PersistentPreRunE: func(*cobra.Command, []string) error {
v.SetConfigFile(path)
if err := v.ReadInConfig(); err != nil {
return err
}
return v.Unmarshal(cnf)
},
}
flags := command.PersistentFlags()
flags.StringVarP(&path, "config", "c", "config.toml", "path to config file")
flags.StringVar(&cnf.Server.Connect.Address, "host", "", "remote rpc host")
command.AddCommand(
Hello(&cnf.Server.Connect),
Sign(&cnf.Server.Connect),
)
return &command
}
func Hello(cnf *config.Protocol) *cobra.Command {
return &cobra.Command{
Use: "hello",
Short: "send a message to the service",
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
client := v1connect.NewGreeterServiceClient(http.DefaultClient, cnf.BaseURL())
resp, err := client.Hello(cmd.Context(), connect.NewRequest(&v1.HelloRequest{
Subject: strings.Join(args, " "),
}))
if err != nil {
return err
}
cmd.Println(resp.Msg.GetText())
return nil
},
}
}
func Sign(cnf *config.Protocol) *cobra.Command {
return &cobra.Command{
Use: "sign",
Short: "send a license request to the service",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
client := v1connect.NewGreeterServiceClient(http.DefaultClient, cnf.BaseURL())
resp, err := client.Sign(cmd.Context(), connect.NewRequest(new(v1.SignRequest)))
if err != nil {
return err
}
cmd.Println(string(resp.Msg.GetLicense()))
return nil
},
}
}