-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
165 lines (143 loc) · 4.45 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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"sort"
"strings"
"github.com/google/go-github/github"
"github.com/urfave/cli"
"golang.org/x/oauth2"
"gopkg.in/yaml.v2"
"github.com/teamhephy/deisrel/actions"
"github.com/teamhephy/deisrel/components"
)
var version = "0.0.0" // replaced when building
func main() {
app := cli.NewApp()
token, ok := os.LookupEnv("GITHUB_ACCESS_TOKEN")
if !ok {
fmt.Println("\"GITHUB_ACCESS_TOKEN\" must be set to a valid GitHub access token.")
os.Exit(1)
}
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
ghclient := github.NewClient(oauth2.NewClient(oauth2.NoContext, ts))
app.Name = "deisrel"
app.Usage = "Manage deis workflow releases."
app.UsageText = "deisrel [options] <chart versions> <repo map>"
app.Version = version
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "output, o",
Value: "pretty",
Usage: "format to print output in. Valid options: [pretty, json]",
},
}
app.Commands = []cli.Command{
cli.Command{
Name: "release",
Action: actions.ReleaseComponent(ghclient, os.Stdout),
Usage: "create a new tagged release of a Hephy component",
ArgsUsage: "<component> <new-tag>",
Flags: []cli.Flag{
cli.BoolTFlag{
Name: "dry-run",
Usage: "print the actions to be taken, but don't change anything (default: true)",
},
cli.StringFlag{
Name: "sha",
Usage: "commit to be tagged as a new release. (default: HEAD of master)",
},
},
},
cli.Command{
Name: "changelog",
Subcommands: []cli.Command{
cli.Command{
Name: "global",
Action: actions.GenerateChangelog(ghclient, os.Stdout),
Usage: "deisrel changelog global <previous chart requirements.lock> <repo map>",
Description: "Aggregate changelog entries from all known repositories for a specified release",
},
cli.Command{
Name: "individual",
Action: actions.GenerateIndividualChangelog(ghclient, os.Stdout),
Usage: "deisrel changelog individual <repo-name> <new-release>",
Description: "Generate a changelog entry for an changes on an individual repository, from a specified old release through a specified git SHA. The release will be called the specified new release in the changelog's title",
Flags: []cli.Flag{
cli.StringFlag{
Name: "base-tag",
Usage: "old tag where changelog starts. Optional, defaults to most recent tag",
},
cli.StringFlag{
Name: "sha",
Usage: "commit that will be tagged in the new release. Optional, defaults to most recent commit on master.",
},
},
},
},
},
}
app.Action = func(c *cli.Context) error {
res := make(map[string]interface{})
if c.NArg() < 2 {
return cli.NewExitError("A requirements.lock file and a repo mapping file are required", 1)
}
out, err := ioutil.ReadFile(c.Args().Get(0))
if err != nil {
return cli.NewExitError(err.Error(), 2)
}
err = yaml.Unmarshal(out, &res)
if err != nil {
return cli.NewExitError(err.Error(), 3)
}
mapping := make(map[string]string)
out, err = ioutil.ReadFile(c.Args().Get(1))
if err != nil {
return cli.NewExitError(err.Error(), 2)
}
err = json.Unmarshal(out, &mapping)
if err != nil {
return cli.NewExitError(err.Error(), 3)
}
versions, err := components.CheckVersions(res, mapping, ghclient)
if err != nil {
return cli.NewExitError(err.Error(), 4)
}
sort.Sort(components.ByName(versions))
if c.String("output") == "json" {
out, err := json.MarshalIndent(versions, "", " ")
if err != nil {
return cli.NewExitError(err.Error(), 5)
}
fmt.Println(string(out))
} else if c.String("output") == "pretty" {
// Add padding for version name to all output lines up
longestName := 0
for _, version := range versions {
name := len(version.Name)
if name > longestName {
longestName = name
}
}
for _, version := range versions {
cleanMsg := "clean"
if !version.Clean {
cleanMsg = "dirty"
}
padding := strings.Repeat(" ", longestName-len(version.Name))
fmt.Printf("%s%s %s -> %s (%s)\n", version.Name, padding, version.ChartVersion, version.ComponentVersion, cleanMsg)
if !version.Clean {
fmt.Printf("\t%s has unreleased changes. See %s\n", version.Name, version.Diff)
}
}
} else {
return cli.NewExitError(fmt.Sprintf("Unrecognized output format: %s", c.String("output")), 1)
}
return nil
}
app.Run(os.Args)
}