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

     1  package commands
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"sort"
     7  
     8  	"strings"
     9  
    10  	"github.com/chenbh/concourse/v6/fly/commands/internal/displayhelpers"
    11  	"github.com/chenbh/concourse/v6/fly/rc"
    12  	"github.com/chenbh/concourse/v6/fly/ui"
    13  	"github.com/fatih/color"
    14  )
    15  
    16  type TeamsCommand struct {
    17  	Json    bool `long:"json" description:"Print command result as JSON"`
    18  	Details bool `short:"d" long:"details" description:"Print authentication configuration"`
    19  }
    20  
    21  func (command *TeamsCommand) Execute([]string) error {
    22  	target, err := rc.LoadTarget(Fly.Target, Fly.Verbose)
    23  	if err != nil {
    24  		return err
    25  	}
    26  
    27  	err = target.Validate()
    28  	if err != nil {
    29  		return err
    30  	}
    31  
    32  	teams, err := target.Client().ListTeams()
    33  	if err != nil {
    34  		return err
    35  	}
    36  
    37  	if command.Json {
    38  		err = displayhelpers.JsonPrint(teams)
    39  		if err != nil {
    40  			return err
    41  		}
    42  		return nil
    43  	}
    44  
    45  	var headers ui.TableRow
    46  	if command.Details {
    47  		headers = ui.TableRow{
    48  			{Contents: "name/role", Color: color.New(color.Bold)},
    49  			{Contents: "users", Color: color.New(color.Bold)},
    50  			{Contents: "groups", Color: color.New(color.Bold)},
    51  		}
    52  	} else {
    53  		headers = ui.TableRow{
    54  			{Contents: "name", Color: color.New(color.Bold)},
    55  		}
    56  	}
    57  
    58  	table := ui.Table{Headers: headers}
    59  
    60  	for _, t := range teams {
    61  
    62  		if command.Details {
    63  			for role, auth := range t.Auth {
    64  				row := ui.TableRow{
    65  					{Contents: fmt.Sprintf("%s/%s", t.Name, role)},
    66  				}
    67  				var usersCell, groupsCell ui.TableCell
    68  
    69  				hasUsers := len(auth["users"]) != 0
    70  				hasGroups := len(auth["groups"]) != 0
    71  
    72  				if !hasUsers && !hasGroups {
    73  					usersCell.Contents = "all"
    74  					usersCell.Color = color.New(color.Faint)
    75  				} else if !hasUsers {
    76  					usersCell.Contents = "none"
    77  					usersCell.Color = color.New(color.Faint)
    78  				} else {
    79  					usersCell.Contents = strings.Join(auth["users"], ",")
    80  				}
    81  
    82  				if hasGroups {
    83  					groupsCell.Contents = strings.Join(auth["groups"], ",")
    84  				} else {
    85  					groupsCell.Contents = "none"
    86  					groupsCell.Color = color.New(color.Faint)
    87  				}
    88  
    89  				row = append(row, usersCell)
    90  				row = append(row, groupsCell)
    91  				table.Data = append(table.Data, row)
    92  			}
    93  
    94  		} else {
    95  			row := ui.TableRow{
    96  				{Contents: t.Name},
    97  			}
    98  			table.Data = append(table.Data, row)
    99  		}
   100  	}
   101  
   102  	sort.Sort(table.Data)
   103  
   104  	return table.Render(os.Stdout, Fly.PrintTableHeaders)
   105  }