From d193b08bd2bb291bc4a9f8ca5c09783a93f3c15c Mon Sep 17 00:00:00 2001 From: Lee Briggs Date: Mon, 13 Mar 2017 19:11:41 +0000 Subject: [PATCH] Move the vault client into its own function --- cmd/root.go | 7 +------ vault/client.go | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 6 deletions(-) create mode 100644 vault/client.go diff --git a/cmd/root.go b/cmd/root.go index b1ebaaf..43593b3 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -25,8 +25,6 @@ import ( "os" "sync" - "github.com/hashicorp/go-cleanhttp" - "github.com/hashicorp/vault/api" v "github.com/jaxxstorm/unseal/vault" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" @@ -76,12 +74,9 @@ var RootCmd = &cobra.Command{ go func(hostName string, hostPort int) { defer wg.Done() - httpClient := cleanhttp.DefaultPooledClient() - // format the URL with the passed host and por - url := fmt.Sprintf("https://%s:%v", hostName, hostPort) // create a vault client - client, err := api.NewClient(&api.Config{Address: url, HttpClient: httpClient}) + client, err := v.VaultClient(hostName, hostPort) if err != nil { log.WithFields(log.Fields{"host": hostName, "port": hostPort}).Error(err) } diff --git a/vault/client.go b/vault/client.go new file mode 100644 index 0000000..eeab434 --- /dev/null +++ b/vault/client.go @@ -0,0 +1,25 @@ +package vault + +import ( + "fmt" + "github.com/hashicorp/go-cleanhttp" + "github.com/hashicorp/vault/api" +) + +func VaultClient(hostName string, hostPort int) (*api.Client, error) { + + // init a clean httpClient + httpClient := cleanhttp.DefaultPooledClient() + + // format the URL with the passed host and por + url := fmt.Sprintf("https://%s:%v", hostName, hostPort) + + // create a vault client + client, err := api.NewClient(&api.Config{Address: url, HttpClient: httpClient}) + if err != nil { + return nil, err + } + + return client, nil + +}