From f952b510656f652b1ed2a20dd2323f08a21ffd45 Mon Sep 17 00:00:00 2001 From: Samuel Lorch Date: Wed, 8 Sep 2021 10:59:17 +0200 Subject: [PATCH] add utils --- util/client.go | 40 ++++++++++++++++++++++++++++++++++++++++ util/context.go | 13 +++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 util/client.go create mode 100644 util/context.go diff --git a/util/client.go b/util/client.go new file mode 100644 index 0000000..a8826ba --- /dev/null +++ b/util/client.go @@ -0,0 +1,40 @@ +package util + +import ( + "context" + "fmt" + + "github.com/speatzle/go-passbolt/api" + "github.com/spf13/viper" +) + +// 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 == "" { + return nil, fmt.Errorf("userPassword is not defined") + } + + 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 +} diff --git a/util/context.go b/util/context.go new file mode 100644 index 0000000..afbc8d0 --- /dev/null +++ b/util/context.go @@ -0,0 +1,13 @@ +package util + +import ( + "context" + + "github.com/spf13/viper" +) + +func GetContext() context.Context { + ctx, cancel := context.WithTimeout(context.Background(), viper.GetViper().GetDuration("timeout")) + _ = cancel + return ctx +}