github.com/cloudbase/juju-core@v0.0.0-20140504232958-a7271ac7912f/cmd/juju/authorisedkeys_list.go (about)

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