github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/cmd/juju/model/unset.go (about)

     1  // Copyright 2014 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package model
     5  
     6  import (
     7  	"fmt"
     8  	"strings"
     9  
    10  	"github.com/juju/cmd"
    11  
    12  	"github.com/juju/juju/cmd/juju/block"
    13  	"github.com/juju/juju/cmd/modelcmd"
    14  )
    15  
    16  func NewUnsetCommand() cmd.Command {
    17  	return modelcmd.Wrap(&unsetCommand{})
    18  }
    19  
    20  type unsetCommand struct {
    21  	modelcmd.ModelCommandBase
    22  	api  UnsetEnvironmentAPI
    23  	keys []string
    24  }
    25  
    26  // unsetEnvHelpDoc is multi-line since we need to use ` to denote
    27  // commands for ease in markdown.
    28  const unsetEnvHelpDoc = "" +
    29  	"A model key is reset to its default value. If it does not have such a\n" +
    30  	"value defined then it is removed.\n" +
    31  	"Attempting to remove a required key with no default value will result\n" +
    32  	"in an error.\n" +
    33  	"By default, the model is the current model.\n" +
    34  	"Model configuration key values can be viewed with `juju get-model-config`.\n" + unsetEnvHelpDocExamples
    35  
    36  const unsetEnvHelpDocExamples = `
    37  Examples:
    38  
    39      juju unset-model-config api-port test-mode
    40  
    41  See also: set-model-config
    42            get-model-config
    43  `
    44  
    45  func (c *unsetCommand) Info() *cmd.Info {
    46  	return &cmd.Info{
    47  		Name:    "unset-model-config",
    48  		Args:    "<model key> ...",
    49  		Purpose: "Unsets model configuration.",
    50  		Doc:     strings.TrimSpace(unsetEnvHelpDoc),
    51  	}
    52  }
    53  
    54  func (c *unsetCommand) Init(args []string) (err error) {
    55  	if len(args) == 0 {
    56  		return fmt.Errorf("no keys specified")
    57  	}
    58  	c.keys = args
    59  	return nil
    60  }
    61  
    62  type UnsetEnvironmentAPI interface {
    63  	Close() error
    64  	ModelGet() (map[string]interface{}, error)
    65  	ModelUnset(keys ...string) error
    66  }
    67  
    68  func (c *unsetCommand) getAPI() (UnsetEnvironmentAPI, error) {
    69  	if c.api != nil {
    70  		return c.api, nil
    71  	}
    72  	return c.NewAPIClient()
    73  }
    74  
    75  func (c *unsetCommand) Run(ctx *cmd.Context) error {
    76  	client, err := c.getAPI()
    77  	if err != nil {
    78  		return err
    79  	}
    80  	defer client.Close()
    81  
    82  	// extra call to the API to retrieve env config
    83  	envAttrs, err := client.ModelGet()
    84  	if err != nil {
    85  		return err
    86  	}
    87  	for _, key := range c.keys {
    88  		// check if the key exists in the existing env config
    89  		// and warn the user if the key is not defined in
    90  		// the existing config
    91  		if _, exists := envAttrs[key]; !exists {
    92  			logger.Warningf("key %q is not defined in the current model configuration: possible misspelling", key)
    93  		}
    94  
    95  	}
    96  	return block.ProcessBlockedError(client.ModelUnset(c.keys...), block.BlockChange)
    97  }