github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/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  
    11  // OpenedPortsCommand implements the opened-ports command.
    12  type OpenedPortsCommand struct {
    13  	cmd.CommandBase
    14  	ctx Context
    15  	out cmd.Output
    16  }
    17  
    18  func NewOpenedPortsCommand(ctx Context) (cmd.Command, error) {
    19  	return &OpenedPortsCommand{ctx: ctx}, nil
    20  }
    21  
    22  func (c *OpenedPortsCommand) Info() *cmd.Info {
    23  	doc := `Each list entry has format <port>/<protocol> (e.g. "80/tcp") or
    24  <from>-<to>/<protocol> (e.g. "8080-8088/udp").`
    25  	return &cmd.Info{
    26  		Name:    "opened-ports",
    27  		Purpose: "lists all ports or ranges opened by the unit",
    28  		Doc:     doc,
    29  	}
    30  }
    31  
    32  func (c *OpenedPortsCommand) SetFlags(f *gnuflag.FlagSet) {
    33  	c.out.AddFlags(f, "smart", cmd.DefaultFormatters)
    34  }
    35  
    36  func (c *OpenedPortsCommand) Init(args []string) error {
    37  	return cmd.CheckEmpty(args)
    38  }
    39  
    40  func (c *OpenedPortsCommand) Run(ctx *cmd.Context) error {
    41  	unitPorts := c.ctx.OpenedPorts()
    42  	results := make([]string, len(unitPorts))
    43  	for i, portRange := range unitPorts {
    44  		results[i] = portRange.String()
    45  	}
    46  	return c.out.Write(ctx, results)
    47  }