github.com/core-coin/go-core/v2@v2.1.9/cmd/utils/prompt.go (about) 1 // Copyright 2020 by the Authors 2 // This file is part of go-core. 3 // 4 // go-core is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // go-core is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with go-core. If not, see <http://www.gnu.org/licenses/>. 16 17 // Package utils contains internal helper functions for go-core commands. 18 package utils 19 20 import ( 21 "fmt" 22 23 "github.com/core-coin/go-core/v2/console/prompt" 24 ) 25 26 // GetPassPhrase displays the given text(prompt) to the user and requests some textual 27 // data to be entered, but one which must not be echoed out into the terminal. 28 // The method returns the input provided by the user. 29 func GetPassPhrase(text string, confirmation bool) string { 30 if text != "" { 31 fmt.Println(text) 32 } 33 password, err := prompt.Stdin.PromptPassword("Password: ") 34 if err != nil { 35 Fatalf("Failed to read password: %v", err) 36 } 37 if confirmation { 38 confirm, err := prompt.Stdin.PromptPassword("Repeat password: ") 39 if err != nil { 40 Fatalf("Failed to read password confirmation: %v", err) 41 } 42 if password != confirm { 43 Fatalf("Passwords do not match") 44 } 45 } 46 return password 47 } 48 49 // GetPassPhraseWithList retrieves the password associated with an account, either fetched 50 // from a list of preloaded passphrases, or requested interactively from the user. 51 func GetPassPhraseWithList(text string, confirmation bool, index int, passwords []string) string { 52 // If a list of passwords was supplied, retrieve from them 53 if len(passwords) > 0 { 54 if index < len(passwords) { 55 return passwords[index] 56 } 57 return passwords[len(passwords)-1] 58 } 59 // Otherwise prompt the user for the password 60 password := GetPassPhrase(text, confirmation) 61 return password 62 }