github.com/chenbh/concourse/v6@v6.4.2/fly/commands/userinfo.go (about)

     1  package commands
     2  
     3  import (
     4  	"os"
     5  	"sort"
     6  	"strings"
     7  
     8  	"github.com/chenbh/concourse/v6/fly/commands/internal/displayhelpers"
     9  	"github.com/chenbh/concourse/v6/fly/rc"
    10  	"github.com/chenbh/concourse/v6/fly/ui"
    11  	"github.com/fatih/color"
    12  )
    13  
    14  type UserinfoCommand struct {
    15  	Json bool `long:"json" description:"Print command result as JSON"`
    16  }
    17  
    18  func (command *UserinfoCommand) Execute([]string) error {
    19  	target, err := rc.LoadTarget(Fly.Target, Fly.Verbose)
    20  	if err != nil {
    21  		return err
    22  	}
    23  
    24  	err = target.Validate()
    25  	if err != nil {
    26  		return err
    27  	}
    28  
    29  	userinfo, err := target.Client().UserInfo()
    30  	if err != nil {
    31  		return err
    32  	}
    33  
    34  	if command.Json {
    35  		err = displayhelpers.JsonPrint(userinfo)
    36  		if err != nil {
    37  			return err
    38  		}
    39  		return nil
    40  	}
    41  
    42  	headers := ui.TableRow{
    43  		{Contents: "username", Color: color.New(color.Bold)},
    44  		{Contents: "team/role", Color: color.New(color.Bold)},
    45  	}
    46  
    47  	table := ui.Table{Headers: headers}
    48  
    49  	var teamRoles []string
    50  	for team, roles := range userinfo.Teams {
    51  		for _, role := range roles {
    52  			teamRoles = append(teamRoles, team+"/"+role)
    53  		}
    54  	}
    55  
    56  	sort.Strings(teamRoles)
    57  
    58  	row := ui.TableRow{
    59  		{Contents: userinfo.UserName},
    60  		{Contents: strings.Join(teamRoles, ",")},
    61  	}
    62  
    63  	table.Data = append(table.Data, row)
    64  
    65  	return table.Render(os.Stdout, Fly.PrintTableHeaders)
    66  }