Migrated fftr site-config to go:embed

This commit is contained in:
Olivier Meunier
2021-03-12 00:03:32 +01:00
parent cb5936d4c5
commit 7fbf851c85
8 changed files with 31 additions and 65 deletions

View File

@@ -1,7 +1,7 @@
#!/usr/bin/make
TAGS := omit_load_extension foreign_keys json1 fts5 secure_delete
BUILD_TAGS := assets $(TAGS)
BUILD_TAGS := $(TAGS)
SITECONFIG_GIT=https://github.com/j0k3r/graby-site-config.git
SITECONFIG=graby-site-config
@@ -13,7 +13,6 @@ all: web-build build
# Build the server
.PHONY: build
build:
go generate
go build -tags "$(BUILD_TAGS)" -ldflags="-s -w" -o dist/readeck
# Build the server in dev mode, without compiling the assets
@@ -27,7 +26,6 @@ clean:
rm -rf dist
rm -rf assets/www/*
rm -f assets/templates/base.gohtml
rm -f pkg/extract/fftr/siteconfig_vfsdata.go
go clean
# Launch the documentation

View File

@@ -3,7 +3,6 @@ package app
import (
"errors"
"fmt"
"net/http"
"os"
"os/signal"
"path"
@@ -193,9 +192,8 @@ func addSiteConfig(name, src string) {
}
f := &fftr.ConfigFolder{
FileSystem: http.Dir(src),
FS: os.DirFS(src),
Name: name,
Dir: "./",
}
fftr.DefaultConfigurationFolders = append(fftr.ConfigFolderList{f}, fftr.DefaultConfigurationFolders...)

View File

@@ -2,6 +2,7 @@ package cookbook
import (
"fmt"
"io/fs"
"net/http"
"path"
"runtime"
@@ -123,13 +124,8 @@ func (api *cookbookAPI) extract(w http.ResponseWriter, r *http.Request) {
func (api *cookbookAPI) loadURLs() {
api.urls = map[string][]string{}
for i, configRoot := range fftr.DefaultConfigurationFolders {
root, err := configRoot.Open("/")
if err != nil {
panic(err)
}
defer root.Close()
files, err := root.Readdir(-1)
for i, configFS := range fftr.DefaultConfigurationFolders {
files, err := fs.ReadDir(configFS, ".")
if err != nil {
panic(err)
}
@@ -139,7 +135,7 @@ func (api *cookbookAPI) loadURLs() {
if x.IsDir() || path.Ext(x.Name()) != ".toml" {
continue
}
f, err := configRoot.Open(x.Name())
f, err := configFS.Open(x.Name())
if err != nil {
panic(err)
}
@@ -150,7 +146,7 @@ func (api *cookbookAPI) loadURLs() {
f.Close()
if cfg != nil && len(cfg.Tests) > 0 {
name := fmt.Sprintf("%d - %s - %s", i, configRoot.Name,
name := fmt.Sprintf("%d - %s - %s", i, configFS.Name,
path.Base(x.Name()))
api.urls[name] = make([]string, len(cfg.Tests))
for i := range cfg.Tests {

View File

@@ -7,8 +7,6 @@ import (
"github.com/readeck/readeck/internal/app"
)
//go:generate go run -tags=!build tools/generate_assets.go
func main() {
if err := app.Run(); err != nil {
fmt.Printf("ERROR: %s\n", err)

View File

@@ -3,7 +3,7 @@ package fftr
import (
"fmt"
"io"
"net/http"
"io/fs"
"net/url"
"path"
"strings"
@@ -14,9 +14,8 @@ import (
// ConfigFolder is an http.FileSystem with a name.
type ConfigFolder struct {
http.FileSystem
fs.FS
Name string
Dir string
}
// ConfigFolderList is a list of configuration folders.
@@ -25,8 +24,8 @@ type ConfigFolderList []*ConfigFolder
// DefaultConfigurationFolders is a list of default locations with
// configuration files.
var DefaultConfigurationFolders ConfigFolderList = ConfigFolderList{
{SiteConfigFolder, "custom", "custom"},
{SiteConfigFolder, "standard", "standard"},
{siteConfigFS("custom"), "custom"},
{siteConfigFS("standard"), "standard"},
}
// Config holds the fivefilters configuration.
@@ -128,19 +127,11 @@ func (cf *Config) Merge(new *Config) {
}
}
// Open opens a file path with the given "Dir" prefix.
func (cf *ConfigFolder) Open(name string) (http.File, error) {
return cf.FileSystem.Open(path.Join(cf.Dir, name))
}
func (cf *ConfigFolder) fileExists(name string) bool {
f, err := cf.Open(name)
s, err := fs.Stat(cf.FS, name)
if err != nil {
return false
}
defer f.Close()
s, _ := f.Stat()
return !s.IsDir()
}

View File

@@ -1,8 +1,8 @@
package fftr
import (
"net/http"
"net/url"
"os"
"testing"
"github.com/stretchr/testify/assert"
@@ -10,9 +10,9 @@ import (
func TestFftr(t *testing.T) {
folders := ConfigFolderList{
{http.Dir("./"), "fixtures", "test-fixtures"},
{http.Dir("../../../site-config"), "custom", "custom"},
{http.Dir("../../../site-config"), "standard", "standard"},
{os.DirFS("./test-fixtures"), "fixtures"},
{os.DirFS("./site-config/custom"), "custom"},
{os.DirFS("./site-config/standard"), "standard"},
}
t.Run("merge", func(t *testing.T) {

View File

@@ -1,11 +1,19 @@
// +build !assets
package fftr
import (
"net/http"
"embed"
"fmt"
"io/fs"
)
// SiteConfigFolder is the configuration folder with
// site config files.
var SiteConfigFolder = http.Dir("site-config")
//go:embed site-config site-config/**/*
var assets embed.FS
// siteConfigFS returns the given site-config subfolder as an fs.FS instance.
func siteConfigFS(name string) fs.FS {
sub, err := fs.Sub(assets, fmt.Sprintf("site-config/%s", name))
if err != nil {
panic(err)
}
return sub
}

View File

@@ -1,23 +0,0 @@
// +build ignore
package main
import (
"log"
"github.com/shurcooL/vfsgen"
"github.com/readeck/readeck/pkg/extract/fftr"
)
func main() {
var err error
if err = vfsgen.Generate(fftr.SiteConfigFolder, vfsgen.Options{
Filename: "pkg/extract/fftr/siteconfig_vfsdata.go",
PackageName: "fftr",
BuildTags: "assets",
VariableName: "SiteConfigFolder",
}); err != nil {
log.Fatalln(err)
}
}