github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/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  )
    17  
    18  const ListCommandDoc = `
    19  List all the current users in the Juju server.
    20  
    21  See Also:
    22     juju help user info
    23  `
    24  
    25  // ListCommand shows all the users in the Juju server.
    26  type ListCommand struct {
    27  	InfoCommandBase
    28  	all bool
    29  }
    30  
    31  // Info implements Command.Info.
    32  func (c *ListCommand) Info() *cmd.Info {
    33  	return &cmd.Info{
    34  		Name:    "list",
    35  		Purpose: "shows all users",
    36  		Doc:     ListCommandDoc,
    37  	}
    38  }
    39  
    40  // SetFlags implements Command.SetFlags.
    41  func (c *ListCommand) SetFlags(f *gnuflag.FlagSet) {
    42  	c.InfoCommandBase.SetFlags(f)
    43  	f.BoolVar(&c.all, "all", false, "include disabled users in the listing")
    44  	c.out.AddFlags(f, "tabular", map[string]cmd.Formatter{
    45  		"yaml":    cmd.FormatYaml,
    46  		"json":    cmd.FormatJson,
    47  		"tabular": c.formatTabular,
    48  	})
    49  }
    50  
    51  // Run implements Command.Run.
    52  func (c *ListCommand) Run(ctx *cmd.Context) (err error) {
    53  	// Note: the InfoCommandBase and the UserInfo struct are defined
    54  	// in info.go.
    55  	client, err := c.getUserInfoAPI()
    56  	if err != nil {
    57  		return err
    58  	}
    59  	defer client.Close()
    60  
    61  	result, err := client.UserInfo(nil, usermanager.IncludeDisabled(c.all))
    62  	if err != nil {
    63  		return err
    64  	}
    65  
    66  	return c.out.Write(ctx, c.apiUsersToUserInfoSlice(result))
    67  }
    68  
    69  func (c *ListCommand) formatTabular(value interface{}) ([]byte, error) {
    70  	users, valueConverted := value.([]UserInfo)
    71  	if !valueConverted {
    72  		return nil, errors.Errorf("expected value of type %T, got %T", users, value)
    73  	}
    74  	var out bytes.Buffer
    75  	const (
    76  		// To format things into columns.
    77  		minwidth = 0
    78  		tabwidth = 1
    79  		padding  = 2
    80  		padchar  = ' '
    81  		flags    = 0
    82  	)
    83  	tw := tabwriter.NewWriter(&out, minwidth, tabwidth, padding, padchar, flags)
    84  	fmt.Fprintf(tw, "NAME\tDISPLAY NAME\tDATE CREATED\tLAST CONNECTION\n")
    85  	for _, user := range users {
    86  		conn := user.LastConnection
    87  		if user.Disabled {
    88  			conn += " (disabled)"
    89  		}
    90  		fmt.Fprintf(tw, "%s\t%s\t%s\t%s\n", user.Username, user.DisplayName, user.DateCreated, conn)
    91  	}
    92  	tw.Flush()
    93  	return out.Bytes(), nil
    94  }