Browse Source

Refactor setting up the key manager from config

pull/1782/head
Ethan Frey 8 years ago
parent
commit
58e537a42d
8 changed files with 23 additions and 23 deletions
  1. +4
    -3
      cmd/common.go
  2. +1
    -1
      cmd/get.go
  3. +0
    -1
      cmd/keys/main.go
  4. +1
    -1
      cmd/list.go
  5. +1
    -1
      cmd/new.go
  6. +14
    -14
      cmd/root.go
  7. +1
    -1
      cmd/serve.go
  8. +1
    -1
      cmd/update.go

+ 4
- 3
cmd/common.go View File

@ -22,14 +22,15 @@ It is here for experimentation of re-use between go-keys and light-client.
*********/
const (
RootFlag = "root"
OutputFlag = "output"
RootFlag = "root"
OutputFlag = "output"
EncodingFlag = "encoding"
)
func PrepareMainCmd(cmd *cobra.Command, envPrefix, defautRoot string) func() {
cobra.OnInitialize(func() { initEnv(envPrefix) })
cmd.PersistentFlags().StringP(RootFlag, "r", defautRoot, "root directory for config and data")
cmd.PersistentFlags().StringP("encoding", "e", "hex", "Binary encoding (hex|b64|btc)")
cmd.PersistentFlags().StringP(EncodingFlag, "e", "hex", "Binary encoding (hex|b64|btc)")
cmd.PersistentFlags().StringP(OutputFlag, "o", "text", "Output format (text|json)")
cmd.PersistentPreRunE = multiE(bindFlags, setEncoding, validateOutput, cmd.PersistentPreRunE)
return func() { execute(cmd) }


+ 1
- 1
cmd/get.go View File

@ -32,7 +32,7 @@ var getCmd = &cobra.Command{
}
name := args[0]
info, err := Manager.Get(name)
info, err := GetKeyManager().Get(name)
if err != nil {
fmt.Println(err.Error())
return


+ 0
- 1
cmd/keys/main.go View File

@ -21,7 +21,6 @@ import (
)
func main() {
cmd.RootCmd.PersistentPreRunE = cmd.SetupKeys
cmd.PrepareMainCmd(cmd.RootCmd, "TM", os.ExpandEnv("$HOME/.tlc"))
cmd.RootCmd.Execute()
// exec()


+ 1
- 1
cmd/list.go View File

@ -27,7 +27,7 @@ var listCmd = &cobra.Command{
Long: `Return a list of all public keys stored by this key manager
along with their associated name and address.`,
Run: func(cmd *cobra.Command, args []string) {
infos, err := Manager.List()
infos, err := GetKeyManager().List()
if err != nil {
fmt.Println(err.Error())
return


+ 1
- 1
cmd/new.go View File

@ -50,7 +50,7 @@ func newPassword(cmd *cobra.Command, args []string) {
return
}
info, err := Manager.Create(name, pass, algo)
info, err := GetKeyManager().Create(name, pass, algo)
if err != nil {
fmt.Println(err.Error())
return


+ 14
- 14
cmd/root.go View File

@ -27,7 +27,7 @@ import (
const KeySubdir = "keys"
var (
Manager keys.Manager
manager keys.Manager
)
// RootCmd represents the base command when called without any subcommands
@ -41,17 +41,17 @@ used by light-clients, full nodes, or any other application that
needs to sign with a private key.`,
}
// SetupKeys must be registered in main() on the top level command
// here this is RootCmd, but if we embed keys in eg. light-client,
// that must be responsible for the update
func SetupKeys(cmd *cobra.Command, args []string) error {
// store the keys directory
rootDir := viper.GetString("root")
keyDir := filepath.Join(rootDir, KeySubdir)
// and construct the key manager
Manager = cryptostore.New(
cryptostore.SecretBox,
filestorage.New(keyDir),
)
return nil
// GetKeyManager initializes a key manager based on the configuration
func GetKeyManager() keys.Manager {
if manager == nil {
// store the keys directory
rootDir := viper.GetString("root")
keyDir := filepath.Join(rootDir, KeySubdir)
// and construct the key manager
manager = cryptostore.New(
cryptostore.SecretBox,
filestorage.New(keyDir),
)
}
return manager
}

+ 1
- 1
cmd/serve.go View File

@ -64,7 +64,7 @@ func serveHTTP(cmd *cobra.Command, args []string) error {
}
router := mux.NewRouter()
ks := server.New(Manager, viper.GetString("type"))
ks := server.New(GetKeyManager(), viper.GetString("type"))
ks.Register(router)
// only set cors for tcp listener


+ 1
- 1
cmd/update.go View File

@ -50,7 +50,7 @@ func updatePassword(cmd *cobra.Command, args []string) {
return
}
err = Manager.Update(name, oldpass, newpass)
err = GetKeyManager().Update(name, oldpass, newpass)
if err != nil {
fmt.Println(err.Error())
} else {


Loading…
Cancel
Save