/
list.go
94 lines (78 loc) · 2.02 KB
/
list.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
//
// client/command/remote/list.go
//
// Copyright (c) 2017 Junpei Kawamoto
//
// This file is part of cgss.
//
// cgss is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// cgss is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with cgss. If not, see <http://www.gnu.org/licenses/>.
//
package remote
import (
"context"
"fmt"
"io"
"math/rand"
"google.golang.org/grpc"
"github.com/itslab-kyushu/cgss/cfg"
"github.com/itslab-kyushu/cgss/kvs"
"github.com/urfave/cli"
)
// CmdList prepares list command and run cmdList.
func CmdList(c *cli.Context) (err error) {
if c.NArg() != 0 {
return cli.ShowSubcommandHelp(c)
}
conf, err := cfg.ReadConfig(c.String("config"))
if err != nil {
return
}
return cmdList(conf)
}
func cmdList(conf *cfg.Config) (err error) {
if conf.NGroups() == 0 {
return fmt.Errorf("No groups given: %v", conf)
}
group := conf.Groups[rand.Intn(len(conf.Groups))]
if len(group.Servers) == 0 {
return fmt.Errorf("Group %v doesn't have servers", group.Name)
}
server := group.Servers[rand.Intn(len(group.Servers))]
conn, err := grpc.Dial(
fmt.Sprintf("%s:%d", server.Address, server.Port),
grpc.WithInsecure(),
grpc.WithCompressor(grpc.NewGZIPCompressor()),
grpc.WithDecompressor(grpc.NewGZIPDecompressor()),
)
if err != nil {
return
}
defer conn.Close()
ctx := context.Background()
client := kvs.NewKvsClient(conn)
stream, err := client.List(ctx, &kvs.ListRequest{})
if err != nil {
return
}
for {
item, err := stream.Recv()
if err == io.EOF {
break
} else if err != nil {
return err
}
fmt.Println(item.Name)
}
return
}