From f19841a8c8183f4b25721dfa7f04b42df4d7fffb Mon Sep 17 00:00:00 2001 From: cblgh Date: Tue, 23 Mar 2021 18:00:08 +0100 Subject: [PATCH 1/4] rewrite insert-user, use flags for providing all options --- cmd/insert-user/generate-fake-id.sh | 3 ++ cmd/insert-user/main.go | 52 +++++++++++++++++++++-------- 2 files changed, 42 insertions(+), 13 deletions(-) create mode 100755 cmd/insert-user/generate-fake-id.sh diff --git a/cmd/insert-user/generate-fake-id.sh b/cmd/insert-user/generate-fake-id.sh new file mode 100755 index 0000000..f74d528 --- /dev/null +++ b/cmd/insert-user/generate-fake-id.sh @@ -0,0 +1,3 @@ +#!/bin/bash +id=$(dd if=/dev/urandom bs=1 count=32 2>/dev/null | base64 -w0) +echo "@$id.ed25519" diff --git a/cmd/insert-user/main.go b/cmd/insert-user/main.go index 461bb2e..fe1481d 100644 --- a/cmd/insert-user/main.go +++ b/cmd/insert-user/main.go @@ -30,20 +30,35 @@ func main() { check(err) var ( - repoPath string + name string + pubKey *refs.FeedRef role roomdb.Role = roomdb.RoleAdmin + repoPath string ) - flag.StringVar(&repoPath, "repo", filepath.Join(u.HomeDir, ".ssb-go-room"), "where the repo of the room is located") - flag.Func("role", "which role the new member should have (ie moderator, admin, member. defaults to admin)", func(val string) error { + flag.StringVar(&name, "name", "", "username (used when logging into the room's web ui)") + flag.Func("key", "the public key of the user, format: @.ed25519", func(val string) error { + if len(val) == 0 { + return fmt.Errorf("the public key is required. if you are just testing things out, generate one by running 'cmd/insert-user/generate-fake-id.sh'\n") + } + key, err := refs.ParseFeedRef(val) + if err != nil { + return fmt.Errorf("%s\n", err) + } + pubKey = key + return nil + }) + 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.RoleAdmin + role = roomdb.RoleModerator case "member": role = roomdb.RoleMember - default: return fmt.Errorf("unknown member role: %q", val) } @@ -53,11 +68,18 @@ func main() { flag.Parse() - if len(os.Args) != 3 { - fmt.Fprintf(os.Stderr, "usage: %s <@theirPublicKey.ed25519>\n", os.Args[0]) - flag.Usage() - os.Exit(1) - return + // we require at least 5 arguments: + -name + -key + // 1 2 3 4 5 + if len(os.Args) < 5 { + cliMissingArguments("please provide the default arguments -name and -key") + } + + if name == "" { + cliMissingArguments("please provide a username with -name ") + } + + if pubKey == nil { + cliMissingArguments("please provide a public key with -key") } r := repo.New(repoPath) @@ -65,9 +87,6 @@ func main() { check(err) defer db.Close() - pubKey, err := refs.ParseFeedRef(os.Args[2]) - check(err) - fmt.Fprintln(os.Stderr, "Enter Password: ") bytePassword, err := terminal.ReadPassword(int(syscall.Stdin)) check(err) @@ -92,6 +111,13 @@ func main() { fmt.Fprintln(os.Stderr, "created member with ID", mid) } +func cliMissingArguments(message string) { + executable := strings.TrimPrefix(os.Args[0], "./") + fmt.Fprintf(os.Stderr, "%s: %s\nusage:%s -name -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) From bc88a56c38993bd11bfc7a80b823550eadeec3e7 Mon Sep 17 00:00:00 2001 From: cblgh Date: Tue, 23 Mar 2021 18:03:13 +0100 Subject: [PATCH 2/4] update readme instructions --- README.md | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 74aee54..59e0366 100644 --- a/README.md +++ b/README.md @@ -105,11 +105,14 @@ Aside: I would have used `sqlc` since it's a bit more minimal and uses hand writ ```bash cd cmd/insert-user go build -./insert-user $HOME/.ssb-go-room/roomdb my-user +# optional step: run a script to generate a valid ssb id @.ed25519, useful for trying things out quickly +./generate-fake-id.sh +./insert-user -name -key <@pubkey.ed25519> ``` - Then repeat your password twice and you are all set for development. +Run `insert-user` without any flags to see all the options. + ## Testing ### Rooms From 553e5d464b9d163e66299865f45a26074b859daa Mon Sep 17 00:00:00 2001 From: cblgh Date: Wed, 24 Mar 2021 10:09:35 +0100 Subject: [PATCH 3/4] fix formatting, improve output message on member add --- cmd/insert-user/main.go | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/cmd/insert-user/main.go b/cmd/insert-user/main.go index fe1481d..685e0e1 100644 --- a/cmd/insert-user/main.go +++ b/cmd/insert-user/main.go @@ -65,11 +65,10 @@ func main() { return nil }) - flag.Parse() - // we require at least 5 arguments: + -name + -key - // 1 2 3 4 5 + /* we require at least 5 arguments: + -name + -key */ + /* 1 2 3 4 5 */ if len(os.Args) < 5 { cliMissingArguments("please provide the default arguments -name and -key") } @@ -96,7 +95,7 @@ func main() { check(err) if !bytes.Equal(bytePassword, bytePasswordRepeat) { - fmt.Fprintln(os.Stderr, "passwords didn't match") + fmt.Fprintln(os.Stderr, "Passwords didn't match") os.Exit(1) return } @@ -108,7 +107,7 @@ func main() { err = db.AuthFallback.Create(ctx, mid, os.Args[1], bytePassword) check(err) - fmt.Fprintln(os.Stderr, "created member with ID", mid) + fmt.Fprintf(os.Stderr, "Created member %s (%s) with ID %d\n", name, role, mid) } func cliMissingArguments(message string) { From c81304a5999bf759fea73640868dfa12201ef350 Mon Sep 17 00:00:00 2001 From: cblgh Date: Wed, 24 Mar 2021 10:10:39 +0100 Subject: [PATCH 4/4] improve string referencing in script --- cmd/insert-user/generate-fake-id.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/insert-user/generate-fake-id.sh b/cmd/insert-user/generate-fake-id.sh index f74d528..e97b695 100755 --- a/cmd/insert-user/generate-fake-id.sh +++ b/cmd/insert-user/generate-fake-id.sh @@ -1,3 +1,3 @@ #!/bin/bash id=$(dd if=/dev/urandom bs=1 count=32 2>/dev/null | base64 -w0) -echo "@$id.ed25519" +echo "@${id}.ed25519"