config, terminal: move PasswordPromptOutput from config to terminal

First, move this variable in order to avoid circular imports between
config and terminal.  (config already depends on terminal.)

But also, it seems PasswordPromptOutput conceptually fits well in
terminal, as the idea is for it to store the output for the terminal.
This commit is contained in:
Carl Edquist 2022-10-06 00:27:21 -05:00
parent 28f0c08a98
commit a716dc2533
4 changed files with 8 additions and 10 deletions

View File

@ -26,9 +26,6 @@ var (
// When nil, no encryption will be used for saving.
configKey []byte
// PasswordPromptOutput is output of prompt for password
PasswordPromptOutput = os.Stderr
// PassConfigKeyForDaemonization if set to true, the configKey
// is obscured with obscure.Obscure and saved to a temp file
// when it is calculated from the password. The path of that

View File

@ -716,9 +716,9 @@ func checkPassword(password string) (string, error) {
// GetPassword asks the user for a password with the prompt given.
func GetPassword(prompt string) string {
_, _ = fmt.Fprintln(PasswordPromptOutput, prompt)
_, _ = fmt.Fprintln(terminal.PasswordPromptOutput, prompt)
for {
_, _ = fmt.Fprint(PasswordPromptOutput, "password:")
_, _ = fmt.Fprint(terminal.PasswordPromptOutput, "password:")
password := ReadPassword()
password, err := checkPassword(password)
if err == nil {

View File

@ -9,7 +9,7 @@ import (
"log"
"os"
"github.com/rclone/rclone/fs/config"
"github.com/rclone/rclone/lib/terminal"
"golang.org/x/sys/unix"
)
@ -19,7 +19,7 @@ func redirectStderr(f *os.File) {
if err != nil {
log.Fatalf("Failed to duplicate stderr: %v", err)
}
config.PasswordPromptOutput = os.NewFile(uintptr(passPromptFd), "passPrompt")
terminal.PasswordPromptOutput = os.NewFile(uintptr(passPromptFd), "passPrompt")
err = unix.Dup2(int(f.Fd()), int(os.Stderr.Fd()))
if err != nil {
log.Fatalf("Failed to redirect stderr to file: %v", err)

View File

@ -8,8 +8,6 @@ import (
"runtime"
"sync"
"github.com/rclone/rclone/fs/config"
colorable "github.com/mattn/go-colorable"
)
@ -70,12 +68,15 @@ const (
var (
// make sure that start is only called once
once sync.Once
// PasswordPromptOutput is output of prompt for password
PasswordPromptOutput = os.Stderr
)
// Start the terminal - must be called before use
func Start() {
once.Do(func() {
f := config.PasswordPromptOutput
f := PasswordPromptOutput
if !IsTerminal(int(f.Fd())) {
// If output is not a tty then remove escape codes
Out = colorable.NewNonColorable(f)