/
publish_to_github.go
165 lines (139 loc) · 4.6 KB
/
publish_to_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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
* Copyright NetFoundry, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package cmd
import (
"fmt"
"github.com/spf13/cobra"
"os"
"path/filepath"
"strings"
)
type publishToGithubCmd struct {
BaseCommand
name string
archiveBase string
preRelease bool
}
type githubArtifact struct {
name string
sourceName string
sourcePath string
arch string
os string
}
func (cmd *publishToGithubCmd) Execute() {
cmd.name = "ziti"
if len(cmd.Args) > 0 {
cmd.name = cmd.Args[0]
}
if !cmd.Cmd.Flags().Changed("archive-base") {
cmd.archiveBase = cmd.name
}
cmd.EvalCurrentAndNextVersion()
releaseDir, err := filepath.Abs("./release")
cmd.exitIfErrf(err, "could not get absolute path for releases directory")
archDirs, err := os.ReadDir(releaseDir)
cmd.exitIfErrf(err, "failed to read releases dir: %v\n", err)
var artifacts []*githubArtifact
for _, archDir := range archDirs {
arch := archDir.Name()
cmd.Infof("processing files for arch: %v\n", arch)
archDirPath := filepath.Join(releaseDir, archDir.Name())
if archDir.IsDir() {
osDirs, err := os.ReadDir(archDirPath)
cmd.exitIfErrf(err, "failed to read arch dir %v: %v\n", archDirPath, err)
for _, osDir := range osDirs {
osName := osDir.Name()
cmd.Infof("processing files for: %v/%v\n", arch, osName)
osDirPath := filepath.Join(archDirPath, osDir.Name())
releasableFiles, err := os.ReadDir(osDirPath)
cmd.exitIfErrf(err, "failed to read os dir %v: %v\n", osDirPath, err)
for _, releasableFile := range releasableFiles {
if !releasableFile.IsDir() && !strings.HasSuffix(releasableFile.Name(), ".gz") {
name := releasableFile.Name()
if strings.HasSuffix(name, ".exe") {
name = strings.TrimSuffix(name, ".exe")
}
filePath := filepath.Join(osDirPath, releasableFile.Name())
artifacts = append(artifacts, &githubArtifact{
name: name,
sourceName: releasableFile.Name(),
sourcePath: filePath,
arch: arch,
os: osName,
})
}
}
}
}
}
bundleMap := map[string][]*githubArtifact{}
for _, artifact := range artifacts {
bundle := artifact.os + "-" + artifact.arch
list := bundleMap[bundle]
list = append(list, artifact)
bundleMap[bundle] = list
}
version := cmd.getPublishVersion().String()
var releaseArtifacts []string
for k, v := range bundleMap {
if strings.Contains(k, "windows") {
file := fmt.Sprintf("release/%v-%v-%v.zip", cmd.name, k, version)
cmd.Infof("Creating release archive %v\n", file)
cmd.zipGhArtifacts(cmd.name, file, v...)
releaseArtifacts = append(releaseArtifacts, file)
} else {
file := fmt.Sprintf("release/%v-%v-%v.tar.gz", cmd.name, k, version)
cmd.Infof("Creating release archive %v\n", file)
cmd.tarGzGhArtifacts(cmd.name, file, v...)
releaseArtifacts = append(releaseArtifacts, file)
}
}
releaseNotesFile := fmt.Sprintf("changelog-%v.md", version)
extractReleaseNotes("CHANGELOG.md", version, releaseNotesFile)
tagName := version
if cmd.isGoLang() {
tagName = "v" + version
}
releaseParams := []string{"release", "create", tagName, "-F", releaseNotesFile}
if cmd.preRelease {
releaseParams = append(releaseParams, "--prerelease")
}
for _, releaseArtifact := range releaseArtifacts {
cmd.Infof("Publishing %v\n", releaseArtifact)
releaseParams = append(releaseParams, releaseArtifact)
}
if !cmd.dryRun {
cmd.runCommand("Create GH Release and publish release artifacts", "gh", releaseParams...)
}
}
func newPublishToGithubCmd(root *RootCommand) *cobra.Command {
cobraCmd := &cobra.Command{
Use: "publish-to-github <name>",
Short: "Creates archives to be published",
Args: cobra.RangeArgs(0, 1),
}
result := &publishToGithubCmd{
BaseCommand: BaseCommand{
RootCommand: root,
Cmd: cobraCmd,
},
}
cobraCmd.Flags().StringVar(&result.archiveBase, "archive-base", "", "Directory to store release files in archives defaults to project name if not specified. May be set to blank.")
cobraCmd.Flags().BoolVarP(&result.preRelease, "prerelease", "p", false, "Publish as pre-release")
return Finalize(result)
}