github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/cmd/juju/commands/authorizedkeys_list.go (about)

     1  // Copyright 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package commands
     5  
     6  import (
     7  	"fmt"
     8  	"strings"
     9  
    10  	"github.com/juju/cmd"
    11  	"launchpad.net/gnuflag"
    12  
    13  	"github.com/juju/juju/utils/ssh"
    14  )
    15  
    16  var listKeysDoc = `
    17  List a user's authorized ssh keys, allowing the holders of those keys to log on to Juju nodes.
    18  By default, just the key fingerprint is printed. Use --full to display the entire key.
    19  
    20  `
    21  
    22  // ListKeysCommand is used to list the authorized ssh keys.
    23  type ListKeysCommand struct {
    24  	AuthorizedKeysBase
    25  	showFullKey bool
    26  	user        string
    27  }
    28  
    29  func (c *ListKeysCommand) Info() *cmd.Info {
    30  	return &cmd.Info{
    31  		Name:    "list",
    32  		Doc:     listKeysDoc,
    33  		Purpose: "list authorised ssh keys for a specified user",
    34  	}
    35  }
    36  
    37  func (c *ListKeysCommand) SetFlags(f *gnuflag.FlagSet) {
    38  	f.BoolVar(&c.showFullKey, "full", false, "show full key instead of just the key fingerprint")
    39  	f.StringVar(&c.user, "user", "admin", "the user for which to list the keys")
    40  }
    41  
    42  func (c *ListKeysCommand) Run(context *cmd.Context) error {
    43  	client, err := c.NewKeyManagerClient()
    44  	if err != nil {
    45  		return err
    46  	}
    47  	defer client.Close()
    48  
    49  	mode := ssh.Fingerprints
    50  	if c.showFullKey {
    51  		mode = ssh.FullKeys
    52  	}
    53  	results, err := client.ListKeys(mode, c.user)
    54  	if err != nil {
    55  		return err
    56  	}
    57  	result := results[0]
    58  	if result.Error != nil {
    59  		return result.Error
    60  	}
    61  	fmt.Fprintf(context.Stdout, "Keys for user %s:\n", c.user)
    62  	fmt.Fprintln(context.Stdout, strings.Join(result.Result, "\n"))
    63  	return nil
    64  }