github.com/pf-qiu/concourse/v6@v6.7.3-0.20201207032516-1f455d73275f/fly/commands/get_team.go (about)

     1  package commands
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"sort"
     7  	"strings"
     8  
     9  	"github.com/pf-qiu/concourse/v6/fly/commands/internal/displayhelpers"
    10  	"github.com/pf-qiu/concourse/v6/fly/commands/internal/flaghelpers"
    11  	"github.com/pf-qiu/concourse/v6/fly/rc"
    12  	"github.com/pf-qiu/concourse/v6/fly/ui"
    13  	"github.com/fatih/color"
    14  )
    15  
    16  type GetTeamCommand struct {
    17  	Team flaghelpers.TeamFlag `short:"n" long:"team-name" required:"true" description:"Get configuration of this team"`
    18  	JSON bool                 `short:"j" long:"json" description:"Print command result as JSON"`
    19  }
    20  
    21  func (command *GetTeamCommand) Execute(args []string) error {
    22  	target, err := rc.LoadTarget(Fly.Target, Fly.Verbose)
    23  	if err != nil {
    24  		return err
    25  	}
    26  
    27  	if err := target.Validate(); err != nil {
    28  		return err
    29  	}
    30  
    31  	teamName := command.Team.Name()
    32  	team, err := target.FindTeam(teamName)
    33  	if err != nil {
    34  		return err
    35  	}
    36  
    37  	if command.JSON {
    38  		err := displayhelpers.JsonPrint(team.ATCTeam())
    39  		if err != nil {
    40  			return err
    41  		}
    42  		return nil
    43  	}
    44  
    45  	headers := ui.TableRow{
    46  		{Contents: "name/role", Color: color.New(color.Bold)},
    47  		{Contents: "users", Color: color.New(color.Bold)},
    48  		{Contents: "groups", Color: color.New(color.Bold)},
    49  	}
    50  	table := ui.Table{Headers: headers}
    51  	for role, auth := range team.Auth() {
    52  		row := ui.TableRow{
    53  			{Contents: fmt.Sprintf("%s/%s", team.Name(), role)},
    54  		}
    55  		var usersCell, groupsCell ui.TableCell
    56  		hasUsers := len(auth["users"]) != 0
    57  		hasGroups := len(auth["groups"]) != 0
    58  
    59  		if !hasUsers && !hasGroups {
    60  			usersCell.Contents = "all"
    61  			usersCell.Color = color.New(color.Faint)
    62  		} else if !hasUsers {
    63  			usersCell.Contents = "none"
    64  			usersCell.Color = color.New(color.Faint)
    65  		} else {
    66  			usersCell.Contents = strings.Join(auth["users"], ",")
    67  		}
    68  
    69  		if hasGroups {
    70  			groupsCell.Contents = strings.Join(auth["groups"], ",")
    71  		} else {
    72  			groupsCell.Contents = "none"
    73  			groupsCell.Color = color.New(color.Faint)
    74  		}
    75  
    76  		row = append(row, usersCell)
    77  		row = append(row, groupsCell)
    78  		table.Data = append(table.Data, row)
    79  	}
    80  	sort.Sort(table.Data)
    81  	return table.Render(os.Stdout, Fly.PrintTableHeaders)
    82  }