github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/cmd/juju/user/list.go (about)

     1  // Copyright 2014 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for infos.
     3  
     4  package user
     5  
     6  import (
     7  	"bytes"
     8  	"fmt"
     9  	"text/tabwriter"
    10  
    11  	"github.com/juju/cmd"
    12  	"github.com/juju/errors"
    13  	"launchpad.net/gnuflag"
    14  
    15  	"github.com/juju/juju/api/usermanager"
    16  	"github.com/juju/juju/cmd/modelcmd"
    17  )
    18  
    19  var usageListUsersSummary = `
    20  Lists Juju users allowed to connect to a controller.`[1:]
    21  
    22  var usageListUsersDetails = `
    23  By default, the tabular format is used.
    24  
    25  Examples:
    26      juju list-users
    27  
    28  See also: 
    29      add-user
    30      register
    31      show-user
    32      disable-user
    33      enable-user`[1:]
    34  
    35  func NewListCommand() cmd.Command {
    36  	return modelcmd.WrapController(&listCommand{})
    37  }
    38  
    39  // listCommand shows all the users in the Juju server.
    40  type listCommand struct {
    41  	infoCommandBase
    42  	All bool
    43  }
    44  
    45  // Info implements Command.Info.
    46  func (c *listCommand) Info() *cmd.Info {
    47  	return &cmd.Info{
    48  		Name:    "list-users",
    49  		Purpose: usageListUsersSummary,
    50  		Doc:     usageListUsersDetails,
    51  	}
    52  }
    53  
    54  // SetFlags implements Command.SetFlags.
    55  func (c *listCommand) SetFlags(f *gnuflag.FlagSet) {
    56  	c.infoCommandBase.SetFlags(f)
    57  	f.BoolVar(&c.All, "all", false, "Include disabled users")
    58  	c.out.AddFlags(f, "tabular", map[string]cmd.Formatter{
    59  		"yaml":    cmd.FormatYaml,
    60  		"json":    cmd.FormatJson,
    61  		"tabular": c.formatTabular,
    62  	})
    63  }
    64  
    65  // Run implements Command.Run.
    66  func (c *listCommand) Run(ctx *cmd.Context) (err error) {
    67  	// Note: the InfoCommandBase and the UserInfo struct are defined
    68  	// in info.go.
    69  	client, err := c.getUserInfoAPI()
    70  	if err != nil {
    71  		return err
    72  	}
    73  	defer client.Close()
    74  
    75  	result, err := client.UserInfo(nil, usermanager.IncludeDisabled(c.All))
    76  	if err != nil {
    77  		return err
    78  	}
    79  
    80  	return c.out.Write(ctx, c.apiUsersToUserInfoSlice(result))
    81  }
    82  
    83  func (c *listCommand) formatTabular(value interface{}) ([]byte, error) {
    84  	users, valueConverted := value.([]UserInfo)
    85  	if !valueConverted {
    86  		return nil, errors.Errorf("expected value of type %T, got %T", users, value)
    87  	}
    88  	var out bytes.Buffer
    89  	const (
    90  		// To format things into columns.
    91  		minwidth = 0
    92  		tabwidth = 1
    93  		padding  = 2
    94  		padchar  = ' '
    95  		flags    = 0
    96  	)
    97  	tw := tabwriter.NewWriter(&out, minwidth, tabwidth, padding, padchar, flags)
    98  	fmt.Fprintf(tw, "NAME\tDISPLAY NAME\tDATE CREATED\tLAST CONNECTION\n")
    99  	for _, user := range users {
   100  		conn := user.LastConnection
   101  		if user.Disabled {
   102  			conn += " (disabled)"
   103  		}
   104  		fmt.Fprintf(tw, "%s\t%s\t%s\t%s\n", user.Username, user.DisplayName, user.DateCreated, conn)
   105  	}
   106  	tw.Flush()
   107  	return out.Bytes(), nil
   108  }