Skip to content

Commit

Permalink
Added some cli code
Browse files Browse the repository at this point in the history
  • Loading branch information
joncalhoun committed May 16, 2018
1 parent cb5a214 commit f2d5c79
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 38 deletions.
7 changes: 7 additions & 0 deletions cmd/cli.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import "github.com/gophercises/secret/cmd/cobra"

func main() {
cobra.RootCmd.Execute()
}
27 changes: 27 additions & 0 deletions cmd/cobra/get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package cobra

import (
"fmt"

"github.com/gophercises/secret"
"github.com/spf13/cobra"
)

var getCmd = &cobra.Command{
Use: "get",
Short: "Gets a secret in your secret storage",
Run: func(cmd *cobra.Command, args []string) {
v := secret.File(encodingKey, secretsPath())
key := args[0]
value, err := v.Get(key)
if err != nil {
fmt.Println("no value set")
return
}
fmt.Printf("%s = %s\n", key, value)
},
}

func init() {
RootCmd.AddCommand(getCmd)
}
25 changes: 25 additions & 0 deletions cmd/cobra/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package cobra

import (
"path/filepath"

"github.com/spf13/cobra"

homedir "github.com/mitchellh/go-homedir"
)

var RootCmd = &cobra.Command{
Use: "secret",
Short: "Secret is an API key and other secrets manager",
}

var encodingKey string

func init() {
RootCmd.PersistentFlags().StringVarP(&encodingKey, "key", "k", "", "the key to use when encoding and decoding secrets")
}

func secretsPath() string {
home, _ := homedir.Dir()
return filepath.Join(home, ".secrets")
}
26 changes: 26 additions & 0 deletions cmd/cobra/set.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package cobra

import (
"fmt"

"github.com/gophercises/secret"
"github.com/spf13/cobra"
)

var setCmd = &cobra.Command{
Use: "set",
Short: "Sets a secret in your secret storage",
Run: func(cmd *cobra.Command, args []string) {
v := secret.File(encodingKey, secretsPath())
key, value := args[0], args[1]
err := v.Set(key, value)
if err != nil {
panic(err)
}
fmt.Println("Value set successfully!")
},
}

func init() {
RootCmd.AddCommand(setCmd)
}
38 changes: 0 additions & 38 deletions cmd/demo.go

This file was deleted.

0 comments on commit f2d5c79

Please sign in to comment.