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

     1  package commands
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"sort"
     7  
     8  	"github.com/chenbh/concourse/v6/atc"
     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/chenbh/concourse/v6/go-concourse/concourse"
    14  	"github.com/chenbh/concourse/v6/skymarshal/skycmd"
    15  	"github.com/jessevdk/go-flags"
    16  	"github.com/vito/go-interact/interact"
    17  )
    18  
    19  func WireTeamConnectors(command *flags.Command) {
    20  	for _, group := range command.Groups() {
    21  		if group.ShortDescription == "Authentication" {
    22  			skycmd.WireTeamConnectors(group)
    23  			return
    24  		}
    25  	}
    26  }
    27  
    28  type SetTeamCommand struct {
    29  	Team            flaghelpers.TeamFlag `short:"n" long:"team-name" required:"true" description:"The team to create or modify"`
    30  	SkipInteractive bool                 `long:"non-interactive" description:"Force apply configuration"`
    31  	AuthFlags       skycmd.AuthTeamFlags `group:"Authentication"`
    32  }
    33  
    34  func (command *SetTeamCommand) Validate() ([]concourse.ConfigWarning, error) {
    35  	var warnings []concourse.ConfigWarning
    36  	if warning := atc.ValidateIdentifier(command.Team.Name(), "team"); warning != nil {
    37  		warnings = append(warnings, concourse.ConfigWarning{
    38  			Type:    warning.Type,
    39  			Message: warning.Message,
    40  		})
    41  	}
    42  	return warnings, nil
    43  }
    44  
    45  func (command *SetTeamCommand) Execute([]string) error {
    46  	warnings, err := command.Validate()
    47  	if err != nil {
    48  		return err
    49  	}
    50  
    51  	target, err := rc.LoadTarget(Fly.Target, Fly.Verbose)
    52  	if err != nil {
    53  		return err
    54  	}
    55  
    56  	err = target.Validate()
    57  	if err != nil {
    58  		return err
    59  	}
    60  
    61  	authRoles, err := command.AuthFlags.Format()
    62  	if err != nil {
    63  		fmt.Fprintln(ui.Stderr, "error:", err)
    64  		os.Exit(1)
    65  	}
    66  
    67  	roles := []string{}
    68  	for role := range authRoles {
    69  		roles = append(roles, role)
    70  	}
    71  	sort.Strings(roles)
    72  
    73  	teamName := command.Team.Name()
    74  	fmt.Println("setting team:", ui.Embolden("%s", teamName))
    75  
    76  	for _, role := range roles {
    77  		authUsers := authRoles[role]["users"]
    78  		authGroups := authRoles[role]["groups"]
    79  
    80  		fmt.Println()
    81  		fmt.Printf("role %s:\n", ui.Embolden(role))
    82  		fmt.Printf("  users:\n")
    83  		if len(authUsers) > 0 {
    84  			for _, user := range authUsers {
    85  				fmt.Printf("  - %s\n", user)
    86  			}
    87  		} else {
    88  			fmt.Printf("    %s\n", ui.OffColor.Sprint("none"))
    89  		}
    90  
    91  		fmt.Println()
    92  		fmt.Printf("  groups:\n")
    93  		if len(authGroups) > 0 {
    94  			for _, group := range authGroups {
    95  				fmt.Printf("  - %s\n", group)
    96  			}
    97  		} else {
    98  			fmt.Printf("    %s\n", ui.OffColor.Sprint("none"))
    99  		}
   100  	}
   101  
   102  	if len(warnings) > 0 {
   103  		displayhelpers.ShowWarnings(warnings)
   104  	}
   105  
   106  	confirm := true
   107  	if !command.SkipInteractive {
   108  		confirm = false
   109  		err = interact.NewInteraction("\napply team configuration?").Resolve(&confirm)
   110  		if err != nil {
   111  			return err
   112  		}
   113  	}
   114  
   115  	if !confirm {
   116  		displayhelpers.Failf("bailing out")
   117  	}
   118  
   119  	team := atc.Team{Auth: authRoles}
   120  
   121  	_, created, updated, warnings, err := target.Client().Team(teamName).CreateOrUpdate(team)
   122  	if err != nil {
   123  		return err
   124  	}
   125  
   126  	if len(warnings) > 0 {
   127  		displayhelpers.ShowWarnings(warnings)
   128  	}
   129  
   130  	if created {
   131  		fmt.Println("team created")
   132  	} else if updated {
   133  		fmt.Println("team updated")
   134  	}
   135  
   136  	return nil
   137  }