Skip to content

Commit

Permalink
feat: set vcs via config file
Browse files Browse the repository at this point in the history
  • Loading branch information
mlcdf committed Mar 8, 2022
1 parent 6157057 commit a73cb7a
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 20 deletions.
31 changes: 19 additions & 12 deletions build.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@ import (
"strings"
)

type vanityTemplateData struct {
Root string
VCS string
RepoURL string
}

// Build the pages
func build(config *Config) error {

t, err := template.New("foo").Parse(`{{define "T"}}` + config.RepoTemplate + "{{end}}")
t, err := template.New("").Parse(`{{define "T"}}` + config.RepoTemplate + "{{end}}")
if err != nil {
return err
}
Expand All @@ -22,22 +27,24 @@ func build(config *Config) error {
return err
}

for _path, repo := range config.Repos {
os.MkdirAll(path.Join(wd, config.Output, _path), os.ModePerm)
for root, repo := range config.Repos {
os.MkdirAll(path.Join(wd, config.Output, root), os.ModePerm)

dest := path.Join(wd, config.Output, _path, "index.html")
log.Printf("%s %s\n", info("Building ["+_path+"]"), dest)
dest := path.Join(wd, config.Output, root, "index.html")
log.Printf("%s %s\n", info("Building ["+root+"]"), dest)

f, err := os.OpenFile(dest, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return err
}

err = t.ExecuteTemplate(f, "T", struct {
Path string
Repo Repository
Config Config
}{_path, repo, *config})
data := vanityTemplateData{
Root: path.Join(config.Domain, root),
VCS: repo.VCS,
RepoURL: repo.URL,
}

err = t.ExecuteTemplate(f, "T", data)

if err != nil {
return err
Expand All @@ -57,7 +64,7 @@ func buildIndex(config *Config) error {
return strings.Replace(input, from, to, -1)
}}

t, err := template.New("foo").Funcs(funcMap).Parse(`{{define "T"}}` + config.IndexTemplate + "{{end}}")
t, err := template.New("").Funcs(funcMap).Parse(`{{define "T"}}` + config.IndexTemplate + "{{end}}")
if err != nil {
return err
}
Expand Down
9 changes: 7 additions & 2 deletions config.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package main

import (
"errors"
"fmt"
"log"
"os"

_ "embed"

"github.com/pelletier/go-toml"
"github.com/pkg/errors"
)

const defaultConfigName = ".vanity-imports.toml"
Expand Down Expand Up @@ -50,6 +50,7 @@ type Index struct {

type Repository struct {
URL string `toml:"repo"`
VCS string `vcs:"repo"`
}

func (r Repository) String() string {
Expand All @@ -59,7 +60,7 @@ func (r Repository) String() string {
func newConfig(path string) (*Config, error) {
data, err := os.ReadFile(path)
if err != nil {
return nil, err
return nil, errors.Wrap(err, "cannot open config file")
}

config := &Config{}
Expand Down Expand Up @@ -93,6 +94,10 @@ func (c Config) isValid() error {
if repo.URL == "" {
return fmt.Errorf("repo.%s.repo is empty or missing in config", path)
}

if repo.VCS == "" {
return fmt.Errorf("repo.%s.vcs is empty or missing in config", path)
}
}

return nil
Expand Down
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ module go.mlcdf.fr/vanity-imports

go 1.16

require github.com/pelletier/go-toml v1.9.0
require (
github.com/pelletier/go-toml v1.9.0
github.com/pkg/errors v0.9.1
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
github.com/pelletier/go-toml v1.9.0 h1:NOd0BRdOKpPf0SxkL3HxSQOG7rNh+4kl6PHcBPFs7Q0=
github.com/pelletier/go-toml v1.9.0/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
10 changes: 5 additions & 5 deletions templates/repo.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="go-import" content="{{ .Config.Domain }}{{ .Path }} git {{ .Repo }}.git">
<meta name="go-import" content="{{ .Root }} {{ .VCS }} {{ .RepoURL }}">
<meta name="go-source"
content="{{ .Config.Domain }}{{ .Path }} _ {{ .Repo }}/tree/master{/dir} {{ .Repo.URL }}blob/master{/dir}/{file}#L{line}">
<meta http-equiv="refresh" content="0; url=https://pkg.go.dev/{{ .Config.Domain }}{{ .Path }}">
<title>{{ .Config.Domain }}{{ .Path }}</title>
content="{{ .Root }} _ {{ .RepoURL }}/tree/main{/dir} {{ .RepoURL }}blob/main{/dir}/{file}#L{line}">
<meta http-equiv="refresh" content="0; url=https://pkg.go.dev/{{ .Root }}">
<title>{{ .Root }}</title>
</head>

<body>
Nothing to see here; <a href='https://pkg.go.dev/{{ .Config.Domain }}{{ .Path }}'>move along</a>.
Nothing to see here; <a href='https://pkg.go.dev/{{ .Root }}'>move along</a>.
</body>

</html>

0 comments on commit a73cb7a

Please sign in to comment.