Skip to content
This repository has been archived by the owner on Oct 29, 2020. It is now read-only.

Commit

Permalink
Merge pull request #12 from jaxxstorm/refactor_vaultclient
Browse files Browse the repository at this point in the history
Move the vault client into its own function
  • Loading branch information
jaxxstorm authored Mar 13, 2017
2 parents 27f1101 + d193b08 commit dd98b41
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
7 changes: 1 addition & 6 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
Expand Down
25 changes: 25 additions & 0 deletions vault/client.go
Original file line number Diff line number Diff line change
@@ -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

}

0 comments on commit dd98b41

Please sign in to comment.