2020-10-25 15:06:51 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-06-12 13:51:28 +00:00
|
|
|
_ "embed"
|
2020-10-25 15:06:51 +00:00
|
|
|
"flag"
|
2021-03-09 14:27:14 +00:00
|
|
|
"fmt"
|
2021-07-02 10:25:13 +00:00
|
|
|
"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"
|
2021-07-02 10:25:13 +00:00
|
|
|
"syscall"
|
2021-06-19 11:38:13 +00:00
|
|
|
|
|
|
|
"github.com/bouncepaw/mycorrhiza/cfg"
|
2021-07-02 10:25:13 +00:00
|
|
|
"github.com/bouncepaw/mycorrhiza/files"
|
|
|
|
"github.com/bouncepaw/mycorrhiza/user"
|
|
|
|
"golang.org/x/term"
|
2020-10-25 15:06:51 +00:00
|
|
|
)
|
|
|
|
|
2021-05-11 08:33:00 +00:00
|
|
|
// CLI options are read and parsed here.
|
|
|
|
|
2021-06-12 13:51:28 +00:00
|
|
|
// 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-10-01 17:12:16 +00:00
|
|
|
// CreateUserCommand is parameters for admin creation flag. Only a single parameter, though.
|
2021-07-02 10:25:13 +00:00
|
|
|
type CreateUserCommand struct {
|
|
|
|
name string
|
|
|
|
}
|
|
|
|
|
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() {
|
2021-07-02 10:25:13 +00:00
|
|
|
var createAdminName string
|
|
|
|
|
2021-07-05 19:47:07 +00:00
|
|
|
flag.StringVar(&cfg.ListenAddr, "listen-addr", "", "Address to listen on. For example, 127.0.0.1:1737 or /run/mycorrhiza.sock.")
|
2021-07-02 10:25:13 +00:00
|
|
|
flag.StringVar(&createAdminName, "create-admin", "", "Create a new admin. The password will be prompted in the terminal.")
|
|
|
|
flag.Usage = printHelp
|
2020-10-25 15:06:51 +00:00
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
args := flag.Args()
|
|
|
|
if len(args) == 0 {
|
2021-07-02 10:25:13 +00:00
|
|
|
log.Fatal("error: pass a wiki directory")
|
2020-10-25 15:06:51 +00:00
|
|
|
}
|
|
|
|
|
2021-05-09 10:42:12 +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
|
2021-07-02 10:25:13 +00:00
|
|
|
|
|
|
|
if createAdminName != "" {
|
|
|
|
createAdminCommand(createAdminName)
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func createAdminCommand(name string) {
|
|
|
|
wr := log.Writer()
|
|
|
|
log.SetFlags(0)
|
|
|
|
|
|
|
|
if err := files.PrepareWikiRoot(); err != nil {
|
|
|
|
log.Fatal("error: ", err)
|
|
|
|
}
|
|
|
|
cfg.UseAuth = true
|
|
|
|
cfg.AllowRegistration = true
|
|
|
|
|
|
|
|
log.SetOutput(io.Discard)
|
|
|
|
user.InitUserDatabase()
|
|
|
|
log.SetOutput(wr)
|
|
|
|
|
|
|
|
handle := int(syscall.Stdin)
|
|
|
|
if !term.IsTerminal(handle) {
|
|
|
|
log.Fatal("error: not a terminal")
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Print("Password: ")
|
|
|
|
passwordBytes, err := term.ReadPassword(handle)
|
|
|
|
fmt.Print("\n")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("error: ", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
password := string(passwordBytes)
|
|
|
|
|
|
|
|
log.SetOutput(io.Discard)
|
2021-07-14 21:00:35 +00:00
|
|
|
err = user.Register(name, password, "admin", "local", true)
|
2021-07-02 10:25:13 +00:00
|
|
|
log.SetOutput(wr)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("error: ", err)
|
|
|
|
}
|
2020-10-25 15:06:51 +00:00
|
|
|
}
|