1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2024-10-30 03:36:16 +00:00
mycorrhiza/flag.go

115 lines
2.4 KiB
Go
Raw Normal View History

2020-10-25 15:06:51 +00:00
package main
import (
"bufio"
_ "embed"
2020-10-25 15:06:51 +00:00
"flag"
2021-03-09 14:27:14 +00:00
"fmt"
"io"
2020-10-25 15:06:51 +00:00
"log"
2021-03-09 14:27:14 +00:00
"os"
2020-10-25 15:06:51 +00:00
"path/filepath"
"golang.org/x/term"
2021-06-19 11:38:13 +00:00
"github.com/bouncepaw/mycorrhiza/cfg"
"github.com/bouncepaw/mycorrhiza/files"
"github.com/bouncepaw/mycorrhiza/user"
2022-08-17 08:13:57 +00:00
"github.com/bouncepaw/mycorrhiza/version"
2020-10-25 15:06:51 +00:00
)
2021-05-11 08:33:00 +00:00
// CLI options are read and parsed here.
// printHelp prints the help message.
2021-05-11 08:33:00 +00:00
func printHelp() {
2021-06-19 11:38:13 +00:00
fmt.Fprintf(
2021-05-11 08:33:00 +00:00
flag.CommandLine.Output(),
2021-06-19 11:38:13 +00:00
"Usage: %s WIKI_PATH\n",
2021-05-11 08:33:00 +00:00
os.Args[0],
)
flag.PrintDefaults()
2020-10-25 15:06:51 +00:00
}
2021-05-11 08:33:00 +00:00
// parseCliArgs parses CLI options and sets several important global variables. Call it early.
2020-10-25 15:06:51 +00:00
func parseCliArgs() {
var createAdminName string
2022-08-17 08:13:57 +00:00
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.")
2022-08-17 08:13:57 +00:00
flag.BoolVar(&versionFlag, "version", false, "Print version information and exit.")
flag.Usage = printHelp
2020-10-25 15:06:51 +00:00
flag.Parse()
2022-08-17 08:13:57 +00:00
if versionFlag {
2022-09-14 07:38:36 +00:00
fmt.Println("Mycorrhiza Wiki", version.Long)
2022-08-17 08:13:57 +00:00
os.Exit(0)
}
2020-10-25 15:06:51 +00:00
args := flag.Args()
if len(args) == 0 {
log.Fatal("error: pass a wiki directory")
2020-10-25 15:06:51 +00:00
}
wikiDir, err := filepath.Abs(args[0])
2020-10-25 15:06:51 +00:00
if err != nil {
log.Fatal(err)
}
2021-06-19 04:51:10 +00:00
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
user.InitUserDatabase()
password, err := askPass("Password")
if err != nil {
log.Fatal(err)
}
if err := user.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
2020-10-25 15:06:51 +00:00
}