pagr

A 'static site generator', built using dati.
Log | Files | Refs | Atom

commit 8932dab59e1012ad63fca9fa076b604abd8c4de8
parent 7e48911d776cd383d9e2584b2b070bc6d06f3238
Author: gearsix <gearsix@tuta.io>
Date:   Tue,  8 Jun 2021 15:45:41 +0100

removed -c, -v args; added config.go, config_test.go; pagr loads a config

Diffstat:
Aconfig.go | 54++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aconfig_test.go | 39+++++++++++++++++++++++++++++++++++++++
Mpagr.go | 35++++++++++++++++++++++-------------
3 files changed, 115 insertions(+), 13 deletions(-)

diff --git a/config.go b/config.go @@ -0,0 +1,54 @@ +package main + +import ( + "path/filepath" + "os" + "notabug.org/gearsix/suti" +) + +type Config struct { + Contents string + Templates string + Output string +} + +func (cfg *Config) relPaths(dir string) { + var paths = []string{cfg.Contents, cfg.Templates, cfg.Output} + for i, path := range paths { + if !filepath.IsAbs(path) { + paths[i] = filepath.Join(dir, path) + } + } + cfg.Contents = paths[0] + cfg.Templates = paths[1] + cfg.Output = paths[2] + return +} + +// NewConfig returns a Config with default values +func NewConfig() Config { + return Config { + Contents: "./content", + Templates: "./templates", + Output: "./out", + } +} + +// NewConfigFromFile returns a Config with values read from the config file found at `fpath`. +// If values from the file are missing, default values are used. +// suti.LoadDataFile() is called to load the file (see notabug.org/gearsix/suti). +// Any relative filepaths in the returned Config are set relative to the parent directory of `fpath`. +func NewConfigFromFile(fpath string) (cfg Config, err error) { + cfg = NewConfig() + + if _, err = os.Stat(fpath); err != nil { + return + } + + if err = suti.LoadDataFile(fpath, &cfg); err != nil { + return + } + + cfg.relPaths(filepath.Dir(fpath)) + return +} diff --git a/config_test.go b/config_test.go @@ -0,0 +1,39 @@ +package main + +import ( + "fmt" + "os" + "testing" +) + +func TestNewConfig(test *testing.T) { + test.Parallel() + cfg := NewConfig() + if cfg.Contents != "./content" { + test.Fatalf("invalid .Contents value: '%s'", cfg.Contents) + } + if cfg.Templates != "./templates" { + test.Fatalf("invalid .Templates value: '%s'", cfg.Templates) + } + if cfg.Output != "./out" { + test.Fatalf("invalid .Output value: %s", cfg) + } +} + +func TestNewConfigFromFile(test *testing.T) { + test.Parallel() + tdir := test.TempDir() + cfgp := fmt.Sprintf("%s/%s.toml", tdir, Name) + if f, err := os.Create(cfgp); err != nil { + test.Skipf("failed to create config file: '%s'", cfgp) + } else { + f.WriteString(`Output = "./test"`) + f.Close() + } + + if cfg, err := NewConfigFromFile(cfgp); err != nil { + test.Fatal(err) + } else if cfg.Output != tdir+"/test" { + test.Fatalf(".Output invalid: '%s'", cfg.Output) + } +} diff --git a/pagr.go b/pagr.go @@ -1,6 +1,7 @@ package main import ( + "fmt" "flag" "log" ) @@ -8,25 +9,33 @@ import ( const Name = "pagr" const Version = "0.0.0" -var flagDir string -var flagDirDesc = "directory of target project" -var flagVerbose bool -var flagVerboseDesc = "print verbose logs" - func init() { - flag.StringVar(&flagDir, "d", ".", flagDirDesc) - flag.StringVar(&flagDir, "dir", ".", flagDirDesc) - flag.BoolVar(&flagVerbose, "v", false, flagVerboseDesc) - flag.BoolVar(&flagVerbose, "verbose", false, flagVerboseDesc) - flag.Parse() } func main() { + flag.StringVar(&cfg, "cfg", "", "path to pagr project configuration file") + flag.BoolVar(&verbose, "verbose", false, "print verbose logs") + flag.Parse() + + config, err := loadConfig(cfg) + check(err) + fmt.Println(config) + + return } -func check(error) { +func check(err error) { if err != nil { - log.Printf("ERROR! %s\n", err) + log.Fatalf("ERROR! %s\n", err) } - return err != nil } + +func loadConfig(fpath string) (c Config, e error) { + if len(cfg) > 0 { + c, e = NewConfigFromFile(cfg) + } else { + c = NewConfig() + } + return +} +