-
Notifications
You must be signed in to change notification settings - Fork 224
/
resolve.go
62 lines (49 loc) · 1.13 KB
/
resolve.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
package agent
import (
"context"
"fmt"
"github.com/spf13/cobra"
"github.com/superfly/flyctl/pkg/agent"
"github.com/superfly/flyctl/pkg/iostreams"
"github.com/superfly/flyctl/internal/command"
"github.com/superfly/flyctl/internal/config"
"github.com/superfly/flyctl/internal/flag"
"github.com/superfly/flyctl/internal/render"
)
func newResolve() (cmd *cobra.Command) {
const (
short = "Resolve the IP of a host[:port]"
long = short + "\n"
usage = "resolve <slug> <host[:port]>"
)
cmd = command.New(usage, short, long, runResolve,
command.RequireSession,
)
cmd.Args = cobra.ExactArgs(2)
return
}
func runResolve(ctx context.Context) (err error) {
var client *agent.Client
if client, err = establish(ctx); err != nil {
return
}
var (
args = flag.Args(ctx)
slug = args[0]
hostport = args[1]
)
addr, err := client.Resolve(ctx, slug, hostport)
if err != nil {
return
}
if out := iostreams.FromContext(ctx).Out; config.FromContext(ctx).JSONOutput {
err = render.JSON(out, struct {
Addr string `json:"addr"`
}{
Addr: addr,
})
} else {
_, err = fmt.Fprintln(out, addr)
}
return
}