init: remove --copy; the plain phrase line is enough
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AeFqV96dVeBX94inzcgZKw
This commit is contained in:
@@ -26,10 +26,7 @@ way to get your identity and listings back if the machine is lost
|
||||
(`allmende recover`). Keys and the database live in the data directory,
|
||||
`~/.local/share/allmende` by default (change it with `--data <dir>` or the
|
||||
`ALLMENDE_DATA` environment variable). Besides the numbered list, `init` prints the
|
||||
words as one plain line you can triple-click and paste. `bin/allmende init --copy` also copies the
|
||||
words to the clipboard as one line (via `wl-copy`, `xclip`, `xsel` or `pbcopy`,
|
||||
or your terminal over SSH): paste them into a password manager, then clear the
|
||||
clipboard.
|
||||
words as one plain line you can triple-click and paste into a password manager.
|
||||
|
||||
**3. Start the node**
|
||||
|
||||
|
||||
@@ -1,62 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// clipboardTools are the programs that put text on the system clipboard, in
|
||||
// the order they are tried.
|
||||
var clipboardTools = [][]string{
|
||||
{"wl-copy"}, // Wayland
|
||||
{"xclip", "-selection", "clipboard"}, // X11
|
||||
{"xsel", "--clipboard", "--input"}, // X11
|
||||
{"pbcopy"}, // macOS
|
||||
{"clip.exe"}, // Windows / WSL
|
||||
}
|
||||
|
||||
// copyToClipboard puts text on the clipboard and says how it did that. It uses
|
||||
// a clipboard program when one is installed, and otherwise asks the terminal
|
||||
// itself (OSC 52), which also works over SSH. The terminal gives no answer, so
|
||||
// for that route the result can only say that it was asked.
|
||||
func copyToClipboard(text string, tty io.Writer) (how string, err error) {
|
||||
for _, tool := range clipboardTools {
|
||||
path, err := exec.LookPath(tool[0])
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
cmd := exec.Command(path, tool[1:]...)
|
||||
cmd.Stdin = strings.NewReader(text)
|
||||
if err := cmd.Run(); err == nil {
|
||||
return tool[0], nil
|
||||
}
|
||||
}
|
||||
if tty == nil {
|
||||
return "", errors.New("no clipboard program found (install wl-clipboard or xclip)")
|
||||
}
|
||||
if _, err := io.WriteString(tty, osc52(text)); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return "your terminal", nil
|
||||
}
|
||||
|
||||
// osc52 is the escape sequence that makes a terminal copy text.
|
||||
func osc52(text string) string {
|
||||
return "\x1b]52;c;" + base64.StdEncoding.EncodeToString([]byte(text)) + "\x07"
|
||||
}
|
||||
|
||||
// copyPhrase copies the recovery phrase, as one line of words, which is the
|
||||
// form 'allmende recover' reads, and tells the owner how to treat the clipboard.
|
||||
func copyPhrase(words []string, tty *os.File) {
|
||||
how, err := copyToClipboard(strings.Join(words, " "), tty)
|
||||
if err != nil {
|
||||
fmt.Printf("Could not copy the phrase: %v\n\n", err)
|
||||
return
|
||||
}
|
||||
fmt.Printf("The 12 words are copied (by %s) as one line, ready to paste. Paste them into a\npassword manager now, then clear your clipboard: clipboard managers and other\nprograms can read it.\n\n", how)
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/base64"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestOSC52CarriesTheTextBase64(t *testing.T) {
|
||||
got := osc52("abandon ability able")
|
||||
want := "\x1b]52;c;" + base64.StdEncoding.EncodeToString([]byte("abandon ability able")) + "\x07"
|
||||
if got != want {
|
||||
t.Fatalf("got %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWithoutToolsOrTerminalCopyingFailsClearly(t *testing.T) {
|
||||
t.Setenv("PATH", t.TempDir()) // no clipboard program
|
||||
if _, err := copyToClipboard("x", nil); err == nil || !strings.Contains(err.Error(), "no clipboard program") {
|
||||
t.Fatalf("err = %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWithoutToolsTheTerminalIsAsked(t *testing.T) {
|
||||
t.Setenv("PATH", t.TempDir())
|
||||
var tty bytes.Buffer
|
||||
how, err := copyToClipboard("a b c", &tty)
|
||||
if err != nil || how != "your terminal" || tty.String() != osc52("a b c") {
|
||||
t.Fatalf("how=%q err=%v out=%q", how, err, tty.String())
|
||||
}
|
||||
}
|
||||
@@ -21,7 +21,6 @@ import (
|
||||
func cmdInit(args []string) error {
|
||||
fs := flag.NewFlagSet("init", flag.ExitOnError)
|
||||
data := dataFlag(fs)
|
||||
copyWords := fs.Bool("copy", false, "also copy the 12 words to the clipboard, as one line for pasting")
|
||||
fs.Parse(args)
|
||||
|
||||
if _, err := os.Stat(filepath.Join(*data, "device.key")); err == nil {
|
||||
@@ -34,13 +33,6 @@ func cmdInit(args []string) error {
|
||||
}
|
||||
words := strings.Fields(phrase)
|
||||
showPhrase(words)
|
||||
if *copyWords {
|
||||
var tty *os.File
|
||||
if term.IsTerminal(int(os.Stdout.Fd())) {
|
||||
tty = os.Stdout // only a real terminal can act on the OSC 52 fallback
|
||||
}
|
||||
copyPhrase(words, tty)
|
||||
}
|
||||
|
||||
// Only ask for confirmation when a person is at the keyboard.
|
||||
if term.IsTerminal(int(os.Stdin.Fd())) {
|
||||
|
||||
Reference in New Issue
Block a user