diff --git a/adapter/eureka.go b/adapter/eureka.go deleted file mode 100644 index 19be07d..0000000 --- a/adapter/eureka.go +++ /dev/null @@ -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 -} diff --git a/adapter/adapter.go b/archetype/adapter.go similarity index 55% rename from adapter/adapter.go rename to archetype/adapter.go index 0c684de..8ccd355 100644 --- a/adapter/adapter.go +++ b/archetype/adapter.go @@ -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 } diff --git a/cmd/cmd.go b/archetype/cmd.go similarity index 84% rename from cmd/cmd.go rename to archetype/cmd.go index 5675175..3d946a1 100644 --- a/cmd/cmd.go +++ b/archetype/cmd.go @@ -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() } diff --git a/config/config.go b/archetype/config.go similarity index 73% rename from config/config.go rename to archetype/config.go index 246d404..425d43f 100644 --- a/config/config.go +++ b/archetype/config.go @@ -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) } diff --git a/archetype/eureka.go b/archetype/eureka.go new file mode 100644 index 0000000..527ed68 --- /dev/null +++ b/archetype/eureka.go @@ -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" +} diff --git a/page/page.go b/archetype/page.go similarity index 83% rename from page/page.go rename to archetype/page.go index 0397376..6054646 100644 --- a/page/page.go +++ b/archetype/page.go @@ -1,4 +1,4 @@ -package page +package archetype import ( "time" diff --git a/nirvash.go b/nirvash.go index c285df0..44cc2bf 100644 --- a/nirvash.go +++ b/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,