Refactor setting up the key manager from config

This commit is contained in:
Ethan Frey 2017-03-28 19:05:33 +02:00
parent 398ac046da
commit 58e537a42d
8 changed files with 23 additions and 23 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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