Skip to content

Commit

Permalink
add cli cmd:public which collects html template files into single pub…
Browse files Browse the repository at this point in the history
…lic directory while preserving the path. read template files embedded binary set by fir.WithEmbedFS
  • Loading branch information
adnaan committed Sep 4, 2022
1 parent 92ac691 commit e216a0c
Show file tree
Hide file tree
Showing 4 changed files with 266 additions and 113 deletions.
53 changes: 53 additions & 0 deletions cli/cmd/public.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
"fmt"
"os"

"github.com/adnaan/fir"
"github.com/spf13/cobra"
)

var (
inDir string
outDir string
extensions []string
)

// publicCmd represents the public command
var publicCmd = &cobra.Command{
Use: "public",
Short: "Generates the public folder containing the html files",
Long: `The public command generates a public folder containing the html files in the project.
It preserves the paths of the html files enabling a flexible project structure. The generated public directory
can be embedded in the binary as is.`,
Run: func(cmd *cobra.Command, args []string) {
var opts []fir.PublicOption
if inDir != "" {
opts = append(opts, fir.InDir(inDir))
}

if outDir != "" {
opts = append(opts, fir.OutDir(outDir))
}

if len(extensions) != 0 {
opts = append(opts, fir.Extensions(extensions))
}

if err := fir.GeneratePublic(opts...); err != nil {
fmt.Println(err)
os.Exit(1)
}
},
}

func init() {
rootCmd.AddCommand(publicCmd)
publicCmd.Flags().StringVarP(&inDir, "in", "i", "", "path to input directory which contains the html template files")
publicCmd.Flags().StringVarP(&outDir, "out", "o", "", "path to output directory")
publicCmd.Flags().StringSliceVarP(&extensions, "extensions", "x", nil, "comma separated list of template exatensions e.g. .html,.tmpl")
}
20 changes: 18 additions & 2 deletions controller.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fir

import (
"embed"
"flag"
"log"
"net/http"
Expand Down Expand Up @@ -30,6 +31,8 @@ type controlOpt struct {
developmentMode bool
errorView View
cookieStore *sessions.CookieStore
embedFS embed.FS
hasEmbedFS bool
}

type Option func(*controlOpt)
Expand Down Expand Up @@ -58,6 +61,13 @@ func WithCookieStore(cookieStore *sessions.CookieStore) Option {
}
}

func WithEmbedFS(fs embed.FS) Option {
return func(o *controlOpt) {
o.embedFS = fs
o.hasEmbedFS = true
}
}

func DisableTemplateCache() Option {
return func(o *controlOpt) {
o.disableTemplateCache = true
Expand Down Expand Up @@ -143,6 +153,12 @@ func NewController(name string, options ...Option) Controller {
if wc.enableWatch {
go watchTemplates(wc)
}

if wc.hasEmbedFS {
log.Println("read template files embedded in the binary")
} else {
log.Println("read template files from disk")
}
return wc
}

Expand Down Expand Up @@ -302,12 +318,12 @@ func (wc *websocketController) getUser(w http.ResponseWriter, r *http.Request) (
}

func (wc *websocketController) Handler(view View) http.HandlerFunc {
viewTemplate, err := parseTemplate(wc.publicDir, view)
viewTemplate, err := parseTemplate(wc.controlOpt, view)
if err != nil {
panic(err)
}

errorViewTemplate, err := parseTemplate(wc.publicDir, wc.errorView)
errorViewTemplate, err := parseTemplate(wc.controlOpt, wc.errorView)
if err != nil {
panic(err)
}
Expand Down
28 changes: 17 additions & 11 deletions public.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,24 @@ package fir

import (
"io/fs"
"log"
"os"
"path/filepath"

gitignore "github.com/sabhiram/go-gitignore"
)

type publicOpt struct {
inputDir string
inDir string
outDir string
extensions []string
}

type PublicOption func(*publicOpt)

func InputDir(path string) PublicOption {
func InDir(path string) PublicOption {
return func(o *publicOpt) {
o.inputDir = path
o.inDir = path
}
}

Expand All @@ -36,7 +37,7 @@ func Extensions(extensions []string) PublicOption {

func GeneratePublic(options ...PublicOption) error {
opt := &publicOpt{
inputDir: ".",
inDir: ".",
outDir: "./public",
extensions: []string{".html"},
}
Expand All @@ -49,38 +50,43 @@ func GeneratePublic(options ...PublicOption) error {
return err
}

ignore, err := gitignore.CompileIgnoreFile(".gitignore")
ignore, err := gitignore.CompileIgnoreFile(filepath.Join(opt.inDir, ".gitignore"))
if err != nil {
return err
log.Printf("[warning] failed to compile .gitignore: %v\n", err)
}

err = filepath.WalkDir(opt.inputDir, func(path string, d fs.DirEntry, err error) error {
err = filepath.WalkDir(opt.inDir, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}

if d.IsDir() {
if d.Name() == filepath.Clean(opt.outDir) {
if d.Name() == filepath.Base(opt.outDir) {
return filepath.SkipDir
}
if d.Name() == ".git" {
return filepath.SkipDir
}
if ignore.MatchesPath(d.Name()) {
if ignore != nil && ignore.MatchesPath(d.Name()) {
return filepath.SkipDir
}
return nil
}

if ignore.MatchesPath(path) {
if ignore != nil && ignore.MatchesPath(path) {
return nil
}

if !contains(opt.extensions, filepath.Ext(path)) {
return nil
}

outPath := filepath.Join(opt.outDir, path)
relpath, err := filepath.Rel(opt.inDir, path)
if err != nil {
return err
}

outPath := filepath.Join(opt.outDir, relpath)
if err := os.MkdirAll(filepath.Dir(outPath), os.ModePerm); err != nil {
return err
}
Expand Down
Loading

0 comments on commit e216a0c

Please sign in to comment.