github.com/rogpeppe/juju@v0.0.0-20140613142852-6337964b789e/cmd/juju/get.go (about)

     1  // Copyright 2012, 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package main
     5  
     6  import (
     7  	"errors"
     8  
     9  	"github.com/juju/cmd"
    10  	"launchpad.net/gnuflag"
    11  
    12  	"github.com/juju/juju/cmd/envcmd"
    13  	"github.com/juju/juju/juju"
    14  )
    15  
    16  // GetCommand retrieves the configuration of a service.
    17  type GetCommand struct {
    18  	envcmd.EnvCommandBase
    19  	ServiceName string
    20  	out         cmd.Output
    21  }
    22  
    23  func (c *GetCommand) Info() *cmd.Info {
    24  	return &cmd.Info{
    25  		Name:    "get",
    26  		Args:    "<service>",
    27  		Purpose: "get service configuration options",
    28  	}
    29  }
    30  
    31  func (c *GetCommand) SetFlags(f *gnuflag.FlagSet) {
    32  	// TODO(dfc) add json formatting ?
    33  	c.out.AddFlags(f, "yaml", map[string]cmd.Formatter{
    34  		"yaml": cmd.FormatYaml,
    35  	})
    36  }
    37  
    38  func (c *GetCommand) Init(args []string) error {
    39  	// TODO(dfc) add --schema-only
    40  	if len(args) == 0 {
    41  		return errors.New("no service name specified")
    42  	}
    43  	c.ServiceName = args[0]
    44  	return cmd.CheckEmpty(args[1:])
    45  }
    46  
    47  // Run fetches the configuration of the service and formats
    48  // the result as a YAML string.
    49  func (c *GetCommand) Run(ctx *cmd.Context) error {
    50  	client, err := juju.NewAPIClientFromName(c.EnvName)
    51  	if err != nil {
    52  		return err
    53  	}
    54  	defer client.Close()
    55  
    56  	results, err := client.ServiceGet(c.ServiceName)
    57  	if err != nil {
    58  		return err
    59  	}
    60  
    61  	resultsMap := map[string]interface{}{
    62  		"service":  results.Service,
    63  		"charm":    results.Charm,
    64  		"settings": results.Config,
    65  	}
    66  	return c.out.Write(ctx, resultsMap)
    67  }