github.com/Pankov404/juju@v0.0.0-20150703034450-be266991dceb/worker/uniter/runner/jujuc/relation-list.go (about)

     1  // Copyright 2012, 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package jujuc
     5  
     6  import (
     7  	"fmt"
     8  
     9  	"github.com/juju/cmd"
    10  	"launchpad.net/gnuflag"
    11  )
    12  
    13  // RelationListCommand implements the relation-list command.
    14  type RelationListCommand struct {
    15  	cmd.CommandBase
    16  	ctx        Context
    17  	RelationId int
    18  	out        cmd.Output
    19  }
    20  
    21  func NewRelationListCommand(ctx Context) cmd.Command {
    22  	return &RelationListCommand{ctx: ctx}
    23  }
    24  
    25  func (c *RelationListCommand) Info() *cmd.Info {
    26  	doc := "-r must be specified when not in a relation hook"
    27  	if _, found := c.ctx.HookRelation(); found {
    28  		doc = ""
    29  	}
    30  	return &cmd.Info{
    31  		Name:    "relation-list",
    32  		Purpose: "list relation units",
    33  		Doc:     doc,
    34  	}
    35  }
    36  
    37  func (c *RelationListCommand) SetFlags(f *gnuflag.FlagSet) {
    38  	rV := newRelationIdValue(c.ctx, &c.RelationId)
    39  
    40  	c.out.AddFlags(f, "smart", cmd.DefaultFormatters)
    41  	f.Var(rV, "r", "specify a relation by id")
    42  	f.Var(rV, "relation", "")
    43  }
    44  
    45  func (c *RelationListCommand) Init(args []string) (err error) {
    46  	if c.RelationId == -1 {
    47  		return fmt.Errorf("no relation id specified")
    48  	}
    49  	return cmd.CheckEmpty(args)
    50  }
    51  
    52  func (c *RelationListCommand) Run(ctx *cmd.Context) error {
    53  	r, found := c.ctx.Relation(c.RelationId)
    54  	if !found {
    55  		return fmt.Errorf("unknown relation id")
    56  	}
    57  	unitNames := r.UnitNames()
    58  	if unitNames == nil {
    59  		unitNames = []string{}
    60  	}
    61  	return c.out.Write(ctx, unitNames)
    62  }