github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/worker/uniter/runner/jujuc/credential-get.go (about)

     1  // Copyright 2018 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package jujuc
     5  
     6  import (
     7  	"github.com/juju/cmd"
     8  	"github.com/juju/errors"
     9  	"github.com/juju/gnuflag"
    10  
    11  	jujucmd "github.com/juju/juju/cmd"
    12  )
    13  
    14  // CredentialGetCommand implements the leader-get command.
    15  type CredentialGetCommand struct {
    16  	cmd.CommandBase
    17  	ctx Context
    18  	out cmd.Output
    19  }
    20  
    21  // NewCredentialGetCommand returns a new CredentialGetCommand with the given context.
    22  func NewCredentialGetCommand(ctx Context) (cmd.Command, error) {
    23  	return &CredentialGetCommand{ctx: ctx}, nil
    24  }
    25  
    26  // Info is part of the cmd.Command interface.
    27  func (c *CredentialGetCommand) Info() *cmd.Info {
    28  	doc := `
    29  credential-get returns the cloud specification used by the unit's model.
    30  `
    31  	return jujucmd.Info(&cmd.Info{
    32  		Name:    "credential-get",
    33  		Args:    "",
    34  		Purpose: "access cloud credentials",
    35  		Doc:     doc,
    36  	})
    37  }
    38  
    39  // SetFlags is part of the cmd.Command interface.
    40  func (c *CredentialGetCommand) SetFlags(f *gnuflag.FlagSet) {
    41  	c.out.AddFlags(f, "smart", cmd.DefaultFormatters)
    42  }
    43  
    44  // Init is part of the cmd.Command interface.
    45  func (c *CredentialGetCommand) Init(args []string) error {
    46  	return cmd.CheckEmpty(args)
    47  }
    48  
    49  // Run is part of the cmd.Command interface.
    50  func (c *CredentialGetCommand) Run(ctx *cmd.Context) error {
    51  	credential, err := c.ctx.CloudSpec()
    52  	if err != nil {
    53  		return errors.Annotatef(err, "cannot access cloud credentials")
    54  	}
    55  	return c.out.Write(ctx, credential)
    56  }