mirror of
https://github.com/passbolt/go-passbolt-cli.git
synced 2025-05-12 02:58:20 +00:00
add utils
This commit is contained in:
parent
219c451098
commit
f952b51065
2 changed files with 53 additions and 0 deletions
40
util/client.go
Normal file
40
util/client.go
Normal file
|
@ -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
|
||||||
|
}
|
13
util/context.go
Normal file
13
util/context.go
Normal file
|
@ -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
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue