-
Notifications
You must be signed in to change notification settings - Fork 17
/
connect.go
49 lines (42 loc) · 1.15 KB
/
connect.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
package main
import (
"fmt"
"net/http"
adminserver "github.com/ipni/index-provider/server/admin/http"
"github.com/urfave/cli/v2"
)
var ConnectCmd = &cli.Command{
Name: "connect",
Usage: "Connects to an indexer through its multiaddr",
Flags: connectFlags,
Action: connectCommand,
}
var connectFlags = []cli.Flag{
&cli.StringFlag{
Name: "indexermaddr",
Usage: "Indexer multiaddr to connect",
Aliases: []string{"imaddr"},
Required: true,
},
adminAPIFlag,
}
func connectCommand(cctx *cli.Context) error {
iaddr := cctx.String("indexermaddr")
req := &adminserver.ConnectReq{Maddr: iaddr}
resp, err := doHttpPostReq(cctx.Context, adminAPIFlagValue+"/admin/connect", req)
if err != nil {
return err
}
defer resp.Body.Close()
// Handle failed requests
if resp.StatusCode != http.StatusOK {
return errFromHttpResp(resp)
}
log.Infof("connected to peer successfully")
var res adminserver.ConnectRes
if _, err := res.ReadFrom(resp.Body); err != nil {
return fmt.Errorf("received OK response from server but cannot decode response body: %w", err)
}
_, err = cctx.App.Writer.Write([]byte("Connected to peer successfully"))
return err
}