github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/cmd/juju/action/defined.go (about)

     1  // Copyright 2014, 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package action
     5  
     6  import (
     7  	"github.com/juju/cmd"
     8  	errors "github.com/juju/errors"
     9  	"github.com/juju/names"
    10  	"launchpad.net/gnuflag"
    11  
    12  	"github.com/juju/juju/apiserver/params"
    13  )
    14  
    15  // DefinedCommand lists actions defined by the charm of a given service.
    16  type DefinedCommand struct {
    17  	ActionCommandBase
    18  	serviceTag names.ServiceTag
    19  	fullSchema bool
    20  	out        cmd.Output
    21  }
    22  
    23  const definedDoc = `
    24  Show the actions available to run on the target service, with a short
    25  description.  To show the full schema for the actions, use --schema.
    26  
    27  For more information, see also the 'do' subcommand, which executes actions.
    28  `
    29  
    30  // Set up the output.
    31  func (c *DefinedCommand) SetFlags(f *gnuflag.FlagSet) {
    32  	c.out.AddFlags(f, "smart", cmd.DefaultFormatters)
    33  	f.BoolVar(&c.fullSchema, "schema", false, "display the full action schema")
    34  }
    35  
    36  func (c *DefinedCommand) Info() *cmd.Info {
    37  	return &cmd.Info{
    38  		Name:    "defined",
    39  		Args:    "<service name>",
    40  		Purpose: "show actions defined for a service",
    41  		Doc:     definedDoc,
    42  	}
    43  }
    44  
    45  // Init validates the service name and any other options.
    46  func (c *DefinedCommand) Init(args []string) error {
    47  	switch len(args) {
    48  	case 0:
    49  		return errors.New("no service name specified")
    50  	case 1:
    51  		svcName := args[0]
    52  		if !names.IsValidService(svcName) {
    53  			return errors.Errorf("invalid service name %q", svcName)
    54  		}
    55  		c.serviceTag = names.NewServiceTag(svcName)
    56  		return nil
    57  	default:
    58  		return cmd.CheckEmpty(args[1:])
    59  	}
    60  }
    61  
    62  // Run grabs the Actions spec from the api.  It then sets up a sensible
    63  // output format for the map.
    64  func (c *DefinedCommand) Run(ctx *cmd.Context) error {
    65  	api, err := c.NewActionAPIClient()
    66  	if err != nil {
    67  		return err
    68  	}
    69  	defer api.Close()
    70  
    71  	actions, err := api.ServiceCharmActions(params.Entity{c.serviceTag.String()})
    72  	if err != nil {
    73  		return err
    74  	}
    75  
    76  	output := actions.ActionSpecs
    77  	if len(output) == 0 {
    78  		return c.out.Write(ctx, "No actions defined for "+c.serviceTag.Id())
    79  	}
    80  
    81  	if c.fullSchema {
    82  		verboseSpecs := make(map[string]interface{})
    83  		for k, v := range output {
    84  			verboseSpecs[k] = v.Params
    85  		}
    86  
    87  		return c.out.Write(ctx, verboseSpecs)
    88  	}
    89  
    90  	shortOutput := make(map[string]string)
    91  	for name, action := range actions.ActionSpecs {
    92  		shortOutput[name] = action.Description
    93  		if shortOutput[name] == "" {
    94  			shortOutput[name] = "No description"
    95  		}
    96  	}
    97  	return c.out.Write(ctx, shortOutput)
    98  }