Skip to content

Commit

Permalink
Fix trailing slash support
Browse files Browse the repository at this point in the history
  • Loading branch information
gearnode committed May 3, 2024
1 parent 8306159 commit c9affb5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
24 changes: 20 additions & 4 deletions cmd/vanity/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"io"
"os"
"path"
"path/filepath"

"go.gearno.de/vanity"
"sigs.k8s.io/yaml"
Expand Down Expand Up @@ -111,6 +112,7 @@ func validateCfg(cfg *Cfg) error {
func main() {
cfgPath := flag.String("cfg", "", `the path of the configuration file (default "./vanity.yaml")`)
outputPath := flag.String("output", "", `the path where generated file will be written (default "./dist")`)
flatFileUrls := flag.Bool("flat-file-urls", false, "generates .html files directly, avoiding directory-style URLs and trailing slashes")
showHelp := flag.Bool("help", false, "shows this help message")
showVersion := flag.Bool("version", false, "prints the vanity cli version")

Expand Down Expand Up @@ -154,13 +156,27 @@ func main() {
vanityImport := vanity.NewImport(cfg.DomainName, importCfg.VCS, importCfg.RepoRoot, importCfg.ImportPrefix)
info("generating %s", vanityImport.ImportRoot())

dirname := path.Join(*outputPath, importCfg.ImportPrefix)
prefix := path.Join(*outputPath, importCfg.ImportPrefix)

var (
dirname = prefix
filename = "index.html"
)
if *flatFileUrls {
dirname = filepath.Dir(prefix)
filename = filepath.Base(prefix) + ".html"

}

err := os.MkdirAll(dirname, os.ModePerm)
if err != nil {
fail("cannot create %q directory: %v", dirname, err)
fail("cannot create %q directory: %v", prefix, err)
}

filename := path.Join(dirname, "index.html")
os.WriteFile(filename, []byte(vanityImport.HTMLPage()), 0644)
os.WriteFile(
path.Join(dirname, filename),
[]byte(vanityImport.HTMLPage()),
0644,
)
}
}
5 changes: 5 additions & 0 deletions doc/vanity.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ vanity import as a static website.
**-cfg** \<filename\>
: The path of the configuration file (default "vanity.yaml").

**-flat-file-urls**
: Generates .html files named after their respective paths, avoiding
directory-style URLs to prevent unwanted URL redirection, such as
the addition of a trailing slash that implies a directory structure.

**-version**
: Prints the vanity cli version.

Expand Down

0 comments on commit c9affb5

Please sign in to comment.