1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2025-03-10 13:38:20 +00:00
mycorrhiza/flag.go
Timur Ismagilov 41733c50bd
New templates #117 (#236)
Didn't have the chance to migrate //all// templates just yet. We'll get there.

* Implement yet another template system

* Move orphans to the new system and fix a bug in it

* Link orphans in the admin panel

* Move the backlink handlers to the web package

* Move auth routing to web

* Move /user-list to the new system

* Move change password and translate it

* Move stuff

* Move admin-related stuff to the web

* Move a lot of files into internal dir

Outside of it are web and stuff that needs further refactoring

* Fix static not loading and de-qtpl tree

* Move tree to internal

* Keep the globe on the same line #230

* Revert "Keep the globe on the same line #230"

This reverts commit ae78e5e459b1e980ba89bf29e61f75c0625ed2c7.

* Migrate templates from hypview: delete, edit, start empty and existing WIP

The delete media view was removed, I didn't even know it still existed as a GET. A rudiment.

* Make views multi-file and break compilation

* Megarefactoring of hypha views

* Auth-related stuffs

* Fix some of those weird imports

* Migrate cat views

* Fix cat js

* Lower standards

* Internalize trauma
2024-09-07 21:22:41 +03:00

115 lines
2.5 KiB
Go

package main
import (
"bufio"
_ "embed"
"flag"
"fmt"
"io"
"log"
"os"
"path/filepath"
"golang.org/x/term"
"github.com/bouncepaw/mycorrhiza/internal/cfg"
"github.com/bouncepaw/mycorrhiza/internal/files"
user2 "github.com/bouncepaw/mycorrhiza/internal/user"
"github.com/bouncepaw/mycorrhiza/internal/version"
)
// CLI options are read and parsed here.
// printHelp prints the help message.
func printHelp() {
_, _ = fmt.Fprintf(
flag.CommandLine.Output(),
"Usage: %s WIKI_PATH\n",
os.Args[0],
)
flag.PrintDefaults()
}
// parseCliArgs parses CLI options and sets several important global variables. Call it early.
func parseCliArgs() {
var createAdminName string
var versionFlag bool
flag.StringVar(&cfg.ListenAddr, "listen-addr", "", "Address to listen on. For example, 127.0.0.1:1737 or /run/mycorrhiza.sock.")
flag.StringVar(&createAdminName, "create-admin", "", "Create a new admin. The password will be prompted in the terminal.")
flag.BoolVar(&versionFlag, "version", false, "Print version information and exit.")
flag.Usage = printHelp
flag.Parse()
if versionFlag {
fmt.Println("Mycorrhiza Wiki", version.Long)
os.Exit(0)
}
args := flag.Args()
if len(args) == 0 {
log.Fatal("error: pass a wiki directory")
}
wikiDir, err := filepath.Abs(args[0])
if err != nil {
log.Fatal(err)
}
cfg.WikiDir = wikiDir
if createAdminName != "" {
createAdminCommand(createAdminName)
os.Exit(0)
}
}
func createAdminCommand(name string) {
if err := files.PrepareWikiRoot(); err != nil {
log.Fatal(err)
}
cfg.UseAuth = true
cfg.AllowRegistration = true
user2.InitUserDatabase()
password, err := askPass("Password")
if err != nil {
log.Fatal(err)
}
if err := user2.Register(name, password, "admin", "local", true); err != nil {
log.Fatal(err)
}
}
func askPass(prompt string) (string, error) {
var password []byte
var err error
fd := int(os.Stdin.Fd())
if term.IsTerminal(fd) {
fmt.Printf("%s: ", prompt)
password, err = term.ReadPassword(int(os.Stdin.Fd()))
if err != nil {
return "", err
}
fmt.Println()
} else {
fmt.Fprintf(os.Stderr, "Warning: Reading password from stdin.\n")
// TODO: the buffering messes up repeated calls to readPassword
scanner := bufio.NewScanner(os.Stdin)
if !scanner.Scan() {
if err := scanner.Err(); err != nil {
return "", err
}
return "", io.ErrUnexpectedEOF
}
password = scanner.Bytes()
if len(password) == 0 {
return "", fmt.Errorf("zero length password")
}
}
return string(password), nil
}