add cmd, update config.go

This commit is contained in:
arnaucube
2019-04-13 15:43:48 +02:00
parent ff73690719
commit fd30384b23
2 changed files with 92 additions and 8 deletions

82
cmd/cmd.go Normal file
View File

@@ -0,0 +1,82 @@
package cmd
import (
"crypto/ecdsa"
"encoding/hex"
"fmt"
"io/ioutil"
"github.com/arnaucube/slowlorisdb/config"
"github.com/arnaucube/slowlorisdb/core"
"github.com/arnaucube/slowlorisdb/db"
"github.com/arnaucube/slowlorisdb/node"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli"
)
var Commands = []cli.Command{
{
Name: "create",
Aliases: []string{},
Usage: "create the node",
Action: cmdCreate,
},
{
Name: "start",
Aliases: []string{},
Usage: "start the node",
Action: cmdStart,
},
}
func cmdCreate(c *cli.Context) error {
log.Info("creating new keys of the node")
privK, err := core.NewKey()
if err != nil {
return err
}
fmt.Println(privK)
return nil
}
func cmdStart(c *cli.Context) error {
conf, err := config.MustRead(c)
if err != nil {
return err
}
dir, err := ioutil.TempDir("", conf.DbPath)
if err != nil {
return err
}
db, err := db.New(dir)
if err != nil {
return err
}
// parse AuthNodes from the config file
var authNodes []*ecdsa.PublicKey
for _, authNode := range conf.AuthNodes {
packedPubK, err := hex.DecodeString(authNode)
if err != nil {
return err
}
pubK := core.UnpackPubK(packedPubK)
authNodes = append(authNodes, pubK)
}
bc := core.NewPoABlockchain(db, authNodes)
// TODO parse privK from path in the config file
privK, err := core.NewKey()
node, err := node.NewNode(privK, bc, true)
if err != nil {
return err
}
err = node.Start()
if err != nil {
return err
}
return nil
}

View File

@@ -8,13 +8,15 @@ import (
) )
type Config struct { type Config struct {
DbPath string
Port string Port string
Dest string Dest string
AuthNodes []string // PubKs in hex format of the AuthNodes for the blockchain
} }
var C Config func MustRead(c *cli.Context) (*Config, error) {
var config Config
func MustRead(c *cli.Context) error {
viper.SetConfigType("yaml") viper.SetConfigType("yaml")
viper.SetConfigName("config") viper.SetConfigName("config")
viper.AddConfigPath(".") // adding home directory as first search path viper.AddConfigPath(".") // adding home directory as first search path
@@ -27,10 +29,10 @@ func MustRead(c *cli.Context) error {
} }
if err := viper.ReadInConfig(); err != nil { if err := viper.ReadInConfig(); err != nil {
return err return nil, err
} }
if err := viper.Unmarshal(&C); err != nil { if err := viper.Unmarshal(&config); err != nil {
return err return nil, err
} }
return nil return &config, nil
} }