forked from vmware-archive/fly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
set_team.go
136 lines (111 loc) · 3.28 KB
/
set_team.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package commands
import (
"encoding/json"
"fmt"
"os"
"github.com/concourse/atc"
"github.com/concourse/atc/auth/provider"
"github.com/concourse/fly/commands/internal/displayhelpers"
"github.com/concourse/fly/rc"
"github.com/concourse/fly/ui"
"github.com/vito/go-interact/interact"
)
type SetTeamCommand struct {
TeamName string `short:"n" long:"team-name" required:"true" description:"The team to create or modify"`
Authentication atc.AuthFlags `group:"Authentication"`
ProviderAuth map[string]provider.AuthConfig
}
func (command *SetTeamCommand) Execute([]string) error {
target, err := rc.LoadTarget(Fly.Target)
if err != nil {
return err
}
err = target.Validate()
if err != nil {
return err
}
err = command.ValidateFlags()
if err != nil {
return err
}
fmt.Println("Team Name:", command.TeamName)
fmt.Println("Basic Auth:", authMethodStatusDescription(command.Authentication.BasicAuth.IsConfigured()))
fmt.Println("GitHub Auth:", authMethodStatusDescription(command.ProviderAuth["github"].IsConfigured()))
fmt.Println("UAA Auth:", authMethodStatusDescription(command.ProviderAuth["uaa"].IsConfigured()))
fmt.Println("Generic OAuth:", authMethodStatusDescription(command.ProviderAuth["oauth"].IsConfigured()))
confirm := false
err = interact.NewInteraction("apply configuration?").Resolve(&confirm)
if err != nil {
return err
}
if !confirm {
displayhelpers.Failf("bailing out")
}
team := atc.Team{}
if command.Authentication.BasicAuth.IsConfigured() {
team.BasicAuth = &atc.BasicAuth{
BasicAuthUsername: command.Authentication.BasicAuth.Username,
BasicAuthPassword: command.Authentication.BasicAuth.Password,
}
}
teamAuth := make(map[string]*json.RawMessage)
for name, config := range command.ProviderAuth {
if config.IsConfigured() {
data, err := json.Marshal(config)
if err != nil {
return err
}
teamAuth[name] = (*json.RawMessage)(&data)
}
}
team.Auth = teamAuth
_, created, updated, err := target.Client().Team(command.TeamName).CreateOrUpdate(team)
if err != nil {
return err
}
if created {
fmt.Println("team created")
} else if updated {
fmt.Println("team updated")
}
return nil
}
func (command *SetTeamCommand) ValidateFlags() error {
isConfigured := false
if command.Authentication.BasicAuth.IsConfigured() {
err := command.Authentication.BasicAuth.Validate()
if err != nil {
return err
}
isConfigured = true
}
for _, p := range command.ProviderAuth {
if p.IsConfigured() {
err := p.Validate()
if err != nil {
return err
}
isConfigured = true
}
}
if !isConfigured {
if !command.Authentication.NoAuth {
fmt.Fprintln(ui.Stderr, "no auth methods configured! to continue, run:")
fmt.Fprintln(ui.Stderr, "")
fmt.Fprintln(ui.Stderr, " "+ui.Embolden("fly -t %s set-team -n %s --no-really-i-dont-want-any-auth", Fly.Target, command.TeamName))
fmt.Fprintln(ui.Stderr, "")
fmt.Fprintln(ui.Stderr, "this will leave the team open to anyone to mess with!")
os.Exit(1)
}
displayhelpers.PrintWarningHeader()
fmt.Fprintln(ui.Stderr, ui.WarningColor("no auth methods configured. you asked for it!"))
fmt.Fprintln(ui.Stderr, "")
}
return nil
}
func authMethodStatusDescription(enabled bool) string {
if enabled {
return "enabled"
}
return "disabled"
}