github.com/cloudbase/juju-core@v0.0.0-20140504232958-a7271ac7912f/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  	"launchpad.net/gnuflag"
    10  
    11  	"launchpad.net/juju-core/cmd"
    12  	"launchpad.net/juju-core/juju"
    13  )
    14  
    15  // GetCommand retrieves the configuration of a service.
    16  type GetCommand struct {
    17  	cmd.EnvCommandBase
    18  	ServiceName string
    19  	out         cmd.Output
    20  }
    21  
    22  func (c *GetCommand) Info() *cmd.Info {
    23  	return &cmd.Info{
    24  		Name:    "get",
    25  		Args:    "<service>",
    26  		Purpose: "get service configuration options",
    27  	}
    28  }
    29  
    30  func (c *GetCommand) SetFlags(f *gnuflag.FlagSet) {
    31  	c.EnvCommandBase.SetFlags(f)
    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  }