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

     1  package commands
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"sort"
     7  	"strings"
     8  
     9  	"github.com/chenbh/concourse/v6/fly/commands/internal/displayhelpers"
    10  	"github.com/chenbh/concourse/v6/fly/commands/internal/flaghelpers"
    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 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  		if err := displayhelpers.JsonPrint(team); err != nil {
    39  			return err
    40  		}
    41  		return nil
    42  	}
    43  
    44  	headers := ui.TableRow{
    45  		{Contents: "name/role", Color: color.New(color.Bold)},
    46  		{Contents: "users", Color: color.New(color.Bold)},
    47  		{Contents: "groups", Color: color.New(color.Bold)},
    48  	}
    49  	table := ui.Table{Headers: headers}
    50  	for role, auth := range team.Auth() {
    51  		row := ui.TableRow{
    52  			{Contents: fmt.Sprintf("%s/%s", team.Name(), role)},
    53  		}
    54  		var usersCell, groupsCell ui.TableCell
    55  		hasUsers := len(auth["users"]) != 0
    56  		hasGroups := len(auth["groups"]) != 0
    57  
    58  		if !hasUsers && !hasGroups {
    59  			usersCell.Contents = "all"
    60  			usersCell.Color = color.New(color.Faint)
    61  		} else if !hasUsers {
    62  			usersCell.Contents = "none"
    63  			usersCell.Color = color.New(color.Faint)
    64  		} else {
    65  			usersCell.Contents = strings.Join(auth["users"], ",")
    66  		}
    67  
    68  		if hasGroups {
    69  			groupsCell.Contents = strings.Join(auth["groups"], ",")
    70  		} else {
    71  			groupsCell.Contents = "none"
    72  			groupsCell.Color = color.New(color.Faint)
    73  		}
    74  
    75  		row = append(row, usersCell)
    76  		row = append(row, groupsCell)
    77  		table.Data = append(table.Data, row)
    78  	}
    79  	sort.Sort(table.Data)
    80  	return table.Render(os.Stdout, Fly.PrintTableHeaders)
    81  }