github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/cmd/juju/romulus/setbudget/setbudget.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package setbudget 5 6 import ( 7 "fmt" 8 "strconv" 9 10 "github.com/juju/cmd" 11 "github.com/juju/errors" 12 "github.com/juju/juju/cmd/modelcmd" 13 "gopkg.in/macaroon-bakery.v1/httpbakery" 14 15 api "github.com/juju/romulus/api/budget" 16 ) 17 18 type setBudgetCommand struct { 19 modelcmd.JujuCommandBase 20 Name string 21 Value string 22 } 23 24 // NewSetBudgetCommand returns a new setBudgetCommand. 25 func NewSetBudgetCommand() cmd.Command { 26 return &setBudgetCommand{} 27 } 28 29 const doc = ` 30 Set the monthly budget limit. 31 32 Examples: 33 # Sets the monthly limit for budget named 'personal' to 96. 34 juju set-budget personal 96 35 ` 36 37 // Info implements cmd.Command.Info. 38 func (c *setBudgetCommand) Info() *cmd.Info { 39 return &cmd.Info{ 40 Name: "set-budget", 41 Args: "<budget name> <value>", 42 Purpose: "Set the budget limit.", 43 Doc: doc, 44 } 45 } 46 47 // Init implements cmd.Command.Init. 48 func (c *setBudgetCommand) Init(args []string) error { 49 if len(args) < 2 { 50 return errors.New("name and value required") 51 } 52 c.Name, c.Value = args[0], args[1] 53 if _, err := strconv.ParseInt(c.Value, 10, 32); err != nil { 54 return errors.New("budget value needs to be a whole number") 55 } 56 return cmd.CheckEmpty(args[2:]) 57 } 58 59 // Run implements cmd.Command.Run and contains most of the setbudget logic. 60 func (c *setBudgetCommand) Run(ctx *cmd.Context) error { 61 client, err := c.BakeryClient() 62 if err != nil { 63 return errors.Annotate(err, "failed to create an http client") 64 } 65 api, err := newAPIClient(client) 66 if err != nil { 67 return errors.Annotate(err, "failed to create an api client") 68 } 69 resp, err := api.SetBudget(c.Name, c.Value) 70 if err != nil { 71 return errors.Annotate(err, "failed to set the budget") 72 } 73 fmt.Fprintln(ctx.Stdout, resp) 74 return nil 75 } 76 77 var newAPIClient = newAPIClientImpl 78 79 func newAPIClientImpl(c *httpbakery.Client) (apiClient, error) { 80 client := api.NewClient(c) 81 return client, nil 82 } 83 84 type apiClient interface { 85 SetBudget(string, string) (string, error) 86 }