github.com/mattyw/juju@v0.0.0-20140610034352-732aecd63861/cmd/juju/unset.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/juju/cmd"
    10  	"github.com/juju/juju/cmd/envcmd"
    11  	"github.com/juju/juju/juju"
    12  )
    13  
    14  // UnsetCommand sets configuration values of a service back
    15  // to their default.
    16  type UnsetCommand struct {
    17  	envcmd.EnvCommandBase
    18  	ServiceName string
    19  	Options     []string
    20  }
    21  
    22  const unsetDoc = `
    23  Set one or more configuration options for the specified service to their
    24  default. See also the set commmand to set one or more configuration options for
    25  a specified service.
    26  `
    27  
    28  func (c *UnsetCommand) Info() *cmd.Info {
    29  	return &cmd.Info{
    30  		Name:    "unset",
    31  		Args:    "<service> name ...",
    32  		Purpose: "set service config options back to their default",
    33  		Doc:     unsetDoc,
    34  	}
    35  }
    36  
    37  func (c *UnsetCommand) Init(args []string) error {
    38  	if len(args) == 0 {
    39  		return errors.New("no service name specified")
    40  	}
    41  	c.ServiceName = args[0]
    42  	c.Options = args[1:]
    43  	if len(c.Options) == 0 {
    44  		return errors.New("no configuration options specified")
    45  	}
    46  	return nil
    47  }
    48  
    49  // Run resets the configuration of a service.
    50  func (c *UnsetCommand) Run(ctx *cmd.Context) error {
    51  	apiclient, err := juju.NewAPIClientFromName(c.EnvName)
    52  	if err != nil {
    53  		return err
    54  	}
    55  	defer apiclient.Close()
    56  	return apiclient.ServiceUnset(c.ServiceName, c.Options)
    57  }