golint compliant for app, cmd folders

This commit is contained in:
rigel rozanski 2017-07-05 06:57:52 -04:00
parent 375fad3bec
commit 5044032a23
11 changed files with 48 additions and 101 deletions

View File

@ -45,7 +45,7 @@ type BaseTxPresenter struct {
proofs.RawPresenter // this handles MakeKey as hex bytes
}
// ParseData - parse BaseTxPresenter Data
// ParseData - unmarshal raw bytes to a basecoin tx
func (b BaseTxPresenter) ParseData(raw []byte) (interface{}, error) {
var tx basecoin.Tx
err := wire.ReadBinaryBytes(raw, &tx)

View File

@ -18,7 +18,7 @@ import (
coincmd "github.com/tendermint/basecoin/cmd/basecoin/commands"
)
// BaseCli represents the base command when called without any subcommands
// BaseCli - main basecoin client command
var BaseCli = &cobra.Command{
Use: "basecli",
Short: "Light client for tendermint",
@ -34,16 +34,19 @@ func main() {
commands.AddBasicFlags(BaseCli)
// Prepare queries
pr := proofs.RootCmd
// These are default parsers, but optional in your app (you can remove key)
pr.AddCommand(proofs.TxCmd)
pr.AddCommand(proofs.KeyCmd)
pr.AddCommand(bcmd.AccountQueryCmd)
proofs.RootCmd.AddCommand(
// These are default parsers, but optional in your app (you can remove key)
proofs.TxCmd,
proofs.KeyCmd,
bcmd.AccountQueryCmd,
)
// you will always want this for the base send command
proofs.TxPresenters.Register("base", bcmd.BaseTxPresenter{})
tr := txs.RootCmd
tr.AddCommand(bcmd.SendTxCmd)
txs.RootCmd.AddCommand(
// This is the default transaction, optional in your app
bcmd.SendTxCmd,
)
// Set up the various commands to use
BaseCli.AddCommand(
@ -52,8 +55,8 @@ func main() {
keycmd.RootCmd,
seeds.RootCmd,
rpccmd.RootCmd,
pr,
tr,
proofs.RootCmd,
txs.RootCmd,
proxy.RootCmd,
coincmd.VersionCmd,
bcmd.AutoCompleteCmd,

View File

@ -11,23 +11,21 @@ import (
tcmd "github.com/tendermint/tendermint/cmd/tendermint/commands"
)
//commands
var (
InitCmd = &cobra.Command{
Use: "init [address]",
Short: "Initialize a basecoin blockchain",
RunE: initCmd,
}
)
// InitCmd - node initialization command
var InitCmd = &cobra.Command{
Use: "init [address]",
Short: "Initialize a basecoin blockchain",
RunE: initCmd,
}
//flags
var (
chainIDFlag string
flagChainID string
)
func init() {
flags := []Flag2Register{
{&chainIDFlag, "chain-id", "test_chain_id", "Chain ID"},
{&flagChainID, "chain-id", "test_chain_id", "Chain ID"},
}
RegisterFlags(InitCmd, flags)
}
@ -62,7 +60,7 @@ func initCmd(cmd *cobra.Command, args []string) error {
privValFile := cfg.PrivValidatorFile()
keyFile := path.Join(cfg.RootDir, "key.json")
mod1, err := setupFile(genesisFile, GetGenesisJSON(chainIDFlag, userAddr), 0644)
mod1, err := setupFile(genesisFile, GetGenesisJSON(flagChainID, userAddr), 0644)
if err != nil {
return err
}
@ -85,6 +83,7 @@ func initCmd(cmd *cobra.Command, args []string) error {
return nil
}
// PrivValJSON - validator private key file contents in json
var PrivValJSON = `{
"address": "7A956FADD20D3A5B2375042B2959F8AB172A058F",
"last_height": 0,
@ -134,7 +133,7 @@ func GetGenesisJSON(chainID, addr string) string {
}`, chainID, addr)
}
// TODO: remove this once not needed for relay
// KeyJSON - TODO: remove this once not needed for relay
var KeyJSON = `{
"address": "1B1BE55F969F54064628A63B9559E7C21C925165",
"priv_key": {

View File

@ -19,12 +19,15 @@ import (
//---------------------------------------------
// simple implementation of a key
// Address - public address for a key
type Address [20]byte
// MarshalJSON - marshal the json bytes of the address
func (a Address) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf(`"%x"`, a[:])), nil
}
// UnmarshalJSON - unmarshal the json bytes of the address
func (a *Address) UnmarshalJSON(addrHex []byte) error {
addr, err := hex.DecodeString(strings.Trim(string(addrHex), `"`))
if err != nil {
@ -34,17 +37,19 @@ func (a *Address) UnmarshalJSON(addrHex []byte) error {
return nil
}
// Key - full private key
type Key struct {
Address Address `json:"address"`
PubKey crypto.PubKey `json:"pub_key"`
PrivKey crypto.PrivKey `json:"priv_key"`
}
// Implements Signer
// Sign - Implements Signer
func (k *Key) Sign(msg []byte) crypto.Signature {
return k.PrivKey.Sign(msg)
}
// LoadKey - load key from json file
func LoadKey(keyFile string) (*Key, error) {
filePath := keyFile
@ -61,7 +66,7 @@ func LoadKey(keyFile string) (*Key, error) {
key := new(Key)
err = json.Unmarshal(keyJSONBytes, key)
if err != nil {
return nil, fmt.Errorf("Error reading key from %v: %v\n", filePath, err) //never stack trace
return nil, fmt.Errorf("Error reading key from %v: %v", filePath, err) //never stack trace
}
return key, nil

View File

@ -14,12 +14,12 @@ type plugin struct {
var plugins = []plugin{}
// RegisterStartPlugin is used to enable a plugin
// RegisterStartPlugin - used to enable a plugin
func RegisterStartPlugin(name string, newPlugin func() types.Plugin) {
plugins = append(plugins, plugin{name: name, newPlugin: newPlugin})
}
//Returns a version command based on version input
// QuickVersionCmd - returns a version command based on version input
func QuickVersionCmd(version string) *cobra.Command {
return &cobra.Command{
Use: "version",

View File

@ -6,6 +6,7 @@ import (
tcmd "github.com/tendermint/tendermint/cmd/tendermint/commands"
)
// UnsafeResetAllCmd - extension of the tendermint command, resets initialization
var UnsafeResetAllCmd = &cobra.Command{
Use: "unsafe_reset_all",
Short: "Reset all blockchain data",

View File

@ -11,6 +11,7 @@ import (
"github.com/tendermint/tmlibs/log"
)
//nolint
const (
defaultLogLevel = "error"
FlagLogLevel = "log_level"
@ -20,6 +21,7 @@ var (
logger = log.NewTMLogger(log.NewSyncWriter(os.Stdout)).With("module", "main")
)
// RootCmd - main node command
var RootCmd = &cobra.Command{
Use: "basecoin",
Short: "A cryptocurrency framework in Golang based on Tendermint-Core",

View File

@ -24,13 +24,14 @@ import (
"github.com/tendermint/basecoin/app"
)
// StartCmd - command to start running the basecoin node!
var StartCmd = &cobra.Command{
Use: "start",
Short: "Start basecoin",
RunE: startCmd,
}
// TODO: move to config file
// nolint TODO: move to config file
const EyesCacheSize = 10000
//nolint
@ -41,7 +42,7 @@ const (
)
var (
// use a global to store the handler, so we can set it in main.
// Handler - use a global to store the handler, so we can set it in main.
// TODO: figure out a cleaner way to register plugins
Handler basecoin.Handler
)
@ -95,11 +96,10 @@ func startCmd(cmd *cobra.Command, args []string) error {
logger.Info("Starting Basecoin without Tendermint", "chain_id", chainID)
// run just the abci app/server
return startBasecoinABCI(basecoinApp)
} else {
logger.Info("Starting Basecoin with Tendermint", "chain_id", chainID)
// start the app with tendermint in-process
return startTendermint(rootDir, basecoinApp)
}
logger.Info("Starting Basecoin with Tendermint", "chain_id", chainID)
// start the app with tendermint in-process
return startTendermint(rootDir, basecoinApp)
}
func startBasecoinABCI(basecoinApp *app.Basecoin) error {

View File

@ -5,8 +5,6 @@ import (
"fmt"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
abci "github.com/tendermint/abci/types"
wire "github.com/tendermint/go-wire"
@ -17,68 +15,6 @@ import (
tmtypes "github.com/tendermint/tendermint/types"
)
//Quickly registering flags can be quickly achieved through using the utility functions
//RegisterFlags, and RegisterPersistentFlags. Ex:
// flags := []Flag2Register{
// {&myStringFlag, "mystringflag", "foobar", "description of what this flag does"},
// {&myBoolFlag, "myboolflag", false, "description of what this flag does"},
// {&myInt64Flag, "myintflag", 333, "description of what this flag does"},
// }
// RegisterFlags(MyCobraCmd, flags)
type Flag2Register struct {
Pointer interface{}
Use string
Value interface{}
Desc string
}
//register flag utils
func RegisterFlags(c *cobra.Command, flags []Flag2Register) {
registerFlags(c, flags, false)
}
func RegisterPersistentFlags(c *cobra.Command, flags []Flag2Register) {
registerFlags(c, flags, true)
}
func registerFlags(c *cobra.Command, flags []Flag2Register, persistent bool) {
var flagset *pflag.FlagSet
if persistent {
flagset = c.PersistentFlags()
} else {
flagset = c.Flags()
}
for _, f := range flags {
ok := false
switch f.Value.(type) {
case string:
if _, ok = f.Pointer.(*string); ok {
flagset.StringVar(f.Pointer.(*string), f.Use, f.Value.(string), f.Desc)
}
case int:
if _, ok = f.Pointer.(*int); ok {
flagset.IntVar(f.Pointer.(*int), f.Use, f.Value.(int), f.Desc)
}
case uint64:
if _, ok = f.Pointer.(*uint64); ok {
flagset.Uint64Var(f.Pointer.(*uint64), f.Use, f.Value.(uint64), f.Desc)
}
case bool:
if _, ok = f.Pointer.(*bool); ok {
flagset.BoolVar(f.Pointer.(*bool), f.Use, f.Value.(bool), f.Desc)
}
}
if !ok {
panic("improper use of RegisterFlags")
}
}
}
// Returns true for non-empty hex-string prefixed with "0x"
func isHex(s string) bool {
if len(s) > 2 && s[:2] == "0x" {
@ -91,6 +27,7 @@ func isHex(s string) bool {
return false
}
// StripHex remove the first two hex bytes
func StripHex(s string) string {
if isHex(s) {
return s[2:]
@ -98,6 +35,7 @@ func StripHex(s string) string {
return s
}
// Query - send an abci query
func Query(tmAddr string, key []byte) (*abci.ResultQuery, error) {
httpClient := client.NewHTTP(tmAddr, "/websocket")
return queryWithClient(httpClient, key)

View File

@ -8,6 +8,7 @@ import (
"github.com/tendermint/basecoin/version"
)
// VersionCmd - command to show the application version
var VersionCmd = &cobra.Command{
Use: "version",
Short: "Show version info",

View File

@ -16,13 +16,11 @@ func main() {
rt.AddCommand(
commands.InitCmd,
commands.StartCmd,
// commands.RelayCmd,
//commands.RelayCmd,
commands.UnsafeResetAllCmd,
commands.VersionCmd,
)
cmd := cli.PrepareMainCmd(rt, "BC", os.ExpandEnv("$HOME/.basecoin"))
if err := cmd.Execute(); err != nil {
os.Exit(1)
}
cmd.Execute()
}