|
1 | 1 | // These functions have been copied from the age project |
2 | | -// https://github.com/FiloSottile/age/blob/v1.0.0/cmd/age/encrypted_keys.go |
3 | 2 | // https://github.com/FiloSottile/age/blob/3d91014ea095e8d70f7c6c4833f89b53a96e0832/cmd/age/tui.go |
4 | 3 | // |
5 | 4 | // Copyright 2021 The age Authors. All rights reserved. |
@@ -27,43 +26,6 @@ const ( |
27 | 26 | SopsAgePasswordEnv = "SOPS_AGE_PASSWORD" |
28 | 27 | ) |
29 | 28 |
|
30 | | -// readPassphrase reads a passphrase from the terminal. It does not read from a |
31 | | -// non-terminal stdin, so it does not check stdinInUse. |
32 | | -func readPassphrase(prompt string) ([]byte, error) { |
33 | | - if testing.Testing() { |
34 | | - password := os.Getenv(SopsAgePasswordEnv) |
35 | | - if password != "" { |
36 | | - return []byte(password), nil |
37 | | - } |
38 | | - } |
39 | | - |
40 | | - var ( |
41 | | - err error |
42 | | - passphrase []byte |
43 | | - ) |
44 | | - |
45 | | - err = withTerminal(func(in, out *os.File) error { |
46 | | - _, err := fmt.Fprintf(out, "%s ", prompt) |
47 | | - if err != nil { |
48 | | - return fmt.Errorf("could not write prompt: %v", err) |
49 | | - } |
50 | | - |
51 | | - // Use CRLF to work around an apparent bug in WSL2's handling of CONOUT$. |
52 | | - // Only when running a Windows binary from WSL2, the cursor would not go |
53 | | - // back to the start of the line with a simple LF. Honestly, it's impressive |
54 | | - // CONIN$ and CONOUT$ even work at all inside WSL2. |
55 | | - defer fmt.Fprintf(out, "\r\n") |
56 | | - |
57 | | - if passphrase, err = term.ReadPassword(int(in.Fd())); err != nil { |
58 | | - return fmt.Errorf("could not read passphrase: %v", err) |
59 | | - } |
60 | | - |
61 | | - return nil |
62 | | - }) |
63 | | - |
64 | | - return passphrase, err |
65 | | -} |
66 | | - |
67 | 29 | func printf(format string, v ...interface{}) { |
68 | 30 | log.Printf("age: "+format, v...) |
69 | 31 | } |
@@ -133,6 +95,13 @@ func withTerminal(f func(in, out *os.File) error) error { |
133 | 95 |
|
134 | 96 | // readSecret reads a value from the terminal with no echo. The prompt is ephemeral. |
135 | 97 | func readSecret(prompt string) (s []byte, err error) { |
| 98 | + if testing.Testing() { |
| 99 | + password := os.Getenv(SopsAgePasswordEnv) |
| 100 | + if password != "" { |
| 101 | + return []byte(password), nil |
| 102 | + } |
| 103 | + } |
| 104 | + |
136 | 105 | err = withTerminal(func(in, out *os.File) error { |
137 | 106 | fmt.Fprintf(out, "%s ", prompt) |
138 | 107 | defer clearLine(out) |
|
0 commit comments