2021-02-09 11:53:33 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
2021-02-08 16:47:42 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2021-02-09 16:29:42 +00:00
|
|
|
"context"
|
2021-02-08 16:47:42 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"syscall"
|
|
|
|
|
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
|
|
"golang.org/x/crypto/ssh/terminal"
|
|
|
|
|
2021-03-02 16:14:02 +00:00
|
|
|
"github.com/ssb-ngi-pointer/go-ssb-room/internal/repo"
|
2021-03-16 14:11:13 +00:00
|
|
|
"github.com/ssb-ngi-pointer/go-ssb-room/roomdb/sqlite"
|
2021-02-08 16:47:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
|
|
|
if len(os.Args) != 3 {
|
2021-02-09 16:29:42 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "usage: %s <repo-location> <user-name>\n", os.Args[0])
|
|
|
|
fmt.Fprintf(os.Stderr, "repo-location: default is $HOME/.ssb-go-room\n")
|
2021-02-08 16:47:42 +00:00
|
|
|
os.Exit(1)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-02-09 16:29:42 +00:00
|
|
|
r := repo.New(os.Args[1])
|
|
|
|
db, err := sqlite.Open(r)
|
2021-02-08 16:47:42 +00:00
|
|
|
check(err)
|
|
|
|
defer db.Close()
|
|
|
|
|
|
|
|
fmt.Fprintln(os.Stderr, "Enter Password: ")
|
|
|
|
bytePassword, err := terminal.ReadPassword(int(syscall.Stdin))
|
|
|
|
check(err)
|
|
|
|
|
|
|
|
fmt.Fprintln(os.Stderr, "Repeat Password: ")
|
|
|
|
bytePasswordRepeat, err := terminal.ReadPassword(int(syscall.Stdin))
|
|
|
|
check(err)
|
|
|
|
|
|
|
|
if !bytes.Equal(bytePassword, bytePasswordRepeat) {
|
|
|
|
fmt.Fprintln(os.Stderr, "passwords didn't match")
|
|
|
|
os.Exit(1)
|
|
|
|
return
|
|
|
|
}
|
2021-03-02 16:14:02 +00:00
|
|
|
|
2021-02-09 16:29:42 +00:00
|
|
|
ctx := context.Background()
|
2021-03-02 16:14:02 +00:00
|
|
|
uid, err := db.AuthFallback.Create(ctx, os.Args[2], bytePassword)
|
2021-02-08 16:47:42 +00:00
|
|
|
check(err)
|
2021-03-02 16:14:02 +00:00
|
|
|
|
|
|
|
fmt.Fprintln(os.Stderr, "created user with ID", uid)
|
2021-02-08 16:47:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func check(err error) {
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "error: %s\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|