Skip to content
Permalink
Browse files

add configuration

  • Loading branch information...
bobheadxi committed May 6, 2019
1 parent f966658 commit fd1314d84ff272b9bc37510aa43a4fb466ea4ad3
Showing with 129 additions and 34 deletions.
  1. +2 βˆ’0 .gitignore
  2. +10 βˆ’4 README.md
  3. +7 βˆ’0 config.go
  4. +43 βˆ’0 generate.go
  5. +1 βˆ’0 go.mod
  6. +2 βˆ’0 go.sum
  7. +60 βˆ’30 main.go
  8. +4 βˆ’0 twist.example.yml
@@ -1,2 +1,4 @@
./index.html
./twist.yml
x
vendor
@@ -1,9 +1,9 @@
# twist

Generate canonical imports for your Go packages. Since it does not require a running
server (ie in existing tools like [uber-go/sally](https://github.com/uber-go/sally)
Twist generates canonical imports for your Go packages. Since it does not require
a running server (ie in existing tools like [uber-go/sally](https://github.com/uber-go/sally)
and [rsc/go-import-redirector](https://github.com/rsc/go-import-redirector)),
this is particularly useful in conjunction with [GitHub Pages](https://pages.github.com/).
Twist is particularly useful in conjunction with [GitHub Pages](https://pages.github.com/).

A canonical import path allows you to make your package import a little fancier
with a custom domain, for example:
@@ -17,6 +17,12 @@ with a custom domain, for example:

```sh
go get github.com/bobheadxi/twist
# [ source ] [ canonical ]
# [ source ] [ canonical ]
twist github.com/bobheadxi/zapx bobheadxi.dev/zapx
```

Using the example in this repo:

```sh
twist -c twist.example.yml -o x
```
@@ -0,0 +1,7 @@
package main

type config struct {
Packages map[string]string
}

func newConfig() *config { return &config{Packages: make(map[string]string)} }
@@ -0,0 +1,43 @@
package main

import (
"fmt"
"html/template"
"os"
"path/filepath"
"strings"

"github.com/bobheadxi/twist/internal"
)

func generate(source, canonical string) {
b, err := internal.ReadFile("pkg.html")
if err != nil {
panic(err)
}
t, err := template.New("index.html").Parse(string(b))
if err != nil {
panic(err)
}

parts := strings.Split(canonical, "/")
packageName := parts[len(parts)-1]
target := filepath.Join(*outDir, packageName)
os.MkdirAll(target, os.ModePerm)
target = filepath.Join(target, "index.html")
fmt.Printf("generating template in '%s' (for '%s' => '%s')\n", target, source, canonical)
os.Remove(target)
f, err := os.OpenFile(target, os.O_CREATE|os.O_WRONLY, os.ModePerm)
if err != nil {
panic(err)
}

if err := t.Execute(f, pkg{
Source: source,
Canonical: canonical,
}); err != nil {
panic(err)
}
f.Sync()
f.Close()
}
1 go.mod
@@ -6,4 +6,5 @@ require (
github.com/UnnoTed/fileb0x v1.1.3
golang.org/x/lint v0.0.0-20190409202823-959b441ac422
golang.org/x/net v0.0.0-20190311183353-d8887717615a
gopkg.in/yaml.v2 v2.2.2
)
2 go.sum
@@ -59,3 +59,5 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.1 h1:mUhvW9EsL+naU5Q3cakzfE91YhliOondGd6ZrsDBHQE=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
90 main.go
@@ -3,15 +3,15 @@ package main
import (
"flag"
"fmt"
"html/template"
"io/ioutil"
"os"
"path/filepath"

"github.com/bobheadxi/twist/internal"
"gopkg.in/yaml.v2"
)

var (
outDir = flag.String("o", ".", "path to output directory")
outDir = flag.String("o", ".", "path to output directory")
cfgPath = flag.String("c", "./twist.yml", "path to Twist configuration")
)

type pkg struct {
@@ -22,34 +22,64 @@ type pkg struct {
//go:generate go run github.com/UnnoTed/fileb0x b0x.yml
func main() {
flag.Parse()
if len(flag.Args()) == 0 {
println("arguments [source] [canonical] required")
os.Exit(1)
}

b, err := internal.ReadFile("pkg.html")
if err != nil {
panic(err)
}
t, err := template.New("index.html").Parse(string(b))
if err != nil {
panic(err)
}
os.MkdirAll(*outDir, os.ModePerm)
target := filepath.Join(*outDir, "index.html")
fmt.Printf("generating template in '%s'\n", target)
os.Remove(target)
f, err := os.OpenFile(target, os.O_CREATE|os.O_WRONLY, os.ModePerm)
if err != nil {
panic(err)
// if only one argument, we are dealing with a command
if len(flag.Args()) == 1 {
switch arg := flag.Arg(0); arg {
case "help":
showHelp()
case "config":
b, err := yaml.Marshal(newConfig())
if err != nil {
panic(err)
}
if err := ioutil.WriteFile(*cfgPath, b, os.ModePerm); err != nil {
panic(err)
}
fmt.Printf("config generated in '%s'\n", *cfgPath)
default:
println("insufficient arguments provided")
os.Exit(1)
}
}

if err := t.Execute(f, pkg{
Source: flag.Arg(0),
Canonical: flag.Arg(1),
}); err != nil {
panic(err)
// otherwise generate
if *cfgPath == "" {
if len(flag.Args()) == 0 {
println("insufficient arguments provided")
os.Exit(1)
}
generate(flag.Arg(0), flag.Arg(1))
} else {
b, err := ioutil.ReadFile(*cfgPath)
if err != nil {
panic(err)
}
var cfg config
if err := yaml.Unmarshal(b, &cfg); err != nil {
panic(err)
}
for s, c := range cfg.Packages {
generate(s, c)
}
}
f.Sync()
f.Close()
}

func showHelp() {
println(`twist is a tool for generating static, serverless canonical imports for Go packages.
usage:
twist -c twist.yml
twist [source] [canonical]
other commands:
config generate a twist configuration file
help show help text
flags:
`)
flag.PrintDefaults()
println("\nsee https://github.com/bobheadxi/twist for more documentation.")
}
@@ -0,0 +1,4 @@
packages:
github.com/bobheadxi/zapx: bobheadxi.dev/x/zapx
github.com/bobheadxi/gobenchdata: bobheadxi.dev/x/gobenchdata
github.com/bobheadxi/res: bobheadxi.dev/x/res

0 comments on commit fd1314d

Please sign in to comment.
You can’t perform that action at this time.