forked from juju/juju
-
Notifications
You must be signed in to change notification settings - Fork 0
/
opened-ports.go
49 lines (40 loc) · 1.2 KB
/
opened-ports.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
// Copyright 2014 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package jujuc
import (
"github.com/juju/cmd"
"github.com/juju/gnuflag"
jujucmd "github.com/juju/juju/cmd"
)
// OpenedPortsCommand implements the opened-ports command.
type OpenedPortsCommand struct {
cmd.CommandBase
ctx Context
out cmd.Output
}
func NewOpenedPortsCommand(ctx Context) (cmd.Command, error) {
return &OpenedPortsCommand{ctx: ctx}, nil
}
func (c *OpenedPortsCommand) Info() *cmd.Info {
doc := `Each list entry has format <port>/<protocol> (e.g. "80/tcp") or
<from>-<to>/<protocol> (e.g. "8080-8088/udp").`
return jujucmd.Info(&cmd.Info{
Name: "opened-ports",
Purpose: "lists all ports or ranges opened by the unit",
Doc: doc,
})
}
func (c *OpenedPortsCommand) SetFlags(f *gnuflag.FlagSet) {
c.out.AddFlags(f, "smart", cmd.DefaultFormatters.Formatters())
}
func (c *OpenedPortsCommand) Init(args []string) error {
return cmd.CheckEmpty(args)
}
func (c *OpenedPortsCommand) Run(ctx *cmd.Context) error {
unitPorts := c.ctx.OpenedPorts()
results := make([]string, len(unitPorts))
for i, portRange := range unitPorts {
results[i] = portRange.String()
}
return c.out.Write(ctx, results)
}