github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/worker/uniter/runner/jujuc/opened-ports.go (about)

     1  // Copyright 2014 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package jujuc
     5  
     6  import (
     7  	"github.com/juju/cmd"
     8  	"github.com/juju/gnuflag"
     9  
    10  	jujucmd "github.com/juju/juju/cmd"
    11  )
    12  
    13  // OpenedPortsCommand implements the opened-ports command.
    14  type OpenedPortsCommand struct {
    15  	cmd.CommandBase
    16  	ctx Context
    17  	out cmd.Output
    18  }
    19  
    20  func NewOpenedPortsCommand(ctx Context) (cmd.Command, error) {
    21  	return &OpenedPortsCommand{ctx: ctx}, nil
    22  }
    23  
    24  func (c *OpenedPortsCommand) Info() *cmd.Info {
    25  	doc := `Each list entry has format <port>/<protocol> (e.g. "80/tcp") or
    26  <from>-<to>/<protocol> (e.g. "8080-8088/udp").`
    27  	return jujucmd.Info(&cmd.Info{
    28  		Name:    "opened-ports",
    29  		Purpose: "lists all ports or ranges opened by the unit",
    30  		Doc:     doc,
    31  	})
    32  }
    33  
    34  func (c *OpenedPortsCommand) SetFlags(f *gnuflag.FlagSet) {
    35  	c.out.AddFlags(f, "smart", cmd.DefaultFormatters)
    36  }
    37  
    38  func (c *OpenedPortsCommand) Init(args []string) error {
    39  	return cmd.CheckEmpty(args)
    40  }
    41  
    42  func (c *OpenedPortsCommand) Run(ctx *cmd.Context) error {
    43  	unitPorts := c.ctx.OpenedPorts()
    44  	results := make([]string, len(unitPorts))
    45  	for i, portRange := range unitPorts {
    46  		results[i] = portRange.String()
    47  	}
    48  	return c.out.Write(ctx, results)
    49  }