diff options
-rw-r--r-- | .gitignore | 27 | ||||
-rw-r--r-- | Makefile | 10 | ||||
-rw-r--r-- | README.md | 1 | ||||
-rw-r--r-- | bindata.go | 260 | ||||
-rw-r--r-- | glide.lock | 6 | ||||
-rw-r--r-- | glide.yaml | 3 | ||||
-rw-r--r-- | main.go | 23 | ||||
-rw-r--r-- | parse.go | 34 | ||||
-rw-r--r-- | sally.yaml | 7 | ||||
-rw-r--r-- | templates/index.tpl | 10 | ||||
-rw-r--r-- | templates/package.tpl | 11 | ||||
-rw-r--r-- | write.go | 94 |
12 files changed, 462 insertions, 24 deletions
@@ -1,24 +1,3 @@ -# Compiled Object files, Static and Dynamic libs (Shared Objects) -*.o -*.a -*.so - -# Folders -_obj -_test - -# Architecture specific extensions/prefixes -*.[568vq] -[568vq].out - -*.cgo1.go -*.cgo2.c -_cgo_defun.c -_cgo_gotypes.go -_cgo_export.* - -_testmain.go - -*.exe -*.test -*.prof +vendor/ +out/ +sally diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..d548cd7 --- /dev/null +++ b/Makefile @@ -0,0 +1,10 @@ +.PHONY: install +install: + glide --version || go get github.com/Masterminds/glide + glide install + go get -u github.com/jteeuwen/go-bindata/... + + +.PHONY: run +run: + go generate && go build && ./sally @@ -1,2 +1,3 @@ # sally + A canonical import path static site generator for Go diff --git a/bindata.go b/bindata.go new file mode 100644 index 0000000..9e6c781 --- /dev/null +++ b/bindata.go @@ -0,0 +1,260 @@ +// Code generated by go-bindata. +// sources: +// templates/index.tpl +// templates/package.tpl +// DO NOT EDIT! + +package main + +import ( + "bytes" + "compress/gzip" + "fmt" + "io" + "io/ioutil" + "os" + "path/filepath" + "strings" + "time" +) + +func bindataRead(data []byte, name string) ([]byte, error) { + gz, err := gzip.NewReader(bytes.NewBuffer(data)) + if err != nil { + return nil, fmt.Errorf("Read %q: %v", name, err) + } + + var buf bytes.Buffer + _, err = io.Copy(&buf, gz) + clErr := gz.Close() + + if err != nil { + return nil, fmt.Errorf("Read %q: %v", name, err) + } + if clErr != nil { + return nil, err + } + + return buf.Bytes(), nil +} + +type asset struct { + bytes []byte + info os.FileInfo +} + +type bindataFileInfo struct { + name string + size int64 + mode os.FileMode + modTime time.Time +} + +func (fi bindataFileInfo) Name() string { + return fi.name +} +func (fi bindataFileInfo) Size() int64 { + return fi.size +} +func (fi bindataFileInfo) Mode() os.FileMode { + return fi.mode +} +func (fi bindataFileInfo) ModTime() time.Time { + return fi.modTime +} +func (fi bindataFileInfo) IsDir() bool { + return false +} +func (fi bindataFileInfo) Sys() interface{} { + return nil +} + +var _templatesIndexTpl = []byte("\x1f\x8b\x08\x00\x00\x09\x6e\x88\x00\xff\xb2\x51\x74\xf1\x77\x0e\x89\x0c\x70\x55\xc8\x28\xc9\xcd\xb1\xe3\xb2\x81\x50\x9c\x36\x49\xf9\x29\x95\x40\x9a\xd3\xa6\x14\xc4\xe5\xe4\xac\xae\x56\x28\x4a\xcc\x4b\x4f\x55\x50\xc9\x4e\xad\xd4\x51\x50\x29\x4b\xcc\x29\x4d\x55\xb0\xb2\x55\xd0\x0b\x48\x4c\xce\x4e\x4c\x4f\x2d\x56\xa8\xad\x05\x29\xe4\xb4\xc9\xc9\xb4\x03\xaa\x06\xa9\x03\x0a\x29\xe8\x2a\x80\x38\x60\xe5\x7a\x41\xa9\x05\xf9\x40\x31\x1b\x7d\xa0\x12\xa8\xa1\xa9\x79\x29\x10\x8d\x36\xfa\x60\x9b\x6c\xf4\x21\x36\xdb\xe8\x43\x5c\x02\x08\x00\x00\xff\xff\xb2\xe2\x86\x15\xa1\x00\x00\x00") + +func templatesIndexTplBytes() ([]byte, error) { + return bindataRead( + _templatesIndexTpl, + "templates/index.tpl", + ) +} + +func templatesIndexTpl() (*asset, error) { + bytes, err := templatesIndexTplBytes() + if err != nil { + return nil, err + } + + info := bindataFileInfo{name: "templates/index.tpl", size: 161, mode: os.FileMode(420), modTime: time.Unix(1470608150, 0)} + a := &asset{bytes: bytes, info: info} + return a, nil +} + +var _templatesPackageTpl = []byte("\x1f\x8b\x08\x00\x00\x09\x6e\x88\x00\xff\x8c\x91\xb1\x4e\xc3\x30\x10\x86\x77\x9e\xe2\x08\x73\x6b\x66\x70\xba\x14\xc4\x52\x41\x55\xc1\xc0\x68\x92\xbf\x89\x25\xdb\x57\xec\x4b\x25\x14\xe5\xdd\x71\x9a\x4a\x05\x51\x04\x5e\x6c\x9f\xad\xef\xfb\x75\xa7\x2f\xef\x9e\x96\xcf\xaf\xeb\x7b\x6a\xc5\xbb\xc5\x85\x9e\x36\xca\x4b\xb7\x30\xf5\x74\x3c\x5c\x3d\xc4\x50\x30\x1e\x65\xd1\xf0\xcc\xfa\x1d\x47\x29\xa8\xe2\x20\x08\x52\x16\x7d\x4f\xf3\xa5\x09\x1c\x6c\x65\xdc\xcb\x66\x45\xc3\x40\x8d\x95\xcc\x95\x5d\xba\x51\x6a\x7c\xdf\x60\xc7\xb9\x5e\xfc\x4a\x4d\xdc\xc5\x0a\x7f\x50\xcf\x10\xcf\xd5\x94\x44\x40\x79\x93\x04\xb1\x57\xb5\x8d\xff\xfc\xa5\xfa\xad\x75\x18\xae\x56\xbd\xb3\x01\x3f\xc3\x8e\x90\x19\xde\x3b\xbb\x2f\x8b\x88\x6d\x44\x6a\xbf\x04\xbe\xbe\xa5\x2e\xba\x72\x34\x3c\x70\xcd\xd5\x94\xf9\x08\xd1\xea\xd4\x53\xfd\xc6\xf5\xc7\x89\xfd\xc8\xd2\xda\xd0\x90\x30\x25\x80\x5a\x44\xcc\x69\xed\x60\x12\x48\x67\x69\x36\x4d\xdd\xf8\x46\xf5\xbc\x07\x19\xc7\xa1\xd1\xca\x2c\xe6\x47\xc9\x44\xce\xb2\xc3\x2c\x3f\x03\x00\x00\xff\xff\x69\xc7\x6b\xa3\xe3\x01\x00\x00") + +func templatesPackageTplBytes() ([]byte, error) { + return bindataRead( + _templatesPackageTpl, + "templates/package.tpl", + ) +} + +func templatesPackageTpl() (*asset, error) { + bytes, err := templatesPackageTplBytes() + if err != nil { + return nil, err + } + + info := bindataFileInfo{name: "templates/package.tpl", size: 483, mode: os.FileMode(420), modTime: time.Unix(1475273244, 0)} + a := &asset{bytes: bytes, info: info} + return a, nil +} + +// Asset loads and returns the asset for the given name. +// It returns an error if the asset could not be found or +// could not be loaded. +func Asset(name string) ([]byte, error) { + cannonicalName := strings.Replace(name, "\\", "/", -1) + if f, ok := _bindata[cannonicalName]; ok { + a, err := f() + if err != nil { + return nil, fmt.Errorf("Asset %s can't read by error: %v", name, err) + } + return a.bytes, nil + } + return nil, fmt.Errorf("Asset %s not found", name) +} + +// MustAsset is like Asset but panics when Asset would return an error. +// It simplifies safe initialization of global variables. +func MustAsset(name string) []byte { + a, err := Asset(name) + if err != nil { + panic("asset: Asset(" + name + "): " + err.Error()) + } + + return a +} + +// AssetInfo loads and returns the asset info for the given name. +// It returns an error if the asset could not be found or +// could not be loaded. +func AssetInfo(name string) (os.FileInfo, error) { + cannonicalName := strings.Replace(name, "\\", "/", -1) + if f, ok := _bindata[cannonicalName]; ok { + a, err := f() + if err != nil { + return nil, fmt.Errorf("AssetInfo %s can't read by error: %v", name, err) + } + return a.info, nil + } + return nil, fmt.Errorf("AssetInfo %s not found", name) +} + +// AssetNames returns the names of the assets. +func AssetNames() []string { + names := make([]string, 0, len(_bindata)) + for name := range _bindata { + names = append(names, name) + } + return names +} + +// _bindata is a table, holding each asset generator, mapped to its name. +var _bindata = map[string]func() (*asset, error){ + "templates/index.tpl": templatesIndexTpl, + "templates/package.tpl": templatesPackageTpl, +} + +// AssetDir returns the file names below a certain +// directory embedded in the file by go-bindata. +// For example if you run go-bindata on data/... and data contains the +// following hierarchy: +// data/ +// foo.txt +// img/ +// a.png +// b.png +// then AssetDir("data") would return []string{"foo.txt", "img"} +// AssetDir("data/img") would return []string{"a.png", "b.png"} +// AssetDir("foo.txt") and AssetDir("notexist") would return an error +// AssetDir("") will return []string{"data"}. +func AssetDir(name string) ([]string, error) { + node := _bintree + if len(name) != 0 { + cannonicalName := strings.Replace(name, "\\", "/", -1) + pathList := strings.Split(cannonicalName, "/") + for _, p := range pathList { + node = node.Children[p] + if node == nil { + return nil, fmt.Errorf("Asset %s not found", name) + } + } + } + if node.Func != nil { + return nil, fmt.Errorf("Asset %s not found", name) + } + rv := make([]string, 0, len(node.Children)) + for childName := range node.Children { + rv = append(rv, childName) + } + return rv, nil +} + +type bintree struct { + Func func() (*asset, error) + Children map[string]*bintree +} +var _bintree = &bintree{nil, map[string]*bintree{ + "templates": &bintree{nil, map[string]*bintree{ + "index.tpl": &bintree{templatesIndexTpl, map[string]*bintree{}}, + "package.tpl": &bintree{templatesPackageTpl, map[string]*bintree{}}, + }}, +}} + +// RestoreAsset restores an asset under the given directory +func RestoreAsset(dir, name string) error { + data, err := Asset(name) + if err != nil { + return err + } + info, err := AssetInfo(name) + if err != nil { + return err + } + err = os.MkdirAll(_filePath(dir, filepath.Dir(name)), os.FileMode(0755)) + if err != nil { + return err + } + err = ioutil.WriteFile(_filePath(dir, name), data, info.Mode()) + if err != nil { + return err + } + err = os.Chtimes(_filePath(dir, name), info.ModTime(), info.ModTime()) + if err != nil { + return err + } + return nil +} + +// RestoreAssets restores an asset under the given directory recursively +func RestoreAssets(dir, name string) error { + children, err := AssetDir(name) + // File + if err != nil { + return RestoreAsset(dir, name) + } + // Dir + for _, child := range children { + err = RestoreAssets(dir, filepath.Join(name, child)) + if err != nil { + return err + } + } + return nil +} + +func _filePath(dir, name string) string { + cannonicalName := strings.Replace(name, "\\", "/", -1) + return filepath.Join(append([]string{dir}, strings.Split(cannonicalName, "/")...)...) +} + diff --git a/glide.lock b/glide.lock new file mode 100644 index 0000000..66c8804 --- /dev/null +++ b/glide.lock @@ -0,0 +1,6 @@ +hash: 733b6e3d1ba3612c5d43aca4fe2243be4f39f09d609221d841d2eea1d8a060ad +updated: 2016-08-07T15:19:06.856947619-07:00 +imports: +- name: gopkg.in/yaml.v2 + version: e4d366fc3c7938e2958e662b4258c7a89e1f0e3e +testImports: [] diff --git a/glide.yaml b/glide.yaml new file mode 100644 index 0000000..3d8e188 --- /dev/null +++ b/glide.yaml @@ -0,0 +1,3 @@ +package: github.com/uber-go/sally +import: +- package: gopkg.in/yaml.v2 @@ -0,0 +1,23 @@ +package main + +import ( + "flag" + "log" +) + +//go:generate go-bindata templates/ + +func main() { + yml := flag.String("yml", "sally.yaml", "yaml file to read config from") + dir := flag.String("dir", "out", "directory to write html files to") + flag.Parse() + + c, err := Parse(*yml) + if err != nil { + log.Fatal(err) + } + + if err := Write(c, *dir); err != nil { + log.Fatal(err) + } +} diff --git a/parse.go b/parse.go new file mode 100644 index 0000000..5071f37 --- /dev/null +++ b/parse.go @@ -0,0 +1,34 @@ +package main + +import ( + "io/ioutil" + + "gopkg.in/yaml.v2" +) + +// Config represents the structure of the yaml file +type Config struct { + URL string `yaml:"url"` + Packages map[string]Package `yaml:"packages"` +} + +// Package details the options available for each repo +type Package struct { + Repo string `yaml:"repo"` +} + +// Parse takes a path to a yaml file and produces a parsed Config +func Parse(path string) (Config, error) { + var c Config + + data, err := ioutil.ReadFile(path) + if err != nil { + return c, err + } + + if err := yaml.Unmarshal(data, &c); err != nil { + return c, err + } + + return c, err +} diff --git a/sally.yaml b/sally.yaml new file mode 100644 index 0000000..ee8ff87 --- /dev/null +++ b/sally.yaml @@ -0,0 +1,7 @@ +url: go.uber.org + +packages: + thriftrw: + repo: github.com/thriftrw/thriftrw-go + yarpc: + repo: github.com/yarpc/yarpc-go diff --git a/templates/index.tpl b/templates/index.tpl new file mode 100644 index 0000000..bfe01fb --- /dev/null +++ b/templates/index.tpl @@ -0,0 +1,10 @@ +<!DOCTYPE html> +<html> + <body> + <ul> + {{ range $key, $value := .Packages }} + <li>{{ $key }} - {{ $value.Repo }}</li> + {{ end }} + </ul> + </body> +</html> diff --git a/templates/package.tpl b/templates/package.tpl new file mode 100644 index 0000000..38deaf3 --- /dev/null +++ b/templates/package.tpl @@ -0,0 +1,11 @@ +<!DOCTYPE html> +<html> + <head> + <meta name="go-import" content="{{ .CanonicalURL }} git https://{{ .Repo }}"> + <meta name="go-source" content="{{ .CanonicalURL }} https://{{ .Repo }} https://{{ .Repo }}/tree/master{/dir} https://{{ .Repo }}/tree/master{/dir}/{file}#L{line}"> + <meta http-equiv="refresh" content="0; url={{ .GodocURL }}"> + </head> + <body> + Nothing to see here. Please <a href="{{ .GodocURL }}">move along</a>. + </body> +</html> diff --git a/write.go b/write.go new file mode 100644 index 0000000..703b386 --- /dev/null +++ b/write.go @@ -0,0 +1,94 @@ +package main + +import ( + "bytes" + "fmt" + "html/template" + "io/ioutil" + "os" + "path/filepath" +) + +const ( + indexTplPath = "templates/index.tpl" + packagesTplPath = "templates/package.tpl" +) + +// Write takes a Config and produces a static html site to outDir +func Write(c Config, outDir string) error { + if err := os.MkdirAll(outDir, 0755); err != nil { + return err + } + if err := writeIndex(c, outDir); err != nil { + return err + } + if err := writePackages(c, outDir); err != nil { + return err + } + return nil +} + +func writeIndex(c Config, outDir string) error { + tpl, err := Asset(indexTplPath) + if err != nil { + return err + } + + t, err := template.New(filepath.Base(indexTplPath)).Parse(string(tpl)) + if err != nil { + return err + } + + buf := new(bytes.Buffer) + if err := t.Execute(buf, c); err != nil { + return err + } + + err = ioutil.WriteFile(fmt.Sprintf("%s/index.html", outDir), buf.Bytes(), 0644) + if err != nil { + return err + } + + fmt.Println(buf.String()) + return nil +} + +func writePackages(c Config, outDir string) error { + tpl, err := Asset(packagesTplPath) + if err != nil { + return err + } + + t, err := template.New(filepath.Base(packagesTplPath)).Parse(string(tpl)) + if err != nil { + return err + } + + for name, pkg := range c.Packages { + canonicalURL := fmt.Sprintf("%s/%s", c.URL, name) + tpl := struct { + Name string + CanonicalURL string + GodocURL string + Package + }{ + Name: name, + CanonicalURL: canonicalURL, + GodocURL: fmt.Sprintf("https://godoc.org/%s", canonicalURL), + Package: pkg, + } + + buf := new(bytes.Buffer) + if err := t.Execute(buf, tpl); err != nil { + return err + } + + if err := ioutil.WriteFile(fmt.Sprintf("%s/%s.html", outDir, name), buf.Bytes(), 0644); err != nil { + return err + } + + fmt.Println(buf) + } + + return nil +} |