From de8af13098054a776c89e2510e346c81bdbe1a52 Mon Sep 17 00:00:00 2001 From: bouncepaw Date: Sun, 25 Oct 2020 20:06:51 +0500 Subject: [PATCH] Implement CLI args --- README.md | 17 +++++++++++++++-- flag.go | 36 ++++++++++++++++++++++++++++++++++++ main.go | 12 +++--------- name.go | 9 ++++++--- util/util.go | 7 ++++++- 5 files changed, 66 insertions(+), 15 deletions(-) create mode 100644 flag.go diff --git a/README.md b/README.md index 2f42622..e142d68 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/flag.go b/flag.go new file mode 100644 index 0000000..d5d6df4 --- /dev/null +++ b/flag.go @@ -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") + } +} diff --git a/main.go b/main.go index abb11f8..140a926 100644 --- a/main.go +++ b/main.go @@ -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)) } diff --git a/name.go b/name.go index 741e965..1ceb0ba 100644 --- a/name.go +++ b/name.go @@ -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 = `

- 🍄` + html = fmt.Sprintf(`

+ %s`, util.HomePage, util.SiteTitle) prevAcc = `/page/` parts = strings.Split(canonicalName, "/") ) for _, part := range parts { html += fmt.Sprintf(` - / + %s`, prevAcc+part, strings.Title(part)) diff --git a/util/util.go b/util/util.go index 2e556d0..8458d4f 100644 --- a/util/util.go +++ b/util/util.go @@ -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 {