github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/worker/uniter/runner/jujuc/state-get.go (about)

     1  // Copyright 2020 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package jujuc
     5  
     6  import (
     7  	"github.com/juju/cmd/v3"
     8  	"github.com/juju/errors"
     9  	"github.com/juju/gnuflag"
    10  
    11  	jujucmd "github.com/juju/juju/cmd"
    12  )
    13  
    14  // StateGetCommand implements the state-get command.
    15  type StateGetCommand struct {
    16  	cmd.CommandBase
    17  	ctx Context
    18  	out cmd.Output
    19  
    20  	key    string // The key to show
    21  	strict bool
    22  }
    23  
    24  // NewStateGetCommand returns a state-get command.
    25  func NewStateGetCommand(ctx Context) (cmd.Command, error) {
    26  	return &StateGetCommand{ctx: ctx}, nil
    27  }
    28  
    29  // Info returns information about the Command.
    30  // Info implements part of the cmd.Command interface.
    31  func (c *StateGetCommand) Info() *cmd.Info {
    32  	doc := `
    33  state-get prints the value of the server side state specified by key.
    34  If no key is given, or if the key is "-", all keys and values will be printed.
    35  
    36  See also:
    37      state-delete
    38      state-set
    39  `
    40  	return jujucmd.Info(&cmd.Info{
    41  		Name:    "state-get",
    42  		Args:    "[<key>]",
    43  		Purpose: "print server-side-state value",
    44  		Doc:     doc,
    45  	})
    46  }
    47  
    48  // SetFlags adds command specific flags to the flag set.
    49  // SetFlags implements part of the cmd.Command interface.
    50  func (c *StateGetCommand) SetFlags(f *gnuflag.FlagSet) {
    51  	c.out.AddFlags(f, "smart", cmd.DefaultFormatters.Formatters())
    52  	f.BoolVar(&c.strict, "strict", false, "Return an error if the requested key does not exist")
    53  }
    54  
    55  // Init initializes the Command before running.
    56  // Init implements part of the cmd.Command interface.
    57  func (c *StateGetCommand) Init(args []string) error {
    58  	if args == nil {
    59  		return nil
    60  	}
    61  	c.key = ""
    62  	if len(args) > 0 {
    63  		if c.key = args[0]; c.key == "-" {
    64  			c.key = ""
    65  		}
    66  		args = args[1:]
    67  	}
    68  	return cmd.CheckEmpty(args)
    69  }
    70  
    71  // Run will execute the Command as directed by the options and positional
    72  // arguments passed to Init.
    73  // Run implements part of the cmd.Command interface.
    74  func (c *StateGetCommand) Run(ctx *cmd.Context) error {
    75  	if c.key == "" {
    76  		cache, err := c.ctx.GetCharmState()
    77  		if err != nil {
    78  			return err
    79  		}
    80  		return c.out.Write(ctx, cache)
    81  	}
    82  
    83  	value, err := c.ctx.GetCharmStateValue(c.key)
    84  	notFound := errors.IsNotFound(err)
    85  	if err != nil && (!notFound || (notFound && c.strict)) {
    86  		return err
    87  	}
    88  	return c.out.Write(ctx, value)
    89  }