github.com/rogpeppe/juju@v0.0.0-20140613142852-6337964b789e/cmd/juju/authorizedkeys_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  	"github.com/juju/cmd"
    11  	"launchpad.net/gnuflag"
    12  
    13  	"github.com/juju/juju/cmd/envcmd"
    14  	"github.com/juju/juju/juju"
    15  	"github.com/juju/juju/utils/ssh"
    16  )
    17  
    18  var listKeysDoc = `
    19  List a user's authorized ssh keys, allowing the holders of those keys to log on to Juju nodes.
    20  By default, just the key fingerprint is printed. Use --full to display the entire key.
    21  
    22  `
    23  
    24  // ListKeysCommand is used to list the authorized ssh keys.
    25  type ListKeysCommand struct {
    26  	envcmd.EnvCommandBase
    27  	showFullKey bool
    28  	user        string
    29  }
    30  
    31  func (c *ListKeysCommand) Info() *cmd.Info {
    32  	return &cmd.Info{
    33  		Name:    "list",
    34  		Doc:     listKeysDoc,
    35  		Purpose: "list authorized ssh keys for a specified user",
    36  	}
    37  }
    38  
    39  func (c *ListKeysCommand) SetFlags(f *gnuflag.FlagSet) {
    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  }