-
Notifications
You must be signed in to change notification settings - Fork 36
/
main.go
105 lines (86 loc) · 2.52 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// SPDX-License-Identifier: MIT
// insert-user is a utility to create a new member and fallback password for them
package main
import (
"bytes"
"context"
"flag"
"fmt"
"os"
"os/user"
"path/filepath"
"strings"
"syscall"
_ "github.com/mattn/go-sqlite3"
"golang.org/x/crypto/ssh/terminal"
"github.com/ssb-ngi-pointer/go-ssb-room/v2/internal/repo"
"github.com/ssb-ngi-pointer/go-ssb-room/v2/roomdb"
"github.com/ssb-ngi-pointer/go-ssb-room/v2/roomdb/sqlite"
refs "go.mindeco.de/ssb-refs"
)
func main() {
u, err := user.Current()
check(err)
var (
role roomdb.Role = roomdb.RoleAdmin
repoPath string
)
flag.StringVar(&repoPath, "repo", filepath.Join(u.HomeDir, ".ssb-go-room"), "[optional] where the locally stored files of the room is located")
flag.Func("role", "[optional] which role the new member should have (values: mod[erator], admin, or member. default is admin)", func(val string) error {
switch strings.ToLower(val) {
case "admin":
role = roomdb.RoleAdmin
case "mod":
fallthrough
case "moderator":
role = roomdb.RoleModerator
case "member":
role = roomdb.RoleMember
default:
return fmt.Errorf("unknown member role: %q", val)
}
return nil
})
flag.Parse()
// we require one more argument which is not a flag.
if len(flag.Args()) != 1 {
cliMissingArguments("please provide a public key")
}
pubKey, err := refs.ParseFeedRef(flag.Arg(0))
if err != nil {
fmt.Fprintln(os.Stderr, "Invalid ssb public-key referenfce:", err)
os.Exit(1)
}
r := repo.New(repoPath)
db, err := sqlite.Open(r)
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)
}
ctx := context.Background()
mid, err := db.Members.Add(ctx, *pubKey, role)
check(err)
err = db.AuthFallback.SetPassword(ctx, mid, string(bytePassword))
check(err)
fmt.Fprintf(os.Stderr, "Created member (%s) with ID %d\n", role, mid)
}
func cliMissingArguments(message string) {
executable := strings.TrimPrefix(os.Args[0], "./")
fmt.Fprintf(os.Stderr, "%s: %s\nusage:%s <optional flags> <@base64-encoded-public-key=.ed25519>\n", executable, message, executable)
flag.Usage()
os.Exit(1)
}
func check(err error) {
if err != nil {
fmt.Fprintf(os.Stderr, "error: %s\n", err)
os.Exit(1)
}
}