mirror of
https://github.com/passbolt/go-passbolt-cli.git
synced 2025-05-12 02:58:20 +00:00
feat: allow password to be taken from pipe
This commit is contained in:
parent
4768cd1333
commit
df187e8a5f
1 changed files with 31 additions and 3 deletions
|
@ -1,19 +1,47 @@
|
|||
package util
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
"syscall"
|
||||
|
||||
"github.com/passbolt/go-passbolt/api"
|
||||
"github.com/passbolt/go-passbolt/helper"
|
||||
"github.com/spf13/viper"
|
||||
"golang.org/x/crypto/ssh/terminal"
|
||||
"golang.org/x/term"
|
||||
)
|
||||
|
||||
func readPassword() (string, error) {
|
||||
var fd int
|
||||
var pass []byte
|
||||
if terminal.IsTerminal(syscall.Stdin) {
|
||||
fmt.Print("Enter Password:")
|
||||
|
||||
fd = syscall.Stdin
|
||||
inputPass, err := terminal.ReadPassword(fd)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
pass = inputPass
|
||||
} else {
|
||||
reader := bufio.NewReader(os.Stdin)
|
||||
s, err := reader.ReadString('\n')
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
pass = []byte(s)
|
||||
}
|
||||
|
||||
return strings.Replace(string(pass), "\n", "", 1), nil
|
||||
}
|
||||
|
||||
// GetClient gets a Logged in Passbolt Client
|
||||
func GetClient(ctx context.Context) (*api.Client, error) {
|
||||
serverAddress := viper.GetString("serverAddress")
|
||||
|
@ -28,13 +56,13 @@ func GetClient(ctx context.Context) (*api.Client, error) {
|
|||
|
||||
userPassword := viper.GetString("userPassword")
|
||||
if userPassword == "" {
|
||||
fmt.Print("Enter Password:")
|
||||
bytepw, err := term.ReadPassword(int(syscall.Stdin))
|
||||
cliPassword, err := readPassword()
|
||||
if err != nil {
|
||||
fmt.Println()
|
||||
return nil, fmt.Errorf("Reading Password: %w", err)
|
||||
}
|
||||
userPassword = string(bytepw)
|
||||
|
||||
userPassword = cliPassword
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue