github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/worker/uniter/runner/jujuc/unit-get.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 "github.com/juju/errors" 11 "github.com/juju/gnuflag" 12 ) 13 14 // UnitGetCommand implements the unit-get command. 15 type UnitGetCommand struct { 16 cmd.CommandBase 17 ctx Context 18 Key string 19 out cmd.Output 20 } 21 22 func NewUnitGetCommand(ctx Context) (cmd.Command, error) { 23 return &UnitGetCommand{ctx: ctx}, nil 24 } 25 26 func (c *UnitGetCommand) Info() *cmd.Info { 27 return &cmd.Info{ 28 Name: "unit-get", 29 Args: "<setting>", 30 Purpose: "print public-address or private-address", 31 } 32 } 33 34 func (c *UnitGetCommand) SetFlags(f *gnuflag.FlagSet) { 35 c.out.AddFlags(f, "smart", cmd.DefaultFormatters) 36 } 37 38 func (c *UnitGetCommand) Init(args []string) error { 39 if args == nil { 40 return errors.New("no setting specified") 41 } 42 if args[0] != "private-address" && args[0] != "public-address" { 43 return fmt.Errorf("unknown setting %q", args[0]) 44 } 45 c.Key = args[0] 46 return cmd.CheckEmpty(args[1:]) 47 } 48 49 func (c *UnitGetCommand) Run(ctx *cmd.Context) error { 50 var value string 51 var err error 52 if c.Key == "private-address" { 53 value, err = c.ctx.PrivateAddress() 54 } else { 55 value, err = c.ctx.PublicAddress() 56 } 57 if err != nil { 58 return errors.Trace(err) 59 } 60 return c.out.Write(ctx, value) 61 }