|
@@ -8,6 +8,14 @@ import (
|
|
|
"os"
|
|
|
)
|
|
|
|
|
|
+const apacheTemplate = `
|
|
|
+{{}}
|
|
|
+{{ range . }}
|
|
|
+{{- -}}
|
|
|
+RewriteRule ^{{ .Clean }}$ {{ .Target }} [L,NC,NE,R=301]
|
|
|
+{{ end }}
|
|
|
+`
|
|
|
+
|
|
|
type redirect struct {
|
|
|
Clean, Target string
|
|
|
}
|
|
@@ -16,7 +24,7 @@ func main() {
|
|
|
var err error
|
|
|
|
|
|
if len(os.Args) != 4 {
|
|
|
- panic(fmt.Errorf(`
|
|
|
+ fmt.Fprintf(os.Stderr, `
|
|
|
Syntax:
|
|
|
%s source destination prefix
|
|
|
|
|
@@ -24,7 +32,8 @@ Where:
|
|
|
- source URL pairs in TSV format
|
|
|
- destination Apache .htaccess fragment
|
|
|
- prefix Prefix to strip from source URLs in pairs
|
|
|
-`, os.Args[0]))
|
|
|
+`, os.Args[0])
|
|
|
+ return
|
|
|
}
|
|
|
|
|
|
source := os.Args[1]
|
|
@@ -79,7 +88,7 @@ func read(tsv io.Reader, expectedPrefix string) ([]redirect, error) {
|
|
|
}
|
|
|
|
|
|
func write(w io.Writer, redirects []redirect) {
|
|
|
- tpl := template.Must(template.ParseFiles(`htaccess_rows.gohtml`))
|
|
|
+ tpl := template.Must(template.New("htaccess").Parse(apacheTemplate))
|
|
|
err := tpl.Execute(w, redirects)
|
|
|
if err != nil {
|
|
|
panic(err)
|