This repository has been archived by the owner on Jan 14, 2022. It is now read-only.
/
main.go
211 lines (186 loc) · 5.6 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package main
import (
"bytes"
"fmt"
"log"
"net/http"
"path"
"strings"
"text/template"
"time"
"github.com/goreleaser/goreleaser/config"
"github.com/goreleaser/goreleaser/context"
"github.com/goreleaser/goreleaser/pipeline/defaults"
"gopkg.in/alecthomas/kingpin.v2"
)
// given a template, and a config, generate shell script
func makeShell(tplsrc string, cfg *config.Project) (string, error) {
// if we want to add a timestamp in the templates this
// function will generate it
funcMap := template.FuncMap{
"timestamp": func() string {
return time.Now().UTC().Format(time.RFC3339)
},
}
var out bytes.Buffer
t, err := template.New("shell").Funcs(funcMap).Parse(tplsrc)
if err != nil {
return "", err
}
err = t.Execute(&out, cfg)
return out.String(), err
}
// converts the given name template to it's equivalent in shell
// except for the default goreleaser templates, templates with
// conditionals will return an error
//
// {{ .Binary }} ---> [prefix]${BINARY}, etc.
//
func makeName(prefix, target string) (string, error) {
// armv6 is the default in the shell script
// so do not need special template condition for ARM
armversion := "{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}"
target = strings.Replace(target, armversion, "{{ .Arch }}", -1)
// otherwise if it contains a conditional, we can't (easily)
// translate that to bash. Ask for bug report.
if strings.Contains(target, "{{ if") || strings.Contains(target, "{{if") || strings.Contains(target, "{{ .Arm") || strings.Contains(target, "{{.Arm") {
return "", fmt.Errorf("name_template %q contains unknown conditional or ARM format. Please file bug at https://github.com/goreleaser/godownloader", target)
}
var varmap = map[string]string{
"Os": "${OS}",
"Arch": "${ARCH}",
"Version": "${VERSION}",
"Tag": "${TAG}",
"Binary": "${BINARY}",
"ProjectName": "${PROJECT_NAME}",
}
var out bytes.Buffer
out.WriteString(prefix)
t, err := template.New("name").Parse(target)
if err != nil {
return "", err
}
err = t.Execute(&out, varmap)
return out.String(), err
}
// returns the owner/name repo from input
//
// see https://github.com/goreleaser/godownloader/issues/55
func normalizeRepo(repo string) string {
// handle full or partial URLs
repo = strings.TrimPrefix(repo, "https://github.com/")
repo = strings.TrimPrefix(repo, "http://github.com/")
repo = strings.TrimPrefix(repo, "github.com/")
// hande /name/repo or name/repo/ cases
repo = strings.Trim(repo, "/")
return repo
}
func loadURLs(path string) (*config.Project, error) {
for _, file := range []string{"goreleaser.yml", ".goreleaser.yml", "goreleaser.yaml", ".goreleaser.yaml"} {
var url = fmt.Sprintf("%s/%s", path, file)
log.Printf("reading %s", url)
project, err := loadURL(url)
if err != nil {
return nil, err
}
if project != nil {
return project, nil
}
}
return nil, fmt.Errorf("could not fetch a goreleaser configuration file")
}
func loadURL(file string) (*config.Project, error) {
resp, err := http.Get(file)
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
log.Printf("reading %s returned %d %s\n", file, resp.StatusCode, http.StatusText(resp.StatusCode))
return nil, nil
}
p, err := config.LoadReader(resp.Body)
// to make errcheck happy
errc := resp.Body.Close()
if errc != nil {
return nil, errc
}
return &p, err
}
func loadFile(file string) (*config.Project, error) {
p, err := config.Load(file)
return &p, err
}
// Load project configuration from a given repo name or filepath/url.
func Load(repo string, file string) (project *config.Project, err error) {
if repo == "" && file == "" {
return nil, fmt.Errorf("repo or file not specified")
}
if file == "" {
repo = normalizeRepo(repo)
log.Printf("reading repo %q on github", repo)
project, err = loadURLs(
fmt.Sprintf("https://raw.githubusercontent.com/%s/master", repo),
)
} else {
log.Printf("reading file %q", file)
project, err = loadFile(file)
}
if err != nil {
return nil, err
}
// if not specified add in GitHub owner/repo info
if project.Release.GitHub.Owner == "" {
if repo == "" {
return nil, fmt.Errorf("owner/name repo not specified")
}
project.Release.GitHub.Owner = path.Dir(repo)
project.Release.GitHub.Name = path.Base(repo)
}
var ctx = context.New(*project)
err = defaults.Pipe{}.Run(ctx)
project = &ctx.Config
// set default binary name
if len(project.Builds) == 0 {
project.Builds = []config.Build{
{Binary: path.Base(repo)},
}
}
if project.Builds[0].Binary == "" {
project.Builds[0].Binary = path.Base(repo)
}
return project, err
}
func main() {
var (
repo = kingpin.Flag("repo", "owner/name or URL of GitHub repository").Required().String()
source = kingpin.Flag("source", "source type [godownloader|raw|equinoxio]").Default("godownloader").String()
exe = kingpin.Flag("exe", "name of binary, used only in raw").String()
nametpl = kingpin.Flag("nametpl", "name template, used only in raw").String()
file = kingpin.Arg("file", "??").String()
)
kingpin.Parse()
var (
out string
err error
)
switch *source {
case "godownloader":
// https://github.com/goreleaser/godownloader
out, err = processGodownloader(*repo, *file)
case "equinoxio":
// https://equinox.io
out, err = processEquinoxio(*repo)
case "raw":
// raw mode is when people upload direct binaries
// to GitHub releases that are not not tar'ed or zip'ed.
// For example:
// https://github.com/mvdan/sh/releases
out, err = processRaw(*repo, *exe, *nametpl)
default:
log.Fatalf("unknown source %q", *source)
}
if err != nil {
log.Fatalf("failed: %s", err)
}
fmt.Print(out)
}