From d2a4ff166e03fec7e157d4adcf1933f030c66e2d Mon Sep 17 00:00:00 2001 From: Benjamin Lowry Date: Mon, 11 Oct 2021 17:57:23 -0500 Subject: [PATCH] add support for branch name specification --- main.go | 6 ++++-- vanityserver/vanityserver.go | 8 +++++--- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/main.go b/main.go index 5cf4d20..3940b5a 100644 --- a/main.go +++ b/main.go @@ -20,16 +20,18 @@ import ( func main() { var port string var user string + var branch string getopt.StringVar(&port, "p", "0.0.0.0:8080", "address and port to bind to") getopt.StringVar(&user, "u", "", "username to redirect to") + getopt.StringVar(&branch, "b", "master", "branch to redirect to") if e := getopt.Parse(); e != nil { panic(e) } if user == "" || port == "" { - fmt.Fprintf(os.Stderr, "usage: sourcehut-vanity -u username [-p 0.0.0.0:8080]\n") + fmt.Fprintf(os.Stderr, "usage: sourcehut-vanity -u username -b branch [-p 0.0.0.0:8080]\n") getopt.PrintDefaults() os.Exit(1) } fmt.Printf("Serving %v's projects on %v\n", user, port) - fmt.Println(http.ListenAndServe(port, vanityserver.VanityServer{Username: user})) + fmt.Println(http.ListenAndServe(port, vanityserver.VanityServer{Username: user, Branch: branch})) } diff --git a/vanityserver/vanityserver.go b/vanityserver/vanityserver.go index bb9bb1e..3d1e4d0 100644 --- a/vanityserver/vanityserver.go +++ b/vanityserver/vanityserver.go @@ -21,6 +21,7 @@ import ( type VanityServer struct { Username string + Branch string } type templatedata struct { @@ -28,6 +29,7 @@ type templatedata struct { ImportHost string ProjectName string Redir string + Branch string } const page = ` @@ -35,7 +37,7 @@ const page = ` - + @@ -53,12 +55,12 @@ func (server VanityServer) ServeHTTP(rw http.ResponseWriter, r *http.Request) { r.ParseForm() if len(path) > 2 && path[2] != "" { - destination = baseurl + "/tree/master/" + strings.Replace(r.URL.Path, "/"+path[1]+"/", "", 1) + destination = baseurl + "/tree/" + server.Branch + "/" + strings.Replace(r.URL.Path, "/"+path[1]+"/", "", 1) } else { destination = baseurl } if r.Form.Get("go-get") == "1" { // go-get(1) - tmpl.Execute(rw, templatedata{User: server.Username, ImportHost: r.Host + "/" + projectname, ProjectName: projectname, Redir: destination}) + tmpl.Execute(rw, templatedata{User: server.Username, Branch: server.Branch, ImportHost: r.Host + "/" + projectname, ProjectName: projectname, Redir: destination}) } else { // Standard browser http.Redirect(rw, r, destination, 302) } -- 2.45.2