go-passbolt-cli/util/client.go
2021-09-10 08:45:54 +02:00

47 lines
1.1 KiB
Go

package util
import (
"context"
"fmt"
"syscall"
"github.com/speatzle/go-passbolt/api"
"github.com/spf13/viper"
"golang.org/x/term"
)
// GetClient gets a Logged in Passbolt Client
func GetClient(ctx context.Context) (*api.Client, error) {
serverAddress := viper.GetString("serverAddress")
if serverAddress == "" {
return nil, fmt.Errorf("serverAddress is not defined")
}
userPrivateKey := viper.GetString("userPrivateKey")
if userPrivateKey == "" {
return nil, fmt.Errorf("userPrivateKey is not defined")
}
userPassword := viper.GetString("userPassword")
if userPassword == "" {
fmt.Print("Enter Password:")
bytepw, err := term.ReadPassword(int(syscall.Stdin))
if err != nil {
return nil, fmt.Errorf("Reading Password: %w", err)
}
userPassword = string(bytepw)
}
client, err := api.NewClient(nil, "", serverAddress, userPrivateKey, userPassword)
if err != nil {
return nil, fmt.Errorf("Creating Client: %w", err)
}
client.Debug = viper.GetBool("debug")
err = client.Login(ctx)
if err != nil {
return nil, fmt.Errorf("Logging in: %w", err)
}
return client, nil
}