Skip to content

Commit

Permalink
vault-token
Browse files Browse the repository at this point in the history
  • Loading branch information
lingrino committed Jun 3, 2018
1 parent 0bab02d commit 3cb9d90
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 13 deletions.
2 changes: 1 addition & 1 deletion cmd/folder.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ var folderCmd = &cobra.Command{
}

func init() {
rootCmd.AddCommand(folderCmd)
vakuCmd.AddCommand(folderCmd)
}
25 changes: 21 additions & 4 deletions cmd/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,46 @@ package cmd
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"strings"

"github.com/Lingrino/vaku/vaku"
"github.com/pkg/errors"

vapi "github.com/hashicorp/vault/api"
homedir "github.com/mitchellh/go-homedir"
)

// vgc is the vaku client used by CLI commands
var vgc *vaku.Client

// authVGC initializes the vgc, vakuGlobalClient, to be used by all CLI commands
// TODO - try read from ~/.vault-token
// authVGC initializes the vgc (vaku global client) to be used by all CLI commands
func authVGC() {
// Initialize a new vault client
vclient, err := vapi.NewClient(vapi.DefaultConfig())
if err != nil {
fmt.Println(errors.Wrap(err, "Failed to create vault client"))
os.Exit(1)
} else if vclient.Token() == "" {
fmt.Println(errors.New("VAULT_TOKEN not set"))
os.Exit(1)
home, err := homedir.Dir()
if err != nil {
fmt.Println(errors.Wrap(err, "Could not find home directory to check ~/.vault-token"))
} else {
token, err := ioutil.ReadFile(home + "/.vault-token")
if err != nil {
if strings.Contains(err.Error(), "no such file or directory") {
fmt.Println("INFO: Attempted to read token at ~/.vault-token, but the file does not exist")
fmt.Println("Could not find token at VAULT_TOKEN or ~/.vault-token, exiting")
os.Exit(1)
} else {
fmt.Println(errors.Wrap(err, "Failed to read ~/.vault-token file"))
os.Exit(1)
}
}
vclient.SetToken(string(token))
}
}

// Add the Vault client to the Vaku client
Expand Down
2 changes: 1 addition & 1 deletion cmd/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ var pathCmd = &cobra.Command{
}

func init() {
rootCmd.AddCommand(pathCmd)
vakuCmd.AddCommand(pathCmd)
}
6 changes: 3 additions & 3 deletions cmd/root.go → cmd/vaku.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/spf13/cobra"
)

var rootCmd = &cobra.Command{
var vakuCmd = &cobra.Command{
Use: "vaku",
Short: "Vaku CLI extends the official Vault CLI with useful high-level functions",
Long: `Vaku CLI extends the official Vault CLI with useful high-level functions
Expand All @@ -31,9 +31,9 @@ API documentation is available at https://godoc.org/github.com/Lingrino/vaku/vak
}

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
// This is called by main.main(). It only needs to happen once to the vakuCmd.
func Execute() {
if err := rootCmd.Execute(); err != nil {
if err := vakuCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ var versionCmd = &cobra.Command{

Run: func(cmd *cobra.Command, args []string) {
print(map[string]interface{}{
"CLI": "1.0",
"CLI": "1.0.0",
"API": vaku.Version(),
})
},
}

func init() {
rootCmd.AddCommand(versionCmd)
vakuCmd.AddCommand(versionCmd)
}
2 changes: 1 addition & 1 deletion vaku/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ package vaku

// Version returns the current Vaku API version
func Version() string {
return "1.0"
return "1.0.0"
}
3 changes: 2 additions & 1 deletion vaku/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ import (
)

func TestVersion(t *testing.T) {
assert.Equal(t, vaku.Version(), "1.0")
t.Parallel()
assert.Equal(t, vaku.Version(), "1.0.0")
}

0 comments on commit 3cb9d90

Please sign in to comment.