Bladeren bron

TSV to .htaccess for gobook.

Frederic G. MARAND 4 jaren geleden
bovenliggende
commit
ed371c81c4

+ 13 - 0
.idea/runConfigurations/TSV_2__htaccess.xml

@@ -0,0 +1,13 @@
+<component name="ProjectRunConfigurationManager">
+  <configuration default="false" name="TSV 2 .htaccess" type="GoApplicationRunConfiguration" factoryName="Go Application">
+    <module name="tooling" />
+    <working_directory value="$PROJECT_DIR$/tsv2htaccess" />
+    <parameters value="urls.tsv .htaccess https://osinet.fr/go/" />
+    <kind value="FILE" />
+    <filePath value="$PROJECT_DIR$/tsv2htaccess/main.go" />
+    <package value="code.osinet.fr/fgm/go_tooling" />
+    <directory value="$PROJECT_DIR$/" />
+    <output_directory value="$PROJECT_DIR$/tsv2htaccess/" />
+    <method v="2" />
+  </configuration>
+</component>

+ 3 - 0
tsv2htaccess/.gitignore

@@ -0,0 +1,3 @@
+tsv2htaccess
+*.htaccess
+*.tsv

+ 5 - 0
tsv2htaccess/go.mod

@@ -0,0 +1,5 @@
+module code.osinet.fr/fgm/go_tooling/tsv2htaccess
+
+go 1.13
+
+require github.com/davecgh/go-spew v1.1.1 // indirect

+ 2 - 0
tsv2htaccess/go.sum

@@ -0,0 +1,2 @@
+github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
+github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=

+ 5 - 0
tsv2htaccess/htaccess_rows.gohtml

@@ -0,0 +1,5 @@
+{{/* Do not edit: generated by tsv2htaccess*/}}
+{{ range . }}
+{{- /*gotype: code.osinet.fr/fgm/go_tooling/tsv2htaccess.redirect*/ -}}
+RewriteRule ^{{ .Clean }}$	{{ .Target }}	[L,NC,R=301]
+{{ end }}

+ 88 - 0
tsv2htaccess/main.go

@@ -0,0 +1,88 @@
+package main
+
+import (
+	"encoding/csv"
+	"fmt"
+	"html/template"
+	"io"
+	"os"
+)
+
+type redirect struct {
+	Clean, Target string
+}
+
+func main() {
+	var err error
+
+	if len(os.Args) != 4 {
+		panic(fmt.Errorf(`
+Syntax:
+	%s source destination prefix
+
+Where:
+- source		URL pairs in TSV format
+- destination	Apache .htaccess fragment 
+- prefix		Prefix to strip from source URLs in pairs
+`, os.Args[0]))
+	}
+
+	source := os.Args[1]
+	dest := os.Args[2]
+	prefix := os.Args[3]
+
+	fmt.Fprintf(os.Stderr, "Converting %s to %s, stripping %s\n", source, dest, prefix)
+	var tsv io.ReadCloser
+	tsv, err = os.Open(source)
+	if err != nil {
+		panic(err)
+	}
+	defer tsv.Close()
+
+	redirects, err := read(tsv, prefix)
+	if err != nil {
+		panic(err)
+	}
+
+	hta, err := os.Create(dest)
+	if err != nil {
+		panic(err)
+	}
+	defer hta.Close()
+
+	write(hta, redirects)
+}
+
+func read(tsv io.Reader, expectedPrefix string) ([]redirect, error) {
+	var err error
+	l := len(expectedPrefix)
+
+	r := csv.NewReader(tsv)
+	r.Comma = '\t'
+	r.FieldsPerRecord = 2
+	rows, err := r.ReadAll()
+	if err != nil {
+		return nil, err
+	}
+
+	redirects := make([]redirect, len(rows))
+	for i, rawRow := range rows {
+		absClean := rawRow[0]
+		actualPrefix := absClean[0:l]
+		if actualPrefix != expectedPrefix {
+			panic(fmt.Errorf("incorrect source prefix: [%s], should be [%s]", actualPrefix, expectedPrefix))
+		}
+		clean := absClean[l:]
+		redirects[i] = redirect{Clean: clean, Target: rawRow[1]}
+	}
+	return redirects, nil
+}
+
+func write(w io.Writer, redirects []redirect) {
+	tpl := template.Must(template.ParseFiles(`htaccess_rows.gohtml`))
+	err := tpl.Execute(w, redirects)
+	if err != nil {
+		panic(err)
+	}
+	tpl.Execute(os.Stderr, redirects)
+}