restructure with single submodule and start implementing EurekaAdapter

This commit is contained in:
Iris Lightshard 2022-05-30 23:38:51 -06:00
parent bea90e0e1c
commit 8e8fd65f92
Signed by: Iris Lightshard
GPG key ID: 3B7FBC22144E6398
7 changed files with 149 additions and 86 deletions

View file

@ -1,41 +0,0 @@
package adapter
import (
"nilfm.cc/git/nirvash/page"
)
type EurekaAdapter struct {
Root string
}
func (self *EurekaAdapter) Name() string {
return "eureka"
}
func (self *EurekaAdapter) GetConfig(key string) (interface{}, error) {
return nil, nil
}
func (self *EurekaAdapter) SetConfig(key string, value interface{}) error {
return nil
}
func (self *EurekaAdapter) ListPages() map[string]string {
return map[string]string{}
}
func (self *EurekaAdapter) GetPage(path string) page.Page {
return page.Page{}
}
func (self *EurekaAdapter) FormatPage(raw string) string {
return raw
}
func (self *EurekaAdapter) FormattingHelp() string {
return "help!"
}
func (self *EurekaAdapter) Build() {
return
}

View file

@ -1,16 +1,16 @@
package adapter package archetype
import (
"nilfm.cc/git/nirvash/page"
)
type Adapter interface { type Adapter interface {
Init(cfg *Config)
Name() string Name() string
GetConfig(key string) (interface{}, error) GetConfig(key string) (interface{}, error)
SetConfig(key string, value interface{}) error SetConfig(key string, value interface{}) error
ListPages() map[string]string ListPages() map[string]string
GetPage(string) page.Page GetPage(string) (Page, error)
FormatPage(string) string FormatPage(string) string
FormattingHelp() string FormattingHelp() string
Build() CreatePage(page Page) error
EditPage(old Page, new Page) error
DeletePage(page Page) error
Build() string
} }

View file

@ -1,13 +1,12 @@
package cmd package archetype
import ( import (
"fmt" "fmt"
"nilfm.cc/git/nirvash/config"
"nilfm.cc/git/quartzgun/auth" "nilfm.cc/git/quartzgun/auth"
"strings" "strings"
) )
func Process(args []string, userStore auth.UserStore, cfg *config.Config) bool { func ProcessCmd(args []string, userStore auth.UserStore, cfg *Config) bool {
if len(args) == 1 { if len(args) == 1 {
return false return false
} }
@ -36,7 +35,7 @@ func Process(args []string, userStore auth.UserStore, cfg *config.Config) bool {
fmt.Printf("%s = %s\n", k, v) fmt.Printf("%s = %s\n", k, v)
switch k { switch k {
case "adapter": case "adapter":
config.SetAdapter(cfg, v) cfg.SetAdapter(v)
case "root": case "root":
cfg.Root = v cfg.Root = v
case "assetRoot": case "assetRoot":
@ -49,7 +48,7 @@ func Process(args []string, userStore auth.UserStore, cfg *config.Config) bool {
panic("unknown configuration option: " + v) panic("unknown configuration option: " + v)
} }
} }
config.Write(cfg) cfg.Write()
default: default:
help() help()
} }

View file

@ -1,8 +1,7 @@
package config package archetype
import ( import (
"fmt" "fmt"
"nilfm.cc/git/nirvash/adapter"
"os" "os"
"path/filepath" "path/filepath"
"runtime" "runtime"
@ -10,10 +9,10 @@ import (
) )
type Config struct { type Config struct {
Adapter adapter.Adapter // adapter for this instance Adapter Adapter // adapter for this instance
Root string // root of the site data Root string // root of the site data
StaticRoot string // root of static files for StaticFileManager StaticRoot string // root of static files for StaticFileManager
AssetRoot string // root of Nirvash dist files (CSS, images) AssetRoot string // root of Nirvash dist files (CSS, images)
Plugins map[string]interface{} Plugins map[string]interface{}
} }
@ -33,75 +32,77 @@ func GetConfigLocation() string {
} }
func ensureConfigLocationExists() { func ensureConfigLocationExists() {
_, err := os.Stat(GetConfigLocation()) fileInfo, err := os.Stat(GetConfigLocation())
if os.IsNotExist(err) { if os.IsNotExist(err) {
os.MkdirAll(GetConfigLocation(), os.ModePerm) os.MkdirAll(GetConfigLocation(), os.ModePerm)
} else if !fileInfo.IsDir() {
panic("Config location is not a directory!")
} }
} }
func Read() *Config { func ReadConfig() *Config {
ensureConfigLocationExists() ensureConfigLocationExists()
return parseConfig(filepath.Join(GetConfigLocation(), "nirvash.conf")) return parseConfig(filepath.Join(GetConfigLocation(), "nirvash.conf"))
} }
func Write(cfg *Config) error { func (self *Config) Write() error {
ensureConfigLocationExists() ensureConfigLocationExists()
return writeConfig(cfg, filepath.Join(GetConfigLocation(), "nirvash.conf")) return writeConfig(self, filepath.Join(GetConfigLocation(), "nirvash.conf"))
} }
func SetAdapter(cfg *Config, adptr string) { func (self *Config) SetAdapter(adapter string) {
switch adptr { switch adapter {
case "eureka": case "eureka":
cfg.Adapter = &adapter.EurekaAdapter{} self.Adapter = &EurekaAdapter{}
default: default:
panic("Unsupported adapter! Try one of [ eureka ]") panic("Unsupported adapter! Try one of [ eureka ]")
} }
} }
func IsNull(cfg *Config) bool { func (self *Config) IsNull() bool {
return cfg.Adapter == nil || len(cfg.Root) == 0 || len(cfg.StaticRoot) == 0 || len(cfg.AssetRoot) == 0 return self.Adapter == nil || len(self.Root) == 0 || len(self.StaticRoot) == 0 || len(self.AssetRoot) == 0
} }
func RunWizard(cfg *Config) { func (self *Config) RunWizard() {
fmt.Printf("All options are required.\n") fmt.Printf("All options are required.\n")
defer func(cfg *Config) { defer func(cfg *Config) {
if r := recover(); r != nil { if r := recover(); r != nil {
fmt.Printf("Invalid selection, starting over...") fmt.Printf("Invalid selection, starting over...")
RunWizard(cfg) cfg.RunWizard()
} }
}(cfg) }(self)
inputBuf := "" inputBuf := ""
fmt.Printf("adapter? (eureka) [eureka] ") fmt.Printf("adapter? (eureka) [eureka] ")
fmt.Scanln(&inputBuf) fmt.Scanln(&inputBuf)
if len(strings.TrimSpace(inputBuf)) == 0 { if len(strings.TrimSpace(inputBuf)) == 0 {
inputBuf = "eureka" inputBuf = "eureka"
} }
SetAdapter(cfg, inputBuf) self.SetAdapter(inputBuf)
inputBuf = "" inputBuf = ""
fmt.Printf("site data root? ") fmt.Printf("site data root? ")
ensureNonEmptyOption(&inputBuf) ensureNonEmptyOption(&inputBuf)
cfg.Root = inputBuf self.Root = inputBuf
inputBuf = "" inputBuf = ""
fmt.Printf("static file root? ") fmt.Printf("static file root? ")
ensureNonEmptyOption(&inputBuf) ensureNonEmptyOption(&inputBuf)
cfg.StaticRoot = inputBuf self.StaticRoot = inputBuf
inputBuf = "" inputBuf = ""
fmt.Printf("nirvash asset root? ") fmt.Printf("nirvash asset root? ")
ensureNonEmptyOption(&inputBuf) ensureNonEmptyOption(&inputBuf)
cfg.AssetRoot = inputBuf self.AssetRoot = inputBuf
inputBuf = "" inputBuf = ""
fmt.Printf("plugins? (not implemented yet) ") fmt.Printf("plugins? (not implemented yet) ")
ensureNonEmptyOption(&inputBuf) ensureNonEmptyOption(&inputBuf)
//cfg.Plugins = processPlugins(inputBuf) //self.Plugins = processPlugins(inputBuf)
fmt.Printf("Configuration complete!\n") fmt.Printf("Configuration complete!\n")
Write(cfg) self.Write()
} }
func ensureNonEmptyOption(buffer *string) { func ensureNonEmptyOption(buffer *string) {
@ -161,7 +162,7 @@ func parseConfig(configFile string) *Config {
case "plugins": case "plugins":
// not implemented // not implemented
case "adapter": case "adapter":
SetAdapter(cfg, v) cfg.SetAdapter(v)
default: default:
panic("Unrecognized config option: " + k) panic("Unrecognized config option: " + k)
} }

103
archetype/eureka.go Normal file
View file

@ -0,0 +1,103 @@
package archetype
import (
"errors"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
type EurekaAdapter struct {
Root string
}
func (self *EurekaAdapter) Init(cfg *Config) {
fileInfo, err := os.Stat(cfg.Root)
if os.IsNotExist(err) {
panic("SSG content root does not exist! Ensure your configs are correct or create it!")
} else if !fileInfo.IsDir() {
panic("SSG content root is not a directory!")
}
self.Root = cfg.Root
}
func (self *EurekaAdapter) Name() string {
return "eureka"
}
func (self *EurekaAdapter) GetConfig(key string) (interface{}, error) {
return nil, nil
}
func (self *EurekaAdapter) SetConfig(key string, value interface{}) error {
return nil
}
func (self *EurekaAdapter) ListPages() map[string]string {
files, err := ioutil.ReadDir(
filepath.Join(self.Root, "inc"))
if err != nil {
panic(err.Error())
}
pages := map[string]string{}
for _, file := range files {
filename := file.Name()
if strings.HasSuffix(filename, ".htm") {
pages[filename] = strings.Replace(
strings.TrimSuffix(filename, ".htm"), "_", " ", -1)
}
}
return pages
}
func (self *EurekaAdapter) GetPage(filename string) (Page, error) {
fullPath := filepath.Join(self.Root, "inc", filename)
f, err := os.ReadFile(fullPath)
if err != nil {
return Page{}, err
}
if !strings.HasSuffix(filename, ".htm") {
return Page{}, errors.New("Page file extension is not '.htm'")
}
title := strings.Replace(
strings.TrimSuffix(filename, ".htm"), "_", " ", -1)
fileInfo, _ := os.Stat(fullPath)
content := string(f[:])
return Page{
Title: title,
Content: content,
Edited: fileInfo.ModTime(),
}, nil
}
func (self *EurekaAdapter) FormatPage(raw string) string {
return raw
}
func (self *EurekaAdapter) FormattingHelp() string {
return "help!"
}
func (self *EurekaAdapter) CreatePage(page Page) error {
return nil
}
func (self *EurekaAdapter) EditPage(old Page, new Page) error {
return nil
}
func (self *EurekaAdapter) DeletePage(page Page) error {
return nil
}
func (self *EurekaAdapter) Build() string {
return "Build successful"
}

View file

@ -1,4 +1,4 @@
package page package archetype
import ( import (
"time" "time"

View file

@ -2,8 +2,7 @@ package main
import ( import (
"net/http" "net/http"
"nilfm.cc/git/nirvash/cmd" core "nilfm.cc/git/nirvash/archetype"
"nilfm.cc/git/nirvash/config"
"nilfm.cc/git/quartzgun/indentalUserDB" "nilfm.cc/git/quartzgun/indentalUserDB"
"nilfm.cc/git/quartzgun/middleware" "nilfm.cc/git/quartzgun/middleware"
"nilfm.cc/git/quartzgun/renderer" "nilfm.cc/git/quartzgun/renderer"
@ -13,18 +12,20 @@ import (
) )
func main() { func main() {
cfg := config.Read() cfg := core.ReadConfig()
udb := indentalUserDB.CreateIndentalUserDB( udb := indentalUserDB.CreateIndentalUserDB(
filepath.Join( filepath.Join(
config.GetConfigLocation(), core.GetConfigLocation(),
"user.db")) "user.db"))
if cmd.Process(os.Args, udb, cfg) { if core.ProcessCmd(os.Args, udb, cfg) {
os.Exit(0) os.Exit(0)
} }
if config.IsNull(cfg) { if cfg.IsNull() {
config.RunWizard(cfg) cfg.RunWizard()
} }
cfg.Adapter.Init(cfg)
rtr := &router.Router{ rtr := &router.Router{
StaticPaths: map[string]string{ StaticPaths: map[string]string{
"/static": cfg.AssetRoot, "/static": cfg.AssetRoot,