github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/cmd/juju/user/credentials.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package user
     5  
     6  import (
     7  	"fmt"
     8  
     9  	"github.com/juju/cmd"
    10  	"github.com/juju/errors"
    11  	"github.com/juju/names"
    12  	"launchpad.net/gnuflag"
    13  )
    14  
    15  const userCredentialsDoc = `
    16  Writes out the current user and credentials to a file that can be used
    17  with 'juju system login' to allow the user to access the same environments
    18  as the same user from another machine.
    19  
    20  Examples:
    21  
    22      $ juju user credentials --output staging.creds
    23  
    24      # copy the staging.creds file to another machine
    25  
    26      $ juju system login staging --server staging.creds --keep-password
    27  
    28  
    29  See Also:
    30      juju system login
    31  `
    32  
    33  // CredentialsCommand changes the password for a user.
    34  type CredentialsCommand struct {
    35  	UserCommandBase
    36  	OutPath string
    37  }
    38  
    39  // Info implements Command.Info.
    40  func (c *CredentialsCommand) Info() *cmd.Info {
    41  	return &cmd.Info{
    42  		Name:    "credentials",
    43  		Purpose: "save the credentials and server details to a file",
    44  		Doc:     userCredentialsDoc,
    45  	}
    46  }
    47  
    48  // SetFlags implements Command.SetFlags.
    49  func (c *CredentialsCommand) SetFlags(f *gnuflag.FlagSet) {
    50  	f.StringVar(&c.OutPath, "o", "", "specifies the path of the generated file")
    51  	f.StringVar(&c.OutPath, "output", "", "")
    52  }
    53  
    54  // Run implements Command.Run.
    55  func (c *CredentialsCommand) Run(ctx *cmd.Context) error {
    56  	creds, err := c.ConnectionCredentials()
    57  	if err != nil {
    58  		return errors.Trace(err)
    59  	}
    60  
    61  	filename := c.OutPath
    62  	if filename == "" {
    63  		// The reason for the dance though the newUserTag
    64  		// is to strip off the optional provider.
    65  		//   user -> user
    66  		//   user@local -> user
    67  		//   user@remote -> user
    68  		name := names.NewUserTag(creds.User).Name()
    69  		filename = fmt.Sprintf("%s.server", name)
    70  	}
    71  	return writeServerFile(c, ctx, creds.User, creds.Password, filename)
    72  }