1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2024-12-11 21:10:26 +00:00

Implement CLI args

This commit is contained in:
bouncepaw 2020-10-25 20:06:51 +05:00
parent 9173b54c91
commit de8af13098
5 changed files with 66 additions and 15 deletions

View File

@ -2,9 +2,9 @@
A wiki engine.
This is the development branch for version 0.10. Features planned for this version:
* [ ] New file structure
* [x] New file structure
* [ ] Mycomarkup
* [ ] CLI options
* [x] CLI options
* [ ] CSS improvements
## Building
@ -17,6 +17,19 @@ make
# * create an executable called `mycorrhiza`. Run it with path to your wiki.
```
## Usage
```
mycorrhiza [OPTIONS...] WIKI_PATH
Options:
-home string
The home page (default "home")
-port string
Port to serve the wiki at (default "1737")
-title string
How to call your wiki in the navititle (default "🍄")
```
## Features
* Edit pages through html forms
* Responsive design

36
flag.go Normal file
View File

@ -0,0 +1,36 @@
package main
import (
"flag"
"log"
"path/filepath"
"github.com/bouncepaw/mycorrhiza/util"
)
func init() {
flag.StringVar(&util.ServerPort, "port", "1737", "Port to serve the wiki at")
flag.StringVar(&util.HomePage, "home", "home", "The home page")
flag.StringVar(&util.SiteTitle, "title", "🍄", "How to call your wiki in the navititle")
}
// Do the things related to cli args and die maybe
func parseCliArgs() {
flag.Parse()
args := flag.Args()
if len(args) == 0 {
log.Fatal("Error: pass a wiki directory")
}
var err error
WikiDir, err = filepath.Abs(args[0])
util.WikiDir = WikiDir
if err != nil {
log.Fatal(err)
}
if !isCanonicalName(util.HomePage) {
log.Fatal("Error: you must use a proper name for the homepage")
}
}

12
main.go
View File

@ -101,13 +101,7 @@ func handlerRecentChanges(w http.ResponseWriter, rq *http.Request) {
func main() {
log.Println("Running MycorrhizaWiki β")
var err error
WikiDir, err = filepath.Abs(os.Args[1])
util.WikiDir = WikiDir
if err != nil {
log.Fatal(err)
}
parseCliArgs()
if err := os.Chdir(WikiDir); err != nil {
log.Fatal(err)
}
@ -129,7 +123,7 @@ func main() {
http.ServeFile(w, rq, WikiDir+"/static/favicon.ico")
})
http.HandleFunc("/", func(w http.ResponseWriter, rq *http.Request) {
http.Redirect(w, rq, "/page/home", http.StatusSeeOther)
http.Redirect(w, rq, "/page/"+util.HomePage, http.StatusSeeOther)
})
log.Fatal(http.ListenAndServe("0.0.0.0:1737", nil))
log.Fatal(http.ListenAndServe("0.0.0.0:"+util.ServerPort, nil))
}

View File

@ -4,6 +4,8 @@ import (
"fmt"
"net/http"
"strings"
"github.com/bouncepaw/mycorrhiza/util"
)
// isCanonicalName checks if the `name` is canonical.
@ -17,16 +19,17 @@ func CanonicalName(name string) string {
}
// naviTitle turns `canonicalName` into html string with each hypha path parts higlighted as links.
// TODO: rework as a template
func naviTitle(canonicalName string) string {
var (
html = `<h1 class="navi-title" id="0">
<a href="/">🍄</a>`
html = fmt.Sprintf(`<h1 class="navi-title" id="navi-title">
<a href="/page/%s">%s</a>`, util.HomePage, util.SiteTitle)
prevAcc = `/page/`
parts = strings.Split(canonicalName, "/")
)
for _, part := range parts {
html += fmt.Sprintf(`
<span>/</span>
<span aria-hidden="true">/</span>
<a href="%s">%s</a>`,
prevAcc+part,
strings.Title(part))

View File

@ -5,7 +5,12 @@ import (
"strings"
)
var WikiDir string
var (
ServerPort string
HomePage string
SiteTitle string
WikiDir string
)
// ShorterPath is used by handlerList to display shorter path to the files. It simply strips WikiDir.
func ShorterPath(path string) string {