-
Notifications
You must be signed in to change notification settings - Fork 153
/
bootstrap_github.go
149 lines (116 loc) · 4.09 KB
/
bootstrap_github.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
137
138
139
140
141
142
143
144
145
146
147
148
149
package fluxexec
import (
"context"
"fmt"
"os/exec"
"reflect"
"strings"
)
type bootstrapGitHubConfig struct {
globalOptions []GlobalOption
bootstrapOptions []BootstrapOption
hostname string
interval string
owner string
path string
personal bool
private bool
readWriteKey bool
reconcile bool
repository string
team []string
}
var defaultBootstrapGitHubOptions = bootstrapGitHubConfig{
hostname: "github.com",
interval: "1m0s",
private: true,
}
// BootstrapGitHubOption represents options used in the BootstrapGitHub method.
type BootstrapGitHubOption interface {
configureBootstrapGitHub(*bootstrapGitHubConfig)
}
func (opt *HostnameOption) configureBootstrapGitHub(conf *bootstrapGitHubConfig) {
conf.hostname = opt.hostname
}
func (opt *IntervalOption) configureBootstrapGitHub(conf *bootstrapGitHubConfig) {
conf.interval = opt.interval
}
func (opt *OwnerOption) configureBootstrapGitHub(conf *bootstrapGitHubConfig) {
conf.owner = opt.owner
}
func (opt *PathOption) configureBootstrapGitHub(conf *bootstrapGitHubConfig) {
conf.path = opt.path
}
func (opt *PersonalOption) configureBootstrapGitHub(conf *bootstrapGitHubConfig) {
conf.personal = opt.personal
}
func (opt *PrivateOption) configureBootstrapGitHub(conf *bootstrapGitHubConfig) {
conf.private = opt.private
}
func (opt *ReadWriteKeyOption) configureBootstrapGitHub(conf *bootstrapGitHubConfig) {
conf.readWriteKey = opt.readWriteKey
}
func (opt *ReconcileOption) configureBootstrapGitHub(conf *bootstrapGitHubConfig) {
conf.reconcile = opt.reconcile
}
func (opt *RepositoryOption) configureBootstrapGitHub(conf *bootstrapGitHubConfig) {
conf.repository = opt.repository
}
func (opt *TeamOption) configureBootstrapGitHub(conf *bootstrapGitHubConfig) {
conf.team = append(conf.team, opt.team...)
}
func (flux *Flux) BootstrapGitHub(ctx context.Context, opts ...BootstrapGitHubOption) error {
bootstrapGitHubCmd, err := flux.bootstrapGitHubCmd(ctx, opts...)
if err != nil {
return err
}
if err := flux.runFluxCmd(ctx, bootstrapGitHubCmd); err != nil {
return err
}
return nil
}
func (flux *Flux) bootstrapGitHubCmd(ctx context.Context, opts ...BootstrapGitHubOption) (*exec.Cmd, error) {
c := defaultBootstrapGitHubOptions
for _, o := range opts {
o.configureBootstrapGitHub(&c)
}
args := []string{"bootstrap", "github"}
// Add the global args first.
globalArgs := flux.globalArgs(c.globalOptions...)
args = append(args, globalArgs...)
// The add the bootstrap args.
bootstrapArgs := flux.bootstrapArgs(c.bootstrapOptions...)
args = append(args, bootstrapArgs...)
// Then follow with the bootstrap github args.
if c.hostname != "" && !reflect.DeepEqual(c.hostname, defaultBootstrapGitHubOptions.hostname) {
args = append(args, "--hostname", c.hostname)
}
if c.interval != "" && !reflect.DeepEqual(c.interval, defaultBootstrapGitHubOptions.interval) {
args = append(args, "--interval", c.interval)
}
if c.owner != "" && !reflect.DeepEqual(c.owner, defaultBootstrapGitHubOptions.owner) {
args = append(args, "--owner", c.owner)
}
if c.path != "" && !reflect.DeepEqual(c.path, defaultBootstrapGitHubOptions.path) {
args = append(args, "--path", c.path)
}
if c.personal && !reflect.DeepEqual(c.personal, defaultBootstrapGitHubOptions.personal) {
args = append(args, "--personal")
}
if !reflect.DeepEqual(c.private, defaultBootstrapGitHubOptions.private) {
args = append(args, "--private", fmt.Sprintf("%v", c.private))
}
if c.readWriteKey && !reflect.DeepEqual(c.readWriteKey, defaultBootstrapGitHubOptions.readWriteKey) {
args = append(args, "--read-write-key")
}
if c.reconcile && !reflect.DeepEqual(c.reconcile, defaultBootstrapGitHubOptions.reconcile) {
args = append(args, "--reconcile")
}
if c.repository != "" && !reflect.DeepEqual(c.repository, defaultBootstrapGitHubOptions.repository) {
args = append(args, "--repository", c.repository)
}
if len(c.team) > 0 && !reflect.DeepEqual(c.team, defaultBootstrapGitHubOptions.team) {
args = append(args, "--team", strings.Join(c.team, ","))
}
return flux.buildFluxCmd(ctx, flux.env, args...), nil
}