From 5044032a23d89e2ea7811a39876abd7aba169eb5 Mon Sep 17 00:00:00 2001 From: rigel rozanski Date: Wed, 5 Jul 2017 06:57:52 -0400 Subject: [PATCH] golint compliant for app, cmd folders --- cmd/basecli/commands/query.go | 2 +- cmd/basecli/main.go | 23 +++++----- cmd/basecoin/commands/init.go | 23 +++++----- cmd/basecoin/commands/key.go | 9 +++- cmd/basecoin/commands/plugin_util.go | 4 +- cmd/basecoin/commands/reset.go | 1 + cmd/basecoin/commands/root.go | 2 + cmd/basecoin/commands/start.go | 12 ++--- cmd/basecoin/commands/utils.go | 66 +--------------------------- cmd/basecoin/commands/version.go | 1 + cmd/basecoin/main.go | 6 +-- 11 files changed, 48 insertions(+), 101 deletions(-) diff --git a/cmd/basecli/commands/query.go b/cmd/basecli/commands/query.go index e497c798dd..d5728d05e2 100644 --- a/cmd/basecli/commands/query.go +++ b/cmd/basecli/commands/query.go @@ -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) diff --git a/cmd/basecli/main.go b/cmd/basecli/main.go index 4f78f792f1..4d96fc3f08 100644 --- a/cmd/basecli/main.go +++ b/cmd/basecli/main.go @@ -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, diff --git a/cmd/basecoin/commands/init.go b/cmd/basecoin/commands/init.go index b0b6a3fc28..58688a1504 100644 --- a/cmd/basecoin/commands/init.go +++ b/cmd/basecoin/commands/init.go @@ -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": { diff --git a/cmd/basecoin/commands/key.go b/cmd/basecoin/commands/key.go index 08f54e0a42..b5b03e3fba 100644 --- a/cmd/basecoin/commands/key.go +++ b/cmd/basecoin/commands/key.go @@ -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 diff --git a/cmd/basecoin/commands/plugin_util.go b/cmd/basecoin/commands/plugin_util.go index d4f47b323a..ee56d7b920 100644 --- a/cmd/basecoin/commands/plugin_util.go +++ b/cmd/basecoin/commands/plugin_util.go @@ -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", diff --git a/cmd/basecoin/commands/reset.go b/cmd/basecoin/commands/reset.go index 00c3d71718..b982fe0230 100644 --- a/cmd/basecoin/commands/reset.go +++ b/cmd/basecoin/commands/reset.go @@ -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", diff --git a/cmd/basecoin/commands/root.go b/cmd/basecoin/commands/root.go index bdcc25d728..5d76c83e5d 100644 --- a/cmd/basecoin/commands/root.go +++ b/cmd/basecoin/commands/root.go @@ -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", diff --git a/cmd/basecoin/commands/start.go b/cmd/basecoin/commands/start.go index 791e810a62..4fd2f19b2c 100644 --- a/cmd/basecoin/commands/start.go +++ b/cmd/basecoin/commands/start.go @@ -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 { diff --git a/cmd/basecoin/commands/utils.go b/cmd/basecoin/commands/utils.go index dc0b5f1620..a039f00146 100644 --- a/cmd/basecoin/commands/utils.go +++ b/cmd/basecoin/commands/utils.go @@ -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) diff --git a/cmd/basecoin/commands/version.go b/cmd/basecoin/commands/version.go index 1cbf891e43..fe72d26ca5 100644 --- a/cmd/basecoin/commands/version.go +++ b/cmd/basecoin/commands/version.go @@ -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", diff --git a/cmd/basecoin/main.go b/cmd/basecoin/main.go index 51c8ab4917..2185262b73 100644 --- a/cmd/basecoin/main.go +++ b/cmd/basecoin/main.go @@ -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() }