mirror of
https://github.com/passbolt/go-passbolt-cli.git
synced 2025-05-12 10:58:21 +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
|
package util
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
"github.com/passbolt/go-passbolt/api"
|
"github.com/passbolt/go-passbolt/api"
|
||||||
"github.com/passbolt/go-passbolt/helper"
|
"github.com/passbolt/go-passbolt/helper"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
|
"golang.org/x/crypto/ssh/terminal"
|
||||||
"golang.org/x/term"
|
"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
|
// GetClient gets a Logged in Passbolt Client
|
||||||
func GetClient(ctx context.Context) (*api.Client, error) {
|
func GetClient(ctx context.Context) (*api.Client, error) {
|
||||||
serverAddress := viper.GetString("serverAddress")
|
serverAddress := viper.GetString("serverAddress")
|
||||||
|
@ -28,13 +56,13 @@ func GetClient(ctx context.Context) (*api.Client, error) {
|
||||||
|
|
||||||
userPassword := viper.GetString("userPassword")
|
userPassword := viper.GetString("userPassword")
|
||||||
if userPassword == "" {
|
if userPassword == "" {
|
||||||
fmt.Print("Enter Password:")
|
cliPassword, err := readPassword()
|
||||||
bytepw, err := term.ReadPassword(int(syscall.Stdin))
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println()
|
fmt.Println()
|
||||||
return nil, fmt.Errorf("Reading Password: %w", err)
|
return nil, fmt.Errorf("Reading Password: %w", err)
|
||||||
}
|
}
|
||||||
userPassword = string(bytepw)
|
|
||||||
|
userPassword = cliPassword
|
||||||
fmt.Println()
|
fmt.Println()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue