-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
106 lines (92 loc) · 2.55 KB
/
main.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
package main
import (
"bufio"
"fmt"
"os"
"time"
"github.com/leodido/maintainers-generator/pkg/version"
"github.com/sirupsen/logrus"
"k8s.io/test-infra/prow/config/secret"
)
var defaultBanner = "# Do not edit this file manually.\n# Made using github.com/leodido/maintainers-generator"
func main() {
o := NewOptions()
// Early exit in case user wants to only know the version
if o.version {
fmt.Println(version.String())
os.Exit(0)
}
// Validate flags
if err := o.Validate(); err != nil {
logrus.WithError(err).Fatal("Invalid options")
os.Exit(1)
}
// Create a secrets agent
secretsAgent := &secret.Agent{}
if err := secretsAgent.Start([]string{o.github.TokenPath}); err != nil {
logrus.WithError(err).Fatal("Unable to start secrets agent.")
}
// Create a GitHub client
// todo > use o.github.GitHubClientWithAccessToken() ?
ghClient, err := o.github.GitHubClient(secretsAgent, o.dryRun)
if err != nil {
logrus.WithError(err).Fatal("Unable to instantiate GitHub client.")
}
// Create a Git client
gitClient, err := o.github.GitClient(secretsAgent, o.dryRun)
if err != nil {
logrus.WithError(err).Fatal("Unable to instantiate git client.")
}
// Obtain maintainers
maintainers, err := getMaintainers(ghClient, gitClient, o)
if err != nil {
logrus.WithField("organization", o.org).Fatal(err)
}
// Output YAML YaY!
out, err := maintainers.Encode()
if err != nil {
logrus.WithError(err).Fatal("Unable to generate YAML maintainers file.")
}
var output *os.File
if o.outputFile == "stdout" || o.outputFile == "/dev/stdout" {
output = os.Stdout
} else {
output, err = os.Create(o.outputFile)
if err != nil {
logrus.WithError(err).WithField("output", o.outputFile).Fatal("Unable to create output file.")
}
defer func() {
if err := output.Close(); err != nil {
logrus.WithError(err).WithField("output", o.outputFile).Fatal("Unable to close output file.")
}
}()
}
w := bufio.NewWriter(output)
n1 := 0
if o.banner {
dt := time.Now()
n1, err = w.WriteString(
fmt.Sprintf(
"%s on %d-%02d-%02dT%02d:%02d:%02d\n",
defaultBanner,
dt.Year(),
dt.Month(),
dt.Day(),
dt.Hour(),
dt.Minute(),
dt.Second(),
),
)
if err != nil {
logrus.WithError(err).WithField("output", o.outputFile).Fatal("Unable to write output file.")
}
}
n2, err := w.WriteString(out)
if err != nil {
logrus.WithError(err).WithField("output", o.outputFile).Fatal("Unable to write output file.")
}
w.Flush()
if o.outputFile != "stdout" {
logrus.WithField("no. of bytes", n1+n2).Info("Wrote output file.")
}
}