Skip to content
Permalink
Browse files

add support for subpackages

  • Loading branch information...
bobheadxi committed May 11, 2019
1 parent d207170 commit 2ab6e9294cb61456f96e57304a06c490374f77b4
Showing with 59 additions and 30 deletions.
  1. +6 −0 Makefile
  2. +2 −2 README.md
  3. +14 −2 config.go
  4. +26 −20 generate.go
  5. +6 −3 main.go
  6. +5 −3 twist.example.yml
@@ -1,3 +1,9 @@
all:
go generate ./...
go install

example-config: all
twist config

example-redirects: all
twist -c twist.example.yml -o x -readme
@@ -17,8 +17,8 @@ with a custom domain, for example:

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

Using the example in this repo:
@@ -1,7 +1,19 @@
package main

type canonical struct {
Path string
Subpackages []string
}

type config struct {
Packages map[string]string
Packages map[string]canonical
}

func newConfig() *config { return &config{Packages: make(map[string]string)} }
func newConfig() *config {
return &config{Packages: map[string]canonical{
"github.com/my/package": canonical{
Path: "go.my.domain/package",
Subpackages: []string{"subpackage1", "subpackage2"},
},
}}
}
@@ -7,11 +7,11 @@ import (
"path/filepath"
"strings"

"go.bobheadxi.dev/twist/internal"
"github.com/olekukonko/tablewriter"
"go.bobheadxi.dev/twist/internal"
)

func generate(source, canonical string) {
func generate(source string, canon canonical) {
b, err := internal.ReadFile("pkg.html")
if err != nil {
panic(err)
@@ -21,26 +21,32 @@ func generate(source, canonical string) {
panic(err)
}

parts := strings.Split(canonical, "/")
parts := strings.Split(canon.Path, "/")
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)
}
for _, p := range append(canon.Subpackages, "") {
var (
path = filepath.Join(packageName, p)
pkgPath = filepath.Join(canon.Path, p)
target = filepath.Join(*outDir, path)
)
os.MkdirAll(target, os.ModePerm)
output := filepath.Join(target, "index.html")
fmt.Printf("generating template in '%s' (for '%s' => '%s')\n", output, source, pkgPath)
os.Remove(output)
f, err := os.OpenFile(output, 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)
if err := t.Execute(f, pkg{
Source: source,
Canonical: pkgPath,
}); err != nil {
panic(err)
}
f.Sync()
f.Close()
}
f.Sync()
f.Close()
}

func generateREADME(cfg *config) {
@@ -59,7 +65,7 @@ func generateREADME(cfg *config) {
table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
table.SetCenterSeparator("|")
for s, c := range cfg.Packages {
table.Append([]string{c, fmt.Sprintf("[%s](https://%s)", s, s)})
table.Append([]string{c.Path, fmt.Sprintf("[%s](https://%s)", s, s)})
}
table.Render()
f.WriteString("\n---\n")
@@ -11,7 +11,7 @@ import (

var (
outDir = flag.String("o", ".", "path to output directory")
cfgPath = flag.String("c", "./twist.yml", "path to Twist configuration")
cfgPath = flag.String("c", "", "path to Twist configuration")
renderREADME = flag.Bool("readme", false, "toggle README rendering - requires configuration")
)

@@ -30,6 +30,9 @@ func main() {
case "help":
showHelp()
case "config":
if *cfgPath == "" {
*cfgPath = "./twist.yml"
}
b, err := yaml.Marshal(newConfig())
if err != nil {
panic(err)
@@ -40,8 +43,8 @@ func main() {
fmt.Printf("config generated in '%s'\n", *cfgPath)
default:
println("insufficient arguments provided")
os.Exit(1)
}
os.Exit(1)
}

// otherwise generate
@@ -51,7 +54,7 @@ func main() {
println("insufficient arguments provided")
os.Exit(1)
}
generate(flag.Arg(0), flag.Arg(1))
generate(flag.Arg(0), canonical{Path: flag.Arg(1), Subpackages: flag.Args()[2:]})
} else {
b, err := ioutil.ReadFile(*cfgPath)
if err != nil {
@@ -1,4 +1,6 @@
packages:
github.com/bobheadxi/zapx: go.bobheadxi.dev/zapx
github.com/bobheadxi/gobenchdata: go.bobheadxi.dev/gobenchdata
github.com/bobheadxi/res: go.bobheadxi.dev/res
github.com/my/package:
path: go.my.domain/package
subpackages:
- subpackage1
- subpackage2

0 comments on commit 2ab6e92

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