diff options
| author | Nicolas Paul <n@nc0.fr> | 2023-05-26 17:26:17 +0200 |
|---|---|---|
| committer | Nicolas Paul <n@nc0.fr> | 2023-05-26 17:26:17 +0200 |
| commit | bd426ee782d633174ce4907cb7a9d2919be79eda (patch) | |
| tree | 5c2f1758cc2e44696dad68520ebb5d82e0456f58 | |
| parent | e00e455cafb72edb4bddd7bc087e1fb0ff1e4e45 (diff) | |
Clean for v2
| -rw-r--r-- | .github/workflows/ci.yml | 41 | ||||
| -rw-r--r-- | config.go | 51 | ||||
| -rw-r--r-- | doc/example-files.png | bin | 216074 -> 0 bytes | |||
| -rw-r--r-- | examples/vanity.yaml | 16 | ||||
| -rw-r--r-- | go.mod | 2 | ||||
| -rw-r--r-- | go.sum | 4 | ||||
| -rw-r--r-- | main.go | 98 | ||||
| -rw-r--r-- | templates.go | 95 |
8 files changed, 0 insertions, 307 deletions
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml deleted file mode 100644 index 0e8a5a6..0000000 --- a/.github/workflows/ci.yml +++ /dev/null @@ -1,41 +0,0 @@ -# Copyright (c) 2023 Nicolas Paul All rights reserved. -# Use of this source code is governed by a BSD-style -# license that can be found in the LICENSE file. - -name: CI -on: - push: - branches: - - master - pull_request: - branches: - - master -jobs: - go: - name: Golang CI - runs-on: ${{ matrix.os }} - timeout-minutes: 5 - strategy: - fail-fast: true - matrix: - os: - - ubuntu-22.04 - - macos-12 - go_version: - - "1.19" - steps: - - uses: actions/checkout@v3 - name: Cloning repository - - uses: actions/setup-go@v4 - name: Installing Go ${{ matrix.go_version }} - with: - go-version: ${{ matrix.go_version }} - - name: Building project - run: go build -race -v -o=staticgovanityurls . - - name: Testing project - run: go test - - name: Downloading goimports - run: go install golang.org/x/tools/cmd/goimports@latest - - name: Formatting project - run: goimports -e -d -l . - # TODO: doc: diff --git a/config.go b/config.go deleted file mode 100644 index 505c5c3..0000000 --- a/config.go +++ /dev/null @@ -1,51 +0,0 @@ -// Copyright (c) 2023 Nicolas Paul All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package main - -// Config represents the content of the `vanity.yaml` configuration file. -// -// Hostname is the used domain, e.g. `go.example.com`. -// -// Paths describes the various import paths to generate. -type Config struct { - Hostname string `yaml:"hostname"` - Paths []Path `yaml:"paths"` -} - -// Path represents an import path. -// -// Repository is the repository URL as it would appear in `go-import` meta tags -// (https://golang.org/cmd/go/#hdr-Remote_import_paths). -// -// Vcs marks the version control system used on the defined repository. -// -// Dir is the URL template for a page listing the files in the package. -// Available substitutions: `{dir}`, `{/dir}`. -// -// File is the URL template for a link to a line in a source file. -// Available substitutions: `{file}`, `{line}`. -// -// Prefix represents the URL path in the total import path, without a -// leading `/`, e.g. `foo`. -// -// Read more about substitutions: https://github.com/golang/gddo/wiki/Source-Code-Links -type Path struct { - Dir string `yaml:"dir"` - File string `yaml:"file"` - Prefix string `yaml:"prefix"` - Repository string `yaml:"repository"` - Vcs VCS `yaml:"vcs"` -} - -// VCS represents a version control system for a Go repository. -type VCS string - -const ( - VcsBazaar VCS = "bzr" - VcsFossil VCS = "fossil" - VcsGit VCS = "git" - VcsMercurial VCS = "hg" - VcsSubversion VCS = "svn" -) diff --git a/doc/example-files.png b/doc/example-files.png Binary files differdeleted file mode 100644 index 2844df7..0000000 --- a/doc/example-files.png +++ /dev/null diff --git a/examples/vanity.yaml b/examples/vanity.yaml deleted file mode 100644 index 78d9dca..0000000 --- a/examples/vanity.yaml +++ /dev/null @@ -1,16 +0,0 @@ -# Copyright (c) 2023 Nicolas Paul All rights reserved. -# Use of this source code is governed by a BSD-style -# license that can be found in the LICENSE file. - -hostname: example.com -paths: - - prefix: foo - repository: https://github.com/example/foo.git - vcs: git - dir: https://github.com/example/foo/tree/master{/dir} - file: https://github.com/example/foo/blob/master{/dir}/{file}#L{line} - - prefix: bar - repository: https://svn.example.com/~baz/bar.svn - vcs: svn - dir: https://svn.example.com/~baz/bar.svn{/dir} - file: https://svn.example.com/~baz/bar.svn{/dir}/{file}#{line} @@ -1,5 +1,3 @@ module go.nc0.fr/staticgovanityurls go 1.19 - -require gopkg.in/yaml.v3 v3.0.1 @@ -1,4 +0,0 @@ -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= -gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go deleted file mode 100644 index 723cce7..0000000 --- a/main.go +++ /dev/null @@ -1,98 +0,0 @@ -// Copyright (c) 2023 Nicolas Paul All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package main - -import ( - "flag" - "fmt" - "log" - "os" - - "gopkg.in/yaml.v3" -) - -var ( - inputfile = flag.String("i", "", "Input configuration file.") - outputdir = flag.String("o", "dist", - "Place output files in the specified directory, by default it is `dist`.") -) - -func main() { - flag.Parse() - log.SetFlags(0) - log.SetPrefix("staticgovanityurls: ") - - if *inputfile == "" { - log.Fatalln("Missing input configuration file.") - } - - if *outputdir == "" { - log.Fatalln("Missing output directory.") - } - - // Read the input configuration file and parse it. - b, err := os.ReadFile(*inputfile) - if err != nil { - log.Fatalf("Failed to read file %s: %v\n", *inputfile, err) - } - - cfg := new(Config) - if err := yaml.Unmarshal(b, cfg); err != nil { - log.Fatalf("Failed to parse configuration file: %v\n", err) - } - - // Create output directory and files - // The index file ($OUTPUT/index.html) should contain an index of the - // modules listed in cfg.paths. - if err := os.Mkdir(*outputdir, 0777); err != nil { - log.Fatalf("Unable to create output directory %s: %v\n", *outputdir, err) - } - - // Create a list of all paths' prefixes with the hostname. - var pfs []string - for _, p := range cfg.Paths { - pfs = append(pfs, fmt.Sprintf("%s/%s", cfg.Hostname, p.Prefix)) - } - - // Create index file - idxpath := fmt.Sprintf("%s/index.html", *outputdir) - indexfl, err := os.Create(idxpath) - if err != nil { - log.Fatalf("Unable to create file %s: %v\n", idxpath, err) - } - defer func(fl *os.File) { - err := fl.Close() - if err != nil { - log.Fatalf("Cannot close file %s: %v\n", idxpath, err) - } - }(indexfl) - - if err := executeIndex(indexfl, cfg.Hostname, pfs); err != nil { - log.Fatalf("Cannot execute template: %v\n", err) - } - log.Printf("Generated %s\n", idxpath) - - // Create path files - for _, p := range cfg.Paths { - if err := os.Mkdir(fmt.Sprintf("%s/%s", *outputdir, p.Prefix), 0777); err != nil { - log.Fatalf("Unable to create directory %s/%s: %v\n", *outputdir, p.Prefix, err) - } - - ppath := fmt.Sprintf("%s/%s/index.html", *outputdir, p.Prefix) - pathfl, err := os.Create(ppath) - if err != nil { - log.Fatalf("Unable to create file %s: %v\n", ppath, err) - } - defer func(fl *os.File) { - err := fl.Close() - if err != nil { - log.Fatalf("Cannot close file %s: %v\n", ppath, err) - } - }(pathfl) - - executePath(pathfl, cfg.Hostname, p.Prefix, p.Vcs, p.Repository, p.Dir, p.File) - log.Printf("Generated %s\n", ppath) - } -} diff --git a/templates.go b/templates.go deleted file mode 100644 index b6b274b..0000000 --- a/templates.go +++ /dev/null @@ -1,95 +0,0 @@ -// Copyright (c) 2023 Nicolas Paul All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package main - -import ( - "fmt" - "html/template" - "io" -) - -// indextmpl is the HTML template to generate for the index page of the static -// site (route "/"). -var indextmpl = template.Must( - template.New("index").Parse(`<!DOCTYPE html> -<html lang="en"> - <head> - <meta charset="UTF-8"> - <meta http-equiv="X-UA-Compatible" content="IE=edge"> - <meta http-equiv="content-type" content="text/html" charset="UTF-8"> - <title>{{.Hostname}}</title> - <meta name="generator" content="staticgovanityurls (https://staticgovanityurls.nc0.fr)"> - <meta name="viewport" content="width=device-width,height=device-height,initial-scale=1.0,user-scalable=yes"> - </head> - <body> - <h1>{{.Hostname}}</h1> - <ul> - {{range .Paths}}<li><a href="https://{{.}}">{{.}}</a></li> - {{end}} - </ul> - </body> -</html>`), -) - -// executeIndex generates the Index template using the given variables. -// paths is a list of import path (containing both hostname and prefix). -func executeIndex(o io.Writer, hostname string, paths []string) error { - return indextmpl.Execute(o, struct { - Hostname string - Paths []string - }{ - Hostname: hostname, - Paths: paths, - }) -} - -// pathtmpl is the HTML template to generate for the page of a module. -var pathtmpl = template.Must( - template.New("path").Parse(`<!DOCTYPE html> - <html lang="en"> - <head> - <meta charset="UTF-8"> - <meta http-equiv="X-UA-Compatible" content="IE=edge"> - <meta http-equiv="content-type" content="text/html" charset="UTF-8"> - <meta name="generator" content="staticgovanityurls (https://staticgovanityurls.nc0.fr)"> - <meta name="viewport" content="width=device-width,height=device-height,initial-scale=1.0,user-scalable=yes"> - <meta name="go-import" content="{{.Prefix}} {{.Vcs}} {{.Repo}}"> - <meta name="go-source" content="{{.Prefix}} {{.Repo}} {{.Dir}} {{.File}}"> - <title>{{.Prefix}}</title> - </head> - <body> - <h1>{{.Prefix}}</h1> - <ul> - <li><a href="https://pkg.go.dev/{{.Prefix}}">Documentation</a></li> - <li><a href="{{.Repo}}">Source ({{.Vcs}})</a></li> - </ul> - </body> - </html>`), -) - -// executePath generates the path template using the given variables. -func executePath( - o io.Writer, - hostname string, - prefix string, - vcs VCS, - repo string, - dir string, - file string, -) error { - return pathtmpl.Execute(o, struct { - Prefix string - Repo string - Dir string - File string - Vcs VCS - }{ - Prefix: fmt.Sprintf("%s/%s", hostname, prefix), - Repo: repo, - Vcs: vcs, - Dir: dir, - File: file, - }) -} |
