restructure with single submodule and start implementing EurekaAdapter
This commit is contained in:
parent
bea90e0e1c
commit
8e8fd65f92
7 changed files with 149 additions and 86 deletions
|
@ -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
|
||||
}
|
|
@ -1,16 +1,16 @@
|
|||
package adapter
|
||||
|
||||
import (
|
||||
"nilfm.cc/git/nirvash/page"
|
||||
)
|
||||
package archetype
|
||||
|
||||
type Adapter interface {
|
||||
Init(cfg *Config)
|
||||
Name() string
|
||||
GetConfig(key string) (interface{}, error)
|
||||
SetConfig(key string, value interface{}) error
|
||||
ListPages() map[string]string
|
||||
GetPage(string) page.Page
|
||||
GetPage(string) (Page, error)
|
||||
FormatPage(string) string
|
||||
FormattingHelp() string
|
||||
Build()
|
||||
CreatePage(page Page) error
|
||||
EditPage(old Page, new Page) error
|
||||
DeletePage(page Page) error
|
||||
Build() string
|
||||
}
|
|
@ -1,13 +1,12 @@
|
|||
package cmd
|
||||
package archetype
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"nilfm.cc/git/nirvash/config"
|
||||
"nilfm.cc/git/quartzgun/auth"
|
||||
"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 {
|
||||
return false
|
||||
}
|
||||
|
@ -36,7 +35,7 @@ func Process(args []string, userStore auth.UserStore, cfg *config.Config) bool {
|
|||
fmt.Printf("%s = %s\n", k, v)
|
||||
switch k {
|
||||
case "adapter":
|
||||
config.SetAdapter(cfg, v)
|
||||
cfg.SetAdapter(v)
|
||||
case "root":
|
||||
cfg.Root = v
|
||||
case "assetRoot":
|
||||
|
@ -49,7 +48,7 @@ func Process(args []string, userStore auth.UserStore, cfg *config.Config) bool {
|
|||
panic("unknown configuration option: " + v)
|
||||
}
|
||||
}
|
||||
config.Write(cfg)
|
||||
cfg.Write()
|
||||
default:
|
||||
help()
|
||||
}
|
|
@ -1,8 +1,7 @@
|
|||
package config
|
||||
package archetype
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"nilfm.cc/git/nirvash/adapter"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
|
@ -10,10 +9,10 @@ import (
|
|||
)
|
||||
|
||||
type Config struct {
|
||||
Adapter adapter.Adapter // adapter for this instance
|
||||
Root string // root of the site data
|
||||
StaticRoot string // root of static files for StaticFileManager
|
||||
AssetRoot string // root of Nirvash dist files (CSS, images)
|
||||
Adapter Adapter // adapter for this instance
|
||||
Root string // root of the site data
|
||||
StaticRoot string // root of static files for StaticFileManager
|
||||
AssetRoot string // root of Nirvash dist files (CSS, images)
|
||||
Plugins map[string]interface{}
|
||||
}
|
||||
|
||||
|
@ -33,75 +32,77 @@ func GetConfigLocation() string {
|
|||
}
|
||||
|
||||
func ensureConfigLocationExists() {
|
||||
_, err := os.Stat(GetConfigLocation())
|
||||
fileInfo, err := os.Stat(GetConfigLocation())
|
||||
|
||||
if os.IsNotExist(err) {
|
||||
os.MkdirAll(GetConfigLocation(), os.ModePerm)
|
||||
} else if !fileInfo.IsDir() {
|
||||
panic("Config location is not a directory!")
|
||||
}
|
||||
}
|
||||
|
||||
func Read() *Config {
|
||||
func ReadConfig() *Config {
|
||||
ensureConfigLocationExists()
|
||||
return parseConfig(filepath.Join(GetConfigLocation(), "nirvash.conf"))
|
||||
}
|
||||
|
||||
func Write(cfg *Config) error {
|
||||
func (self *Config) Write() error {
|
||||
ensureConfigLocationExists()
|
||||
return writeConfig(cfg, filepath.Join(GetConfigLocation(), "nirvash.conf"))
|
||||
return writeConfig(self, filepath.Join(GetConfigLocation(), "nirvash.conf"))
|
||||
}
|
||||
|
||||
func SetAdapter(cfg *Config, adptr string) {
|
||||
switch adptr {
|
||||
func (self *Config) SetAdapter(adapter string) {
|
||||
switch adapter {
|
||||
case "eureka":
|
||||
cfg.Adapter = &adapter.EurekaAdapter{}
|
||||
self.Adapter = &EurekaAdapter{}
|
||||
default:
|
||||
panic("Unsupported adapter! Try one of [ eureka ]")
|
||||
}
|
||||
}
|
||||
|
||||
func IsNull(cfg *Config) bool {
|
||||
return cfg.Adapter == nil || len(cfg.Root) == 0 || len(cfg.StaticRoot) == 0 || len(cfg.AssetRoot) == 0
|
||||
func (self *Config) IsNull() bool {
|
||||
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")
|
||||
defer func(cfg *Config) {
|
||||
if r := recover(); r != nil {
|
||||
fmt.Printf("Invalid selection, starting over...")
|
||||
RunWizard(cfg)
|
||||
cfg.RunWizard()
|
||||
}
|
||||
}(cfg)
|
||||
}(self)
|
||||
inputBuf := ""
|
||||
fmt.Printf("adapter? (eureka) [eureka] ")
|
||||
fmt.Scanln(&inputBuf)
|
||||
if len(strings.TrimSpace(inputBuf)) == 0 {
|
||||
inputBuf = "eureka"
|
||||
}
|
||||
SetAdapter(cfg, inputBuf)
|
||||
self.SetAdapter(inputBuf)
|
||||
|
||||
inputBuf = ""
|
||||
fmt.Printf("site data root? ")
|
||||
ensureNonEmptyOption(&inputBuf)
|
||||
cfg.Root = inputBuf
|
||||
self.Root = inputBuf
|
||||
|
||||
inputBuf = ""
|
||||
|
||||
fmt.Printf("static file root? ")
|
||||
ensureNonEmptyOption(&inputBuf)
|
||||
cfg.StaticRoot = inputBuf
|
||||
self.StaticRoot = inputBuf
|
||||
|
||||
inputBuf = ""
|
||||
fmt.Printf("nirvash asset root? ")
|
||||
ensureNonEmptyOption(&inputBuf)
|
||||
cfg.AssetRoot = inputBuf
|
||||
self.AssetRoot = inputBuf
|
||||
|
||||
inputBuf = ""
|
||||
fmt.Printf("plugins? (not implemented yet) ")
|
||||
ensureNonEmptyOption(&inputBuf)
|
||||
//cfg.Plugins = processPlugins(inputBuf)
|
||||
//self.Plugins = processPlugins(inputBuf)
|
||||
|
||||
fmt.Printf("Configuration complete!\n")
|
||||
Write(cfg)
|
||||
self.Write()
|
||||
}
|
||||
|
||||
func ensureNonEmptyOption(buffer *string) {
|
||||
|
@ -161,7 +162,7 @@ func parseConfig(configFile string) *Config {
|
|||
case "plugins":
|
||||
// not implemented
|
||||
case "adapter":
|
||||
SetAdapter(cfg, v)
|
||||
cfg.SetAdapter(v)
|
||||
default:
|
||||
panic("Unrecognized config option: " + k)
|
||||
}
|
103
archetype/eureka.go
Normal file
103
archetype/eureka.go
Normal 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"
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package page
|
||||
package archetype
|
||||
|
||||
import (
|
||||
"time"
|
15
nirvash.go
15
nirvash.go
|
@ -2,8 +2,7 @@ package main
|
|||
|
||||
import (
|
||||
"net/http"
|
||||
"nilfm.cc/git/nirvash/cmd"
|
||||
"nilfm.cc/git/nirvash/config"
|
||||
core "nilfm.cc/git/nirvash/archetype"
|
||||
"nilfm.cc/git/quartzgun/indentalUserDB"
|
||||
"nilfm.cc/git/quartzgun/middleware"
|
||||
"nilfm.cc/git/quartzgun/renderer"
|
||||
|
@ -13,18 +12,20 @@ import (
|
|||
)
|
||||
|
||||
func main() {
|
||||
cfg := config.Read()
|
||||
cfg := core.ReadConfig()
|
||||
udb := indentalUserDB.CreateIndentalUserDB(
|
||||
filepath.Join(
|
||||
config.GetConfigLocation(),
|
||||
core.GetConfigLocation(),
|
||||
"user.db"))
|
||||
if cmd.Process(os.Args, udb, cfg) {
|
||||
if core.ProcessCmd(os.Args, udb, cfg) {
|
||||
os.Exit(0)
|
||||
}
|
||||
if config.IsNull(cfg) {
|
||||
config.RunWizard(cfg)
|
||||
if cfg.IsNull() {
|
||||
cfg.RunWizard()
|
||||
}
|
||||
|
||||
cfg.Adapter.Init(cfg)
|
||||
|
||||
rtr := &router.Router{
|
||||
StaticPaths: map[string]string{
|
||||
"/static": cfg.AssetRoot,
|
||||
|
|
Loading…
Reference in a new issue