-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
203 lines (170 loc) · 5.5 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
//go:generate go run -v ./generate_build_info.go
package main // import "github.com/JaredReisinger/cbp"
import (
"errors"
"fmt"
"html/template"
"log"
"math"
"net/http"
"net/url"
"os"
"strings"
"github.com/spf13/cobra"
)
var (
fullVersion string
rootCmd *cobra.Command
addr string
depth int
importPrefix string
vcs string
repoPrefix string
testingOnly = false
repoTemplate = template.Must(template.New("repoInfo").Parse(`
<html>
<head>
<title>{{ .ImportPath }} - cbp</title>
<meta name="go-import" content="{{ .ImportPath}} {{ .VCS }} {{ .RepoPath }}" />
</head>
<body>
<h1>{{ .ImportPath }}</h1>
<ul>
<li>VCS: {{ .VCS }}</li>
<li>Repo Root: {{ .RepoPath }}</li>
</ul>
</body>
</html>
`))
)
func init() {
fullVersion := fmt.Sprintf("%s, built: %s", gitVersion, buildDate)
rootCmd = &cobra.Command{
Use: "cbp [flags] repo-prefix",
Short: "Super-simple Golang remote import path service",
Long: "Super-simple Golang remote import path service. Complete documentation is available at https://github.com/JaredReisinger/cbp.",
Args: cobra.ExactArgs(1),
Version: fullVersion,
// Weird output formatting on this... is there a better way to do this?
Example: `
cbp https://github.com/your-name-or-org
The simplest possible example; if hosted at "http://go.your-name-or-org.com",
an import requests for "go.your-name-or-org.com/project" would resolve to
"https://github.com/your-name-or-org/project".`,
Run: run,
}
rootCmd.Flags().StringVarP(&addr, "addr", "a", "", "address/port on which to listen")
rootCmd.Flags().IntVarP(&depth, "depth", "d", 0, "number of path segments after the import prefix to the repository root (defaults to 2 minus any segments from the repo-prefix, but a minimum of 1)")
rootCmd.Flags().StringVarP(&importPrefix, "import-prefix", "i", "", "hostname and/or leading path for the custom import path; the 'Host' header of each incoming request is used by default")
rootCmd.Flags().StringVarP(&vcs, "vcs", "", "", "version control system (VCS) name for the repos (inferred from repo-prefix, or \"git\" if uncertain)")
}
func main() {
err := rootCmd.Execute()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func run(cmd *cobra.Command, args []string) {
repoPrefix = args[0]
err := normalizeArgs()
if err != nil {
log.Fatal(err)
}
// log all of the options in play:
if importPrefix == "" {
log.Print("using the Host header on incoming requests as the import prefix")
} else {
log.Printf("using %q as the import prefix", importPrefix)
}
log.Printf("using %d path segments from import for repository roots", depth)
log.Printf("using %q as the version control system", vcs)
log.Printf("using %q as the repository prefix for import requests", repoPrefix)
if addr == "" {
log.Print("using the default address:port (:http)")
} else {
log.Printf("using address:port %q", addr)
}
if !testingOnly {
log.Print("starting cbp...")
http.HandleFunc("/", serveMeta)
http.Handle("/favicon.ico", http.NotFoundHandler())
log.Fatal(http.ListenAndServe(addr, nil))
}
}
func normalizeArgs() (err error) {
if repoPrefix == "" {
err = errors.New("a repository root/prefix is required")
return
}
// parse the repo prefix to count paths...(?)
url, err := url.Parse(repoPrefix)
if err != nil {
return
}
if url.Opaque != "" {
err = fmt.Errorf("unexpected opaque data %q; missing %q after scheme %q?", url.Opaque, "//", url.Scheme)
return
}
if url.RawQuery != "" {
log.Printf("ignoring query %q in repo prefix", url.RawQuery)
url.RawQuery = ""
}
if url.Fragment != "" {
log.Printf("ignoring fragment %q in repo prefix", url.Fragment)
url.Fragment = ""
}
// we will be stripping leading "/" on incoming reqests, so we need to be
// sure that the repo prefix url is normalized to include a trailing one.
if !strings.HasSuffix(url.Path, "/") {
url.Path = fmt.Sprintf("%s/", url.Path)
}
// save the normalized prefix
repoPrefix = url.String()
// default the VCS if the user didn't specify
if vcs == "" {
scheme := strings.TrimSuffix(url.Scheme, "+ssh")
if scheme != "http" && scheme != "https" {
vcs = scheme
} else {
vcs = "git"
}
}
if depth <= 0 {
// we could do math on the path, but it's either 2 if the path is '/', or 1 otherwise.
if url.Path == "/" {
depth = 2
} else {
depth = 1
}
}
err = nil
return
}
func serveMeta(w http.ResponseWriter, r *http.Request) {
importPrefixAuto := r.Host
if importPrefix != "" {
importPrefixAuto = importPrefix
}
importPath, repoPath := calculatePaths(r.URL.Path, importPrefixAuto, repoPrefix)
repoTemplate.Execute(w, map[string]string{
"ImportPath": importPath,
"VCS": vcs,
"RepoPath": repoPath,
})
}
func calculatePaths(requestPath string, importPrefix string, repoPrefix string) (importPath string, repoPath string) {
pathPrefix := requestPath
// Remember that real requests always start with "/", so we ignore that
// character. Also, we split into 3 since we don't use anything past the
// second component.
parts := strings.SplitN(pathPrefix[1:], "/", 3)
// For zero, one, or two parts, the existing prefix (path) is good. For
// anything longer, we shorten it to just the first two (org/repo).
limit := int(math.Min(float64(len(parts)), 2))
pathPrefix = strings.Join(parts[:limit], "/")
importPath = fmt.Sprintf("%s%s", importPrefix, requestPath)
repoPath = fmt.Sprintf("%s%s", repoPrefix, pathPrefix)
log.Printf("%s => %s => %s", requestPath, importPath, repoPath)
return
}