From de2082577955ec29ed02208737b59c0f6f992448 Mon Sep 17 00:00:00 2001 From: Sridhar Ganesan Date: Mon, 16 Jul 2018 19:32:56 +0200 Subject: [PATCH 01/31] Adding cosmos-sdk-cli - initial version --- cmd/cosmos-sdk-cli/cmd/init.go | 76 ++++++++++ cmd/cosmos-sdk-cli/cmd/root.go | 23 +++ cmd/cosmos-sdk-cli/main.go | 9 ++ cmd/cosmos-sdk-cli/template/Gopkg.toml | 58 ++++++++ cmd/cosmos-sdk-cli/template/Makefile | 23 +++ cmd/cosmos-sdk-cli/template/app/app.go | 144 +++++++++++++++++++ cmd/cosmos-sdk-cli/template/cmd/cli/main.go | 77 ++++++++++ cmd/cosmos-sdk-cli/template/cmd/node/main.go | 68 +++++++++ cmd/cosmos-sdk-cli/template/types/account.go | 78 ++++++++++ 9 files changed, 556 insertions(+) create mode 100644 cmd/cosmos-sdk-cli/cmd/init.go create mode 100644 cmd/cosmos-sdk-cli/cmd/root.go create mode 100644 cmd/cosmos-sdk-cli/main.go create mode 100644 cmd/cosmos-sdk-cli/template/Gopkg.toml create mode 100644 cmd/cosmos-sdk-cli/template/Makefile create mode 100644 cmd/cosmos-sdk-cli/template/app/app.go create mode 100644 cmd/cosmos-sdk-cli/template/cmd/cli/main.go create mode 100644 cmd/cosmos-sdk-cli/template/cmd/node/main.go create mode 100644 cmd/cosmos-sdk-cli/template/types/account.go diff --git a/cmd/cosmos-sdk-cli/cmd/init.go b/cmd/cosmos-sdk-cli/cmd/init.go new file mode 100644 index 0000000000..eb72f6d943 --- /dev/null +++ b/cmd/cosmos-sdk-cli/cmd/init.go @@ -0,0 +1,76 @@ +package cmd + +import ( + "bufio" + "fmt" + "go/build" + "io/ioutil" + "os" + "strings" + "time" + + "github.com/gobuffalo/packr" + "github.com/pkg/errors" + "github.com/spf13/cobra" +) + +func init() { + rootCmd.AddCommand(initCmd) +} + +func resolveProjectPath(remoteProjectPath string) string { + gopath := os.Getenv("GOPATH") + if gopath == "" { + gopath = build.Default.GOPATH + // Use $HOME/go + } + return gopath + string(os.PathSeparator) + "src" + string(os.PathSeparator) + remoteProjectPath +} + +var initCmd = &cobra.Command{ + Use: "init AwesomeProjectName", + Short: "Initialize your new cosmos zone", + RunE: func(cmd *cobra.Command, args []string) error { + if len(args) < 1 { + return errors.New("Project name is required") + } + projectName := args[0] + capitalizedProjectName := strings.Title(projectName) + shortProjectName := strings.ToLower(projectName) + reader := bufio.NewReader(os.Stdin) + fmt.Println("Thank you for using cosmos-zone tool.") + fmt.Println("You are only a few steps away from creating your brand new blockchain project on Cosmos.") + fmt.Print("We will ask you a few more questions to guide you through this process\n\n") + fmt.Print("To configure this project we need a remote project path. If you are unsure you can leave this empty. ") + fmt.Print("Remote project path is usually something like github.com/your_user_name/project_name\n") + fmt.Print("Enter remote project path: ") + remoteProjectPath, _ := reader.ReadString('\n') + remoteProjectPath = strings.ToLower(strings.TrimSpace(remoteProjectPath)) + if remoteProjectPath == "" { + remoteProjectPath = strings.ToLower(shortProjectName) + } + projectPath := resolveProjectPath(remoteProjectPath) + fmt.Print("configuring the project in " + projectPath + "\n\n") + time.Sleep(2 * time.Second) + box := packr.NewBox("../template") + var replacer = strings.NewReplacer("_CAPITALIZED_PROJECT_SHORT_NAME_", capitalizedProjectName, "_PROJECT_SHORT_NAME_", shortProjectName, "_REMOTE_PROJECT_PATH_", remoteProjectPath) + box.Walk(func(path string, file packr.File) error { + actualPath := replacer.Replace(path) + fmt.Println("Creating file: " + actualPath) + contents := box.String(path) + contents = replacer.Replace(contents) + lastIndex := strings.LastIndex(actualPath, string(os.PathSeparator)) + rootDir := "" + if lastIndex != -1 { + rootDir = actualPath[0:lastIndex] + } + // Create directory + os.MkdirAll(projectPath+string(os.PathSeparator)+rootDir, os.ModePerm) + filePath := projectPath + string(os.PathSeparator) + actualPath + ioutil.WriteFile(filePath, []byte(contents), os.ModePerm) + return nil + }) + fmt.Println("Initialized a new project at " + projectPath + ". Happy hacking!") + return nil + }, +} diff --git a/cmd/cosmos-sdk-cli/cmd/root.go b/cmd/cosmos-sdk-cli/cmd/root.go new file mode 100644 index 0000000000..c05234701c --- /dev/null +++ b/cmd/cosmos-sdk-cli/cmd/root.go @@ -0,0 +1,23 @@ +package cmd + +import ( + "fmt" + "os" + + "github.com/spf13/cobra" +) + +var rootCmd = &cobra.Command{ + Use: "cosmos-zone", + Short: "Tools to develop on cosmos-sdk", + Run: func(cmd *cobra.Command, args []string) { + // Do Stuff Here + }, +} + +func Execute() { + if err := rootCmd.Execute(); err != nil { + fmt.Println(err) + os.Exit(1) + } +} diff --git a/cmd/cosmos-sdk-cli/main.go b/cmd/cosmos-sdk-cli/main.go new file mode 100644 index 0000000000..57dcb505b8 --- /dev/null +++ b/cmd/cosmos-sdk-cli/main.go @@ -0,0 +1,9 @@ +package main + +import ( + "github.com/cosmos/cosmos-sdk/cmd/cosmos-sdk-cli/cmd" +) + +func main() { + cmd.Execute() +} diff --git a/cmd/cosmos-sdk-cli/template/Gopkg.toml b/cmd/cosmos-sdk-cli/template/Gopkg.toml new file mode 100644 index 0000000000..d4c3b83520 --- /dev/null +++ b/cmd/cosmos-sdk-cli/template/Gopkg.toml @@ -0,0 +1,58 @@ +# Gopkg.toml example +# +# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md +# for detailed Gopkg.toml documentation. +# +# required = ["github.com/user/thing/cmd/thing"] +# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"] +# +# [[constraint]] +# name = "github.com/user/project" +# version = "1.0.0" +# +# [[constraint]] +# name = "github.com/user/project2" +# branch = "dev" +# source = "github.com/myfork/project2" +# +# [[override]] +# name = "github.com/x/y" +# version = "2.4.0" +# +# [prune] +# non-go = false +# go-tests = true +# unused-packages = true + +[[constraint]] + name = "github.com/pkg/errors" + version = "~0.8.0" + +[[constraint]] + name = "github.com/spf13/cobra" + version = "~0.0.1" + +[[constraint]] + name = "github.com/spf13/viper" + version = "~1.0.0" + +[[constraint]] + name = "github.com/stretchr/testify" + version = "~1.2.1" + +[[constraint]] + version = "~0.10.0" + source = "github.com/tendermint/go-amino" + name = "github.com/tendermint/go-wire" + +[[constraint]] + version = "~0.20.0" + name = "github.com/cosmos/cosmos-sdk" + +[[override]] + version = "=1.1.0" + name = "github.com/golang/protobuf" + +[prune] + go-tests = true + unused-packages = true diff --git a/cmd/cosmos-sdk-cli/template/Makefile b/cmd/cosmos-sdk-cli/template/Makefile new file mode 100644 index 0000000000..33ef7263b3 --- /dev/null +++ b/cmd/cosmos-sdk-cli/template/Makefile @@ -0,0 +1,23 @@ +PACKAGES=$(shell go list ./... | grep -v '/vendor/') +#BUILD_FLAGS = -ldflags "-X _REMOTE_PROJECT_PATH_/version.GitCommit=`git rev-parse --short HEAD`" + +all: get_tools get_vendor_deps build test + +get_tools: + go get github.com/golang/dep/cmd/dep + +build: + go build -o bin/_PROJECT_SHORT_NAME_cli cmd/cli/main.go && go build -o bin/_PROJECT_SHORT_NAME_d cmd/node/main.go + +get_vendor_deps: + @rm -rf vendor/ + @dep ensure + +test: + @go test $(PACKAGES) + +benchmark: + @go test -bench=. $(PACKAGES) + +.PHONY: all build test benchmark + diff --git a/cmd/cosmos-sdk-cli/template/app/app.go b/cmd/cosmos-sdk-cli/template/app/app.go new file mode 100644 index 0000000000..7c2df96f22 --- /dev/null +++ b/cmd/cosmos-sdk-cli/template/app/app.go @@ -0,0 +1,144 @@ +package app + +import ( + "encoding/json" + + abci "github.com/tendermint/tendermint/abci/types" + cmn "github.com/tendermint/tendermint/libs/common" + dbm "github.com/tendermint/tendermint/libs/db" + "github.com/tendermint/tendermint/libs/log" + tmtypes "github.com/tendermint/tendermint/types" + + bam "github.com/cosmos/cosmos-sdk/baseapp" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/wire" + "github.com/cosmos/cosmos-sdk/x/auth" + "github.com/cosmos/cosmos-sdk/x/bank" + + "_REMOTE_PROJECT_PATH_/types" +) + +const ( + appName = "_CAPITALIZED_PROJECT_SHORT_NAME_App" +) + +// Extended ABCI application +type _CAPITALIZED_PROJECT_SHORT_NAME_App struct { + *bam.BaseApp + cdc *wire.Codec + + // keys to access the substores + capKeyMainStore *sdk.KVStoreKey + capKeyAccountStore *sdk.KVStoreKey + + // keepers + feeCollectionKeeper auth.FeeCollectionKeeper + coinKeeper bank.Keeper + + // Manage getting and setting accounts + accountMapper auth.AccountMapper +} + +func New_CAPITALIZED_PROJECT_SHORT_NAME_App(logger log.Logger, db dbm.DB) *_CAPITALIZED_PROJECT_SHORT_NAME_App { + + // Create app-level codec for txs and accounts. + var cdc = MakeCodec() + + // Create your application object. + var app = &_CAPITALIZED_PROJECT_SHORT_NAME_App{ + BaseApp: bam.NewBaseApp(appName, cdc, logger, db), + cdc: cdc, + capKeyMainStore: sdk.NewKVStoreKey("main"), + capKeyAccountStore: sdk.NewKVStoreKey("acc"), + } + + // Define the accountMapper. + app.accountMapper = auth.NewAccountMapper( + cdc, + app.capKeyAccountStore, // target store + types.ProtoAppAccount(), // prototype + ) + + // Add handlers. + app.coinKeeper = bank.NewKeeper(app.accountMapper) + app.Router(). + AddRoute("bank", bank.NewHandler(app.coinKeeper)) + + // Initialize BaseApp. + app.SetInitChainer(app.initChainerFn()) + app.MountStoresIAVL(app.capKeyMainStore, app.capKeyAccountStore) + app.SetAnteHandler(auth.NewAnteHandler(app.accountMapper, app.feeCollectionKeeper)) + err := app.LoadLatestVersion(app.capKeyMainStore) + if err != nil { + cmn.Exit(err.Error()) + } + return app +} + +// custom tx codec +func MakeCodec() *wire.Codec { + var cdc = wire.NewCodec() + wire.RegisterCrypto(cdc) // Register crypto. + sdk.RegisterWire(cdc) // Register Msgs + bank.RegisterWire(cdc) + + // Register AppAccount + cdc.RegisterInterface((*auth.Account)(nil), nil) + cdc.RegisterConcrete(&types.AppAccount{}, "_PROJECT_SHORT_NAME_/Account", nil) + + cdc.Seal() + + return cdc +} + +// custom logic for _PROJECT_SHORT_NAME_ initialization +// nolint: unparam +func (app *_CAPITALIZED_PROJECT_SHORT_NAME_App) initChainerFn() sdk.InitChainer { + return func(ctx sdk.Context, req abci.RequestInitChain) abci.ResponseInitChain { + stateJSON := req.AppStateBytes + + genesisState := new(types.GenesisState) + err := app.cdc.UnmarshalJSON(stateJSON, genesisState) + if err != nil { + panic(err) // TODO https://github.com/cosmos/cosmos-sdk/issues/468 + // return sdk.ErrGenesisParse("").TraceCause(err, "") + } + + for _, gacc := range genesisState.Accounts { + acc, err := gacc.ToAppAccount() + if err != nil { + panic(err) // TODO https://github.com/cosmos/cosmos-sdk/issues/468 + // return sdk.ErrGenesisParse("").TraceCause(err, "") + } + app.accountMapper.SetAccount(ctx, acc) + } + + return abci.ResponseInitChain{} + } +} + +// Custom logic for state export +func (app *_CAPITALIZED_PROJECT_SHORT_NAME_App) ExportAppStateAndValidators() (appState json.RawMessage, validators []tmtypes.GenesisValidator, err error) { + ctx := app.NewContext(true, abci.Header{}) + + // iterate to get the accounts + accounts := []*types.GenesisAccount{} + appendAccount := func(acc auth.Account) (stop bool) { + account := &types.GenesisAccount{ + Address: acc.GetAddress(), + Coins: acc.GetCoins(), + } + accounts = append(accounts, account) + return false + } + app.accountMapper.IterateAccounts(ctx, appendAccount) + + genState := types.GenesisState{ + Accounts: accounts, + } + appState, err = wire.MarshalJSONIndent(app.cdc, genState) + if err != nil { + return nil, nil, err + } + return appState, validators, nil +} diff --git a/cmd/cosmos-sdk-cli/template/cmd/cli/main.go b/cmd/cosmos-sdk-cli/template/cmd/cli/main.go new file mode 100644 index 0000000000..8d03bf8255 --- /dev/null +++ b/cmd/cosmos-sdk-cli/template/cmd/cli/main.go @@ -0,0 +1,77 @@ +package main + +import ( + "os" + + "github.com/spf13/cobra" + + "github.com/tendermint/tendermint/libs/cli" + + "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/client/keys" + "github.com/cosmos/cosmos-sdk/client/lcd" + "github.com/cosmos/cosmos-sdk/client/rpc" + "github.com/cosmos/cosmos-sdk/client/tx" + + "github.com/cosmos/cosmos-sdk/version" + authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli" + bankcmd "github.com/cosmos/cosmos-sdk/x/bank/client/cli" + + "_REMOTE_PROJECT_PATH_/app" + "_REMOTE_PROJECT_PATH_/types" +) + +// rootCmd is the entry point for this binary +var ( + rootCmd = &cobra.Command{ + Use: "_PROJECT_SHORT_NAME_cli", + Short: "_CAPITALIZED_PROJECT_SHORT_NAME_ light-client", + } +) + +func main() { + // disable sorting + cobra.EnableCommandSorting = false + + // get the codec + cdc := app.MakeCodec() + + // TODO: setup keybase, viper object, etc. to be passed into + // the below functions and eliminate global vars, like we do + // with the cdc + + // add standard rpc, and tx commands + rpc.AddCommands(rootCmd) + rootCmd.AddCommand(client.LineBreak) + tx.AddCommands(rootCmd, cdc) + rootCmd.AddCommand(client.LineBreak) + + // add query/post commands (custom to binary) + // start with commands common to basecoin + rootCmd.AddCommand( + client.GetCommands( + authcmd.GetAccountCmd("acc", cdc, types.GetAccountDecoder(cdc)), + )...) + rootCmd.AddCommand( + client.PostCommands( + bankcmd.SendTxCmd(cdc), + )...) + // and now _PROJECT_SHORT_NAME_ specific commands here + + // add proxy, version and key info + rootCmd.AddCommand( + client.LineBreak, + lcd.ServeCommand(cdc), + keys.Commands(), + client.LineBreak, + version.VersionCmd, + ) + + // prepare and add flags + executor := cli.PrepareMainCmd(rootCmd, "BC", os.ExpandEnv("$HOME/._PROJECT_SHORT_NAME_cli")) + err := executor.Execute() + if err != nil { + // handle with #870 + panic(err) + } +} diff --git a/cmd/cosmos-sdk-cli/template/cmd/node/main.go b/cmd/cosmos-sdk-cli/template/cmd/node/main.go new file mode 100644 index 0000000000..edc20449e4 --- /dev/null +++ b/cmd/cosmos-sdk-cli/template/cmd/node/main.go @@ -0,0 +1,68 @@ +package main + +import ( + "encoding/json" + "os" + + "github.com/spf13/cobra" + + abci "github.com/tendermint/tendermint/abci/types" + "github.com/tendermint/tendermint/libs/cli" + dbm "github.com/tendermint/tendermint/libs/db" + "github.com/tendermint/tendermint/libs/log" + tmtypes "github.com/tendermint/tendermint/types" + + "_REMOTE_PROJECT_PATH_/app" + + "github.com/cosmos/cosmos-sdk/server" + "github.com/cosmos/cosmos-sdk/wire" +) + +// init parameters +var _CAPITALIZED_PROJECT_SHORT_NAME_AppInit = server.AppInit{ + AppGenState: _CAPITALIZED_PROJECT_SHORT_NAME_AppGenState, + AppGenTx: server.SimpleAppGenTx, +} + +// GenAppParams sets up the app_state, append any other app specific components. +func _CAPITALIZED_PROJECT_SHORT_NAME_AppGenState(cdc *wire.Codec, appGenTxs []json.RawMessage) (appState json.RawMessage, err error) { + appState, err = server.SimpleAppGenState(cdc, appGenTxs) + if err != nil { + return + } + + return +} + +func newApp(logger log.Logger, db dbm.DB) abci.Application { + return app.New_CAPITALIZED_PROJECT_SHORT_NAME_App(logger, db) +} + +func exportAppStateAndTMValidators(logger log.Logger, db dbm.DB) (json.RawMessage, []tmtypes.GenesisValidator, error) { + dapp := app.New_CAPITALIZED_PROJECT_SHORT_NAME_App(logger, db) + return dapp.ExportAppStateAndValidators() +} + +func main() { + cdc := app.MakeCodec() + ctx := server.NewDefaultContext() + + rootCmd := &cobra.Command{ + Use: "_PROJECT_SHORT_NAME_d", + Short: "_CAPITALIZED_PROJECT_SHORT_NAME_ Daemon (server)", + PersistentPreRunE: server.PersistentPreRunEFn(ctx), + } + + server.AddCommands(ctx, cdc, rootCmd, _CAPITALIZED_PROJECT_SHORT_NAME_AppInit, + server.ConstructAppCreator(newApp, "_PROJECT_SHORT_NAME_"), + server.ConstructAppExporter(exportAppStateAndTMValidators, "_PROJECT_SHORT_NAME_")) + + // prepare and add flags + rootDir := os.ExpandEnv("$HOME/._PROJECT_SHORT_NAME_d") + executor := cli.PrepareBaseCmd(rootCmd, "BC", rootDir) + err := executor.Execute() + if err != nil { + // handle with #870 + panic(err) + } +} diff --git a/cmd/cosmos-sdk-cli/template/types/account.go b/cmd/cosmos-sdk-cli/template/types/account.go new file mode 100644 index 0000000000..d24ac045f3 --- /dev/null +++ b/cmd/cosmos-sdk-cli/template/types/account.go @@ -0,0 +1,78 @@ +package types + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/wire" + "github.com/cosmos/cosmos-sdk/x/auth" + +) + +var _ auth.Account = (*AppAccount)(nil) + +// Custom extensions for this application. This is just an example of +// extending auth.BaseAccount with custom fields. +// +// This is compatible with the stock auth.AccountStore, since +// auth.AccountStore uses the flexible go-amino library. +type AppAccount struct { + auth.BaseAccount + Name string `json:"name"` +} + +// Constructor for AppAccount +func ProtoAppAccount() auth.Account { + return &AppAccount{} +} + +// nolint +func (acc AppAccount) GetName() string { return acc.Name } +func (acc *AppAccount) SetName(name string) { acc.Name = name } + +// Get the AccountDecoder function for the custom AppAccount +func GetAccountDecoder(cdc *wire.Codec) auth.AccountDecoder { + return func(accBytes []byte) (res auth.Account, err error) { + if len(accBytes) == 0 { + return nil, sdk.ErrTxDecode("accBytes are empty") + } + acct := new(AppAccount) + err = cdc.UnmarshalBinaryBare(accBytes, &acct) + if err != nil { + panic(err) + } + return acct, err + } +} + +//___________________________________________________________________________________ + +// State to Unmarshal +type GenesisState struct { + Accounts []*GenesisAccount `json:"accounts"` +} + +// GenesisAccount doesn't need pubkey or sequence +type GenesisAccount struct { + Name string `json:"name"` + Address sdk.AccAddress `json:"address"` + Coins sdk.Coins `json:"coins"` +} + +func NewGenesisAccount(aa *AppAccount) *GenesisAccount { + return &GenesisAccount{ + Name: aa.Name, + Address: aa.Address, + Coins: aa.Coins.Sort(), + } +} + +// convert GenesisAccount to AppAccount +func (ga *GenesisAccount) ToAppAccount() (acc *AppAccount, err error) { + baseAcc := auth.BaseAccount{ + Address: ga.Address, + Coins: ga.Coins.Sort(), + } + return &AppAccount{ + BaseAccount: baseAcc, + Name: ga.Name, + }, nil +} From 26cf09dfa2caf0ca6e6f08aad2267075c745b99a Mon Sep 17 00:00:00 2001 From: Sridhar Ganesan Date: Mon, 16 Jul 2018 20:07:05 +0200 Subject: [PATCH 02/31] Modifying the _REMOTE_PROJECT_PATH, _PROJECT_SHORT_NAME_, _CAPITALIZED_PROJECT_SHORT_NAME_ to actual names and packages --- cmd/cosmos-sdk-cli/cmd/init.go | 2 +- cmd/cosmos-sdk-cli/template/Makefile | 4 ++-- cmd/cosmos-sdk-cli/template/app/app.go | 18 +++++++-------- cmd/cosmos-sdk-cli/template/cmd/cli/main.go | 12 +++++----- cmd/cosmos-sdk-cli/template/cmd/node/main.go | 24 ++++++++++---------- 5 files changed, 30 insertions(+), 30 deletions(-) diff --git a/cmd/cosmos-sdk-cli/cmd/init.go b/cmd/cosmos-sdk-cli/cmd/init.go index eb72f6d943..f5ded2215a 100644 --- a/cmd/cosmos-sdk-cli/cmd/init.go +++ b/cmd/cosmos-sdk-cli/cmd/init.go @@ -53,7 +53,7 @@ var initCmd = &cobra.Command{ fmt.Print("configuring the project in " + projectPath + "\n\n") time.Sleep(2 * time.Second) box := packr.NewBox("../template") - var replacer = strings.NewReplacer("_CAPITALIZED_PROJECT_SHORT_NAME_", capitalizedProjectName, "_PROJECT_SHORT_NAME_", shortProjectName, "_REMOTE_PROJECT_PATH_", remoteProjectPath) + var replacer = strings.NewReplacer("MyAwesomeProject", capitalizedProjectName, "myawesomeproject", shortProjectName, "github.com/cosmos/cosmos-sdk/cmd/cosmos-sdk-cli", remoteProjectPath) box.Walk(func(path string, file packr.File) error { actualPath := replacer.Replace(path) fmt.Println("Creating file: " + actualPath) diff --git a/cmd/cosmos-sdk-cli/template/Makefile b/cmd/cosmos-sdk-cli/template/Makefile index 33ef7263b3..8d7f55706d 100644 --- a/cmd/cosmos-sdk-cli/template/Makefile +++ b/cmd/cosmos-sdk-cli/template/Makefile @@ -1,5 +1,5 @@ PACKAGES=$(shell go list ./... | grep -v '/vendor/') -#BUILD_FLAGS = -ldflags "-X _REMOTE_PROJECT_PATH_/version.GitCommit=`git rev-parse --short HEAD`" +#BUILD_FLAGS = -ldflags "-X github.com/cosmos/cosmos-sdk/cmd/cosmos-sdk-cli/version.GitCommit=`git rev-parse --short HEAD`" all: get_tools get_vendor_deps build test @@ -7,7 +7,7 @@ get_tools: go get github.com/golang/dep/cmd/dep build: - go build -o bin/_PROJECT_SHORT_NAME_cli cmd/cli/main.go && go build -o bin/_PROJECT_SHORT_NAME_d cmd/node/main.go + go build -o bin/myawesomeprojectcli cmd/cli/main.go && go build -o bin/_PROJECT_SHORT_NAME_d cmd/node/main.go get_vendor_deps: @rm -rf vendor/ diff --git a/cmd/cosmos-sdk-cli/template/app/app.go b/cmd/cosmos-sdk-cli/template/app/app.go index 7c2df96f22..bc536101ce 100644 --- a/cmd/cosmos-sdk-cli/template/app/app.go +++ b/cmd/cosmos-sdk-cli/template/app/app.go @@ -15,15 +15,15 @@ import ( "github.com/cosmos/cosmos-sdk/x/auth" "github.com/cosmos/cosmos-sdk/x/bank" - "_REMOTE_PROJECT_PATH_/types" + "github.com/cosmos/cosmos-sdk/cmd/cosmos-sdk-cli/types" ) const ( - appName = "_CAPITALIZED_PROJECT_SHORT_NAME_App" + appName = "MyAwesomeProjectApp" ) // Extended ABCI application -type _CAPITALIZED_PROJECT_SHORT_NAME_App struct { +type MyAwesomeProjectApp struct { *bam.BaseApp cdc *wire.Codec @@ -39,13 +39,13 @@ type _CAPITALIZED_PROJECT_SHORT_NAME_App struct { accountMapper auth.AccountMapper } -func New_CAPITALIZED_PROJECT_SHORT_NAME_App(logger log.Logger, db dbm.DB) *_CAPITALIZED_PROJECT_SHORT_NAME_App { +func NewMyAwesomeProjectApp(logger log.Logger, db dbm.DB) *MyAwesomeProjectApp { // Create app-level codec for txs and accounts. var cdc = MakeCodec() // Create your application object. - var app = &_CAPITALIZED_PROJECT_SHORT_NAME_App{ + var app = &MyAwesomeProjectApp{ BaseApp: bam.NewBaseApp(appName, cdc, logger, db), cdc: cdc, capKeyMainStore: sdk.NewKVStoreKey("main"), @@ -84,16 +84,16 @@ func MakeCodec() *wire.Codec { // Register AppAccount cdc.RegisterInterface((*auth.Account)(nil), nil) - cdc.RegisterConcrete(&types.AppAccount{}, "_PROJECT_SHORT_NAME_/Account", nil) + cdc.RegisterConcrete(&types.AppAccount{}, "myawesomeproject/Account", nil) cdc.Seal() return cdc } -// custom logic for _PROJECT_SHORT_NAME_ initialization +// custom logic for myawesomeproject initialization // nolint: unparam -func (app *_CAPITALIZED_PROJECT_SHORT_NAME_App) initChainerFn() sdk.InitChainer { +func (app *MyAwesomeProjectApp) initChainerFn() sdk.InitChainer { return func(ctx sdk.Context, req abci.RequestInitChain) abci.ResponseInitChain { stateJSON := req.AppStateBytes @@ -118,7 +118,7 @@ func (app *_CAPITALIZED_PROJECT_SHORT_NAME_App) initChainerFn() sdk.InitChainer } // Custom logic for state export -func (app *_CAPITALIZED_PROJECT_SHORT_NAME_App) ExportAppStateAndValidators() (appState json.RawMessage, validators []tmtypes.GenesisValidator, err error) { +func (app *MyAwesomeProjectApp) ExportAppStateAndValidators() (appState json.RawMessage, validators []tmtypes.GenesisValidator, err error) { ctx := app.NewContext(true, abci.Header{}) // iterate to get the accounts diff --git a/cmd/cosmos-sdk-cli/template/cmd/cli/main.go b/cmd/cosmos-sdk-cli/template/cmd/cli/main.go index 8d03bf8255..33a888f3ba 100644 --- a/cmd/cosmos-sdk-cli/template/cmd/cli/main.go +++ b/cmd/cosmos-sdk-cli/template/cmd/cli/main.go @@ -17,15 +17,15 @@ import ( authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli" bankcmd "github.com/cosmos/cosmos-sdk/x/bank/client/cli" - "_REMOTE_PROJECT_PATH_/app" - "_REMOTE_PROJECT_PATH_/types" + "github.com/cosmos/cosmos-sdk/cmd/cosmos-sdk-cli/app" + "github.com/cosmos/cosmos-sdk/cmd/cosmos-sdk-cli/types" ) // rootCmd is the entry point for this binary var ( rootCmd = &cobra.Command{ - Use: "_PROJECT_SHORT_NAME_cli", - Short: "_CAPITALIZED_PROJECT_SHORT_NAME_ light-client", + Use: "myawesomeprojectcli", + Short: "MyAwesomeProject light-client", } ) @@ -56,7 +56,7 @@ func main() { client.PostCommands( bankcmd.SendTxCmd(cdc), )...) - // and now _PROJECT_SHORT_NAME_ specific commands here + // and now myawesomeproject specific commands here // add proxy, version and key info rootCmd.AddCommand( @@ -68,7 +68,7 @@ func main() { ) // prepare and add flags - executor := cli.PrepareMainCmd(rootCmd, "BC", os.ExpandEnv("$HOME/._PROJECT_SHORT_NAME_cli")) + executor := cli.PrepareMainCmd(rootCmd, "BC", os.ExpandEnv("$HOME/.myawesomeprojectcli")) err := executor.Execute() if err != nil { // handle with #870 diff --git a/cmd/cosmos-sdk-cli/template/cmd/node/main.go b/cmd/cosmos-sdk-cli/template/cmd/node/main.go index edc20449e4..79722ae20f 100644 --- a/cmd/cosmos-sdk-cli/template/cmd/node/main.go +++ b/cmd/cosmos-sdk-cli/template/cmd/node/main.go @@ -12,20 +12,20 @@ import ( "github.com/tendermint/tendermint/libs/log" tmtypes "github.com/tendermint/tendermint/types" - "_REMOTE_PROJECT_PATH_/app" + "github.com/cosmos/cosmos-sdk/cmd/cosmos-sdk-cli/app" "github.com/cosmos/cosmos-sdk/server" "github.com/cosmos/cosmos-sdk/wire" ) // init parameters -var _CAPITALIZED_PROJECT_SHORT_NAME_AppInit = server.AppInit{ - AppGenState: _CAPITALIZED_PROJECT_SHORT_NAME_AppGenState, +var MyAwesomeProjectAppInit = server.AppInit{ + AppGenState: MyAwesomeProjectAppGenState, AppGenTx: server.SimpleAppGenTx, } // GenAppParams sets up the app_state, append any other app specific components. -func _CAPITALIZED_PROJECT_SHORT_NAME_AppGenState(cdc *wire.Codec, appGenTxs []json.RawMessage) (appState json.RawMessage, err error) { +func MyAwesomeProjectAppGenState(cdc *wire.Codec, appGenTxs []json.RawMessage) (appState json.RawMessage, err error) { appState, err = server.SimpleAppGenState(cdc, appGenTxs) if err != nil { return @@ -35,11 +35,11 @@ func _CAPITALIZED_PROJECT_SHORT_NAME_AppGenState(cdc *wire.Codec, appGenTxs []js } func newApp(logger log.Logger, db dbm.DB) abci.Application { - return app.New_CAPITALIZED_PROJECT_SHORT_NAME_App(logger, db) + return app.NewMyAwesomeProjectApp(logger, db) } func exportAppStateAndTMValidators(logger log.Logger, db dbm.DB) (json.RawMessage, []tmtypes.GenesisValidator, error) { - dapp := app.New_CAPITALIZED_PROJECT_SHORT_NAME_App(logger, db) + dapp := app.NewMyAwesomeProjectApp(logger, db) return dapp.ExportAppStateAndValidators() } @@ -48,17 +48,17 @@ func main() { ctx := server.NewDefaultContext() rootCmd := &cobra.Command{ - Use: "_PROJECT_SHORT_NAME_d", - Short: "_CAPITALIZED_PROJECT_SHORT_NAME_ Daemon (server)", + Use: "myawesomeprojectd", + Short: "MyAwesomeProject Daemon (server)", PersistentPreRunE: server.PersistentPreRunEFn(ctx), } - server.AddCommands(ctx, cdc, rootCmd, _CAPITALIZED_PROJECT_SHORT_NAME_AppInit, - server.ConstructAppCreator(newApp, "_PROJECT_SHORT_NAME_"), - server.ConstructAppExporter(exportAppStateAndTMValidators, "_PROJECT_SHORT_NAME_")) + server.AddCommands(ctx, cdc, rootCmd, MyAwesomeProjectAppInit, + server.ConstructAppCreator(newApp, "myawesomeproject"), + server.ConstructAppExporter(exportAppStateAndTMValidators, "myawesomeproject")) // prepare and add flags - rootDir := os.ExpandEnv("$HOME/._PROJECT_SHORT_NAME_d") + rootDir := os.ExpandEnv("$HOME/.myawesomeprojectd") executor := cli.PrepareBaseCmd(rootCmd, "BC", rootDir) err := executor.Execute() if err != nil { From 26aa8719576561afdec7ce094a368a50db4b1db9 Mon Sep 17 00:00:00 2001 From: Sridhar Ganesan Date: Mon, 16 Jul 2018 20:11:36 +0200 Subject: [PATCH 03/31] Changing _PROJECT_SHORT_NAME_ to myawesomeproject in Makefile --- cmd/cosmos-sdk-cli/template/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/cosmos-sdk-cli/template/Makefile b/cmd/cosmos-sdk-cli/template/Makefile index 8d7f55706d..77e62de8ed 100644 --- a/cmd/cosmos-sdk-cli/template/Makefile +++ b/cmd/cosmos-sdk-cli/template/Makefile @@ -7,7 +7,7 @@ get_tools: go get github.com/golang/dep/cmd/dep build: - go build -o bin/myawesomeprojectcli cmd/cli/main.go && go build -o bin/_PROJECT_SHORT_NAME_d cmd/node/main.go + go build -o bin/myawesomeprojectcli cmd/cli/main.go && go build -o bin/myawesomeprojectd cmd/node/main.go get_vendor_deps: @rm -rf vendor/ From b952331b22ce92a34450793f5ecc7c08b23f43e1 Mon Sep 17 00:00:00 2001 From: Sridhar Ganesan Date: Mon, 16 Jul 2018 20:45:56 +0200 Subject: [PATCH 04/31] Including changes to Makefile to build cosmos-sdk-cli and also ignoring linting for cosmos-sdk-cli --- Gopkg.lock | 10 +++++++--- Gopkg.toml | 4 ++++ Makefile | 26 +++++++++++++++++++------- cmd/cosmos-sdk-cli/cmd/root.go | 3 ++- tools/Makefile | 15 +++++++++++++++ 5 files changed, 47 insertions(+), 11 deletions(-) diff --git a/Gopkg.lock b/Gopkg.lock index 63f53a96f8..3d5f741794 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -2,7 +2,6 @@ [[projects]] - branch = "master" name = "github.com/bartekn/go-bip39" packages = ["."] revision = "a05967ea095d81c8fe4833776774cfaff8e5036c" @@ -79,6 +78,12 @@ revision = "259ab82a6cad3992b4e21ff5cac294ccb06474bc" version = "v1.7.0" +[[projects]] + name = "github.com/gobuffalo/packr" + packages = ["."] + revision = "bd47f2894846e32edcf9aa37290fef76c327883f" + version = "v1.11.1" + [[projects]] name = "github.com/gogo/protobuf" packages = [ @@ -135,7 +140,6 @@ ".", "hcl/ast", "hcl/parser", - "hcl/printer", "hcl/scanner", "hcl/strconv", "hcl/token", @@ -503,6 +507,6 @@ [solve-meta] analyzer-name = "dep" analyzer-version = 1 - inputs-digest = "94abff3ff321fd150a6e4b95d109297296cdc00693c648c9b2a48171b90e36b0" + inputs-digest = "2a655310c28a7f80cb2cfe5f2969182896aac09293344472de10e9135ddc364c" solver-name = "gps-cdcl" solver-version = 1 diff --git a/Gopkg.toml b/Gopkg.toml index 3db675ebbd..2a8ef4bc7a 100644 --- a/Gopkg.toml +++ b/Gopkg.toml @@ -67,6 +67,10 @@ name = "github.com/zondax/ledger-goclient" revision = "39ba4728c137c75718a21f9b4b3280fa31b9139b" +[[constraint]] + name = "github.com/gobuffalo/packr" + version = "1.11.1" + [prune] go-tests = true unused-packages = true diff --git a/Makefile b/Makefile index b6b136f517..25e9f5c873 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ -PACKAGES=$(shell go list ./... | grep -v '/vendor/') -PACKAGES_NOCLITEST=$(shell go list ./... | grep -v '/vendor/' | grep -v github.com/cosmos/cosmos-sdk/cmd/gaia/cli_test) +PACKAGES=$(shell go list ./... | grep -v '/vendor/' | grep -v '/cosmos-sdk-cli/template') +PACKAGES_NOCLITEST=$(shell go list ./... | grep -v '/vendor/' | grep -v '/cosmos-sdk-cli/template' | grep -v github.com/cosmos/cosmos-sdk/cmd/gaia/cli_test) COMMIT_HASH := $(shell git rev-parse --short HEAD) BUILD_TAGS = netgo ledger BUILD_FLAGS = -tags "${BUILD_TAGS}" -ldflags "-X github.com/cosmos/cosmos-sdk/version.GitCommit=${COMMIT_HASH}" @@ -37,6 +37,15 @@ endif build-linux: LEDGER_ENABLED=false GOOS=linux GOARCH=amd64 $(MAKE) build +build_cosmos-sdk-cli: +ifeq ($(OS),Windows_NT) + packr build $(BUILD_FLAGS) -o build/cosmos-sdk-cli.exe ./cmd/cosmos-sdk-cli + packr clean +else + packr build $(BUILD_FLAGS) -o build/cosmos-sdk-cli ./cmd/cosmos-sdk-cli + packr clean +endif + build_examples: ifeq ($(OS),Windows_NT) go build $(BUILD_FLAGS) -o build/basecoind.exe ./examples/basecoin/cmd/basecoind @@ -60,6 +69,9 @@ install_examples: go install $(BUILD_FLAGS) ./examples/democoin/cmd/democoind go install $(BUILD_FLAGS) ./examples/democoin/cmd/democli +install_cosmos-sdk-cli: + go install $(BUILD_FLAGS) ./cmd/cosmos-sdk-cli + install_debug: go install $(BUILD_FLAGS) ./cmd/gaia/cmd/gaiadebug @@ -82,7 +94,7 @@ get_tools: get_vendor_deps: @rm -rf vendor/ @echo "--> Running dep ensure" - @dep ensure -v + @dep ensure -v --vendor-only draw_deps: @# requires brew install graphviz or apt-get install graphviz @@ -116,9 +128,9 @@ test_cover: @bash tests/test_cover.sh test_lint: - gometalinter.v2 --config=tools/gometalinter.json ./... - !(gometalinter.v2 --disable-all --enable='errcheck' --vendor ./... | grep -v "client/") - find . -name '*.go' -type f -not -path "./vendor*" -not -path "*.git*" | xargs gofmt -d -s + gometalinter.v2 --config=tools/gometalinter.json ./... --exclude cmd/cosmos-sdk-cli + !(gometalinter.v2 --disable-all --enable='errcheck' --vendor ./... | grep -v "client/" | grep -v "cosmos-sdk-cli/template/") + find . -name '*.go' -type f -not -path "./vendor*" -not -path "./cmd/cosmos-sdk-cli/*" -not -path "*.git*" | xargs gofmt -d -s dep status >> /dev/null !(grep -n branch Gopkg.toml) @@ -193,7 +205,7 @@ remotenet-status: # To avoid unintended conflicts with file names, always add to .PHONY # unless there is a reason not to. # https://www.gnu.org/software/make/manual/html_node/Phony-Targets.html -.PHONY: build build_examples install install_examples install_debug dist \ +.PHONY: build build_cosmos-sdk-cli build_examples install install_examples install_cosmos-sdk-cli install_debug dist \ check_tools get_tools get_vendor_deps draw_deps test test_cli test_unit \ test_cover test_lint benchmark devdoc_init devdoc devdoc_save devdoc_update \ build-linux build-docker-gaiadnode localnet-start localnet-stop remotenet-start \ diff --git a/cmd/cosmos-sdk-cli/cmd/root.go b/cmd/cosmos-sdk-cli/cmd/root.go index c05234701c..ae09169d90 100644 --- a/cmd/cosmos-sdk-cli/cmd/root.go +++ b/cmd/cosmos-sdk-cli/cmd/root.go @@ -8,13 +8,14 @@ import ( ) var rootCmd = &cobra.Command{ - Use: "cosmos-zone", + Use: "cosmos-sdk-cli", Short: "Tools to develop on cosmos-sdk", Run: func(cmd *cobra.Command, args []string) { // Do Stuff Here }, } +// Execute the command func Execute() { if err := rootCmd.Execute(); err != nil { fmt.Println(err) diff --git a/tools/Makefile b/tools/Makefile index d58f52d1b1..5dfbd08fa0 100644 --- a/tools/Makefile +++ b/tools/Makefile @@ -13,6 +13,7 @@ MISSPELL = github.com/client9/misspell/cmd/misspell ERRCHECK = github.com/kisielk/errcheck UNPARAM = mvdan.cc/unparam GOCYCLO = github.com/alecthomas/gocyclo +PACKR = github.com/gobuffalo/packr DEP_CHECK := $(shell command -v dep 2> /dev/null) GOLINT_CHECK := $(shell command -v golint 2> /dev/null) @@ -23,6 +24,7 @@ MISSPELL_CHECK := $(shell command -v misspell 2> /dev/null) ERRCHECK_CHECK := $(shell command -v errcheck 2> /dev/null) UNPARAM_CHECK := $(shell command -v unparam 2> /dev/null) GOCYCLO_CHECK := $(shell command -v gocyclo 2> /dev/null) +PACKR_CHECK := $(shell command -v packr 2> /dev/null) check_tools: ifndef DEP_CHECK @@ -70,6 +72,11 @@ ifndef GOCYCLO_CHECK else @echo "Found gocyclo in path." endif +ifndef PACKR_CHECK + @echo "No packr in path. Install with make get_tools'." +else + @echo "Found packr in path." +endif get_tools: ifdef DEP_CHECK @@ -126,6 +133,12 @@ else @echo "Installing goyclo" go get -v $(GOCYCLO) endif +ifdef PACKR_CHECK + @echo "Packr is already installed. Run 'make update_tools' to update." +else + @echo "$(ansi_grn)Installing packr$(ansi_end)" + go get -v $(PACKR) +endif update_tools: @echo "Updating dep" @@ -146,6 +159,8 @@ update_tools: go get -u -v $(UNPARAM) @echo "Updating goyclo" go get -u -v $(GOCYCLO) + @echo "Updating packr" + go get -u -v $(PACKR) # To avoid unintended conflicts with file names, always add to .PHONY # unless there is a reason not to. From c0cf5952211c39770a0b1cec9df75160ea56f488 Mon Sep 17 00:00:00 2001 From: Sridhar Ganesan Date: Mon, 16 Jul 2018 21:16:39 +0200 Subject: [PATCH 05/31] Using packr install instead of go install --- Makefile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 25e9f5c873..7d452d91a8 100644 --- a/Makefile +++ b/Makefile @@ -70,7 +70,8 @@ install_examples: go install $(BUILD_FLAGS) ./examples/democoin/cmd/democli install_cosmos-sdk-cli: - go install $(BUILD_FLAGS) ./cmd/cosmos-sdk-cli + packr install $(BUILD_FLAGS) ./cmd/cosmos-sdk-cli + packr clean install_debug: go install $(BUILD_FLAGS) ./cmd/gaia/cmd/gaiadebug From e3a54f7c2875ab486f49358507edd30cf6336341 Mon Sep 17 00:00:00 2001 From: Sridhar Ganesan Date: Mon, 16 Jul 2018 21:23:27 +0200 Subject: [PATCH 06/31] ignoring template directory in cosmos-sdk-cli --- Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 7d452d91a8..d567c99136 100644 --- a/Makefile +++ b/Makefile @@ -129,9 +129,9 @@ test_cover: @bash tests/test_cover.sh test_lint: - gometalinter.v2 --config=tools/gometalinter.json ./... --exclude cmd/cosmos-sdk-cli + gometalinter.v2 --config=tools/gometalinter.json ./... --exclude cmd/cosmos-sdk-cli/template !(gometalinter.v2 --disable-all --enable='errcheck' --vendor ./... | grep -v "client/" | grep -v "cosmos-sdk-cli/template/") - find . -name '*.go' -type f -not -path "./vendor*" -not -path "./cmd/cosmos-sdk-cli/*" -not -path "*.git*" | xargs gofmt -d -s + find . -name '*.go' -type f -not -path "./vendor*" -not -path "./cmd/cosmos-sdk-cli/template" -not -path "*.git*" | xargs gofmt -d -s dep status >> /dev/null !(grep -n branch Gopkg.toml) From 4208c0d2eb699d9d1c7b19eae3e7b107f8325e4f Mon Sep 17 00:00:00 2001 From: Sridhar Ganesan Date: Mon, 16 Jul 2018 21:30:19 +0200 Subject: [PATCH 07/31] Formatting go code in template --- cmd/cosmos-sdk-cli/template/app/app.go | 2 +- cmd/cosmos-sdk-cli/template/types/account.go | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/cmd/cosmos-sdk-cli/template/app/app.go b/cmd/cosmos-sdk-cli/template/app/app.go index bc536101ce..d73d7df340 100644 --- a/cmd/cosmos-sdk-cli/template/app/app.go +++ b/cmd/cosmos-sdk-cli/template/app/app.go @@ -134,7 +134,7 @@ func (app *MyAwesomeProjectApp) ExportAppStateAndValidators() (appState json.Raw app.accountMapper.IterateAccounts(ctx, appendAccount) genState := types.GenesisState{ - Accounts: accounts, + Accounts: accounts, } appState, err = wire.MarshalJSONIndent(app.cdc, genState) if err != nil { diff --git a/cmd/cosmos-sdk-cli/template/types/account.go b/cmd/cosmos-sdk-cli/template/types/account.go index d24ac045f3..211aef2cf9 100644 --- a/cmd/cosmos-sdk-cli/template/types/account.go +++ b/cmd/cosmos-sdk-cli/template/types/account.go @@ -4,7 +4,6 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/wire" "github.com/cosmos/cosmos-sdk/x/auth" - ) var _ auth.Account = (*AppAccount)(nil) @@ -47,7 +46,7 @@ func GetAccountDecoder(cdc *wire.Codec) auth.AccountDecoder { // State to Unmarshal type GenesisState struct { - Accounts []*GenesisAccount `json:"accounts"` + Accounts []*GenesisAccount `json:"accounts"` } // GenesisAccount doesn't need pubkey or sequence From 6f617057f8de706718e0fa646e3e7eabad5d1794 Mon Sep 17 00:00:00 2001 From: Sridhar Ganesan Date: Mon, 16 Jul 2018 21:32:34 +0200 Subject: [PATCH 08/31] Formatting cosmos-sdk-cli main.go --- cmd/cosmos-sdk-cli/main.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/cosmos-sdk-cli/main.go b/cmd/cosmos-sdk-cli/main.go index 57dcb505b8..ee5ac02156 100644 --- a/cmd/cosmos-sdk-cli/main.go +++ b/cmd/cosmos-sdk-cli/main.go @@ -1,9 +1,9 @@ package main import ( - "github.com/cosmos/cosmos-sdk/cmd/cosmos-sdk-cli/cmd" + "github.com/cosmos/cosmos-sdk/cmd/cosmos-sdk-cli/cmd" ) func main() { - cmd.Execute() + cmd.Execute() } From 07ddbae7a3ef5a41a2d1970a04b0f24850f11f4f Mon Sep 17 00:00:00 2001 From: Sridhar Ganesan Date: Mon, 16 Jul 2018 21:48:16 +0200 Subject: [PATCH 09/31] Adding install_cosmos-sdk-cli to target all --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index d567c99136..d8eab8e868 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ BUILD_TAGS = netgo ledger BUILD_FLAGS = -tags "${BUILD_TAGS}" -ldflags "-X github.com/cosmos/cosmos-sdk/version.GitCommit=${COMMIT_HASH}" GCC := $(shell command -v gcc 2> /dev/null) LEDGER_ENABLED ?= true -all: get_tools get_vendor_deps install install_examples test_lint test +all: get_tools get_vendor_deps install install_examples install_cosmos-sdk-cli test_lint test ######################################## ### CI From bfe160605d5f884032f2aa8aa0d1ecdbc7ccb03b Mon Sep 17 00:00:00 2001 From: Sridhar Ganesan Date: Mon, 16 Jul 2018 22:17:18 +0200 Subject: [PATCH 10/31] Modifying Changelog to include features added(cosmos-sdk-cli) --- CHANGELOG.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f882c56959..74e0bdc8e3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # Changelog +## 0.21.2 +*July 16th, 2018* + +Features +* [cosmos-sdk-cli] Added support for cosmos-sdk-cli under cosmos-sdk/cmd + This allows SDK users to init a new project repository with a single command. + ## 0.21.1 *July 14th, 2018* From 0bde720c80d3720881eadb47d6317267e592619d Mon Sep 17 00:00:00 2001 From: Sridhar Ganesan Date: Mon, 16 Jul 2018 22:28:21 +0200 Subject: [PATCH 11/31] Adding docs for cosmos-sdk-cli --- docs/sdk/cli.md | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 docs/sdk/cli.md diff --git a/docs/sdk/cli.md b/docs/sdk/cli.md new file mode 100644 index 0000000000..f9778668ba --- /dev/null +++ b/docs/sdk/cli.md @@ -0,0 +1,34 @@ +# cosmos-sdk-cli +Create a new blockchain project based on cosmos-sdk with a single command. + +--- + +# Installation + +```shell +go get github.com/cosmos/cosmos-sdk +cd $GOPATH/src/github.com/cosmos/cosmos-sdk +make all +``` + +This will install a binary cosmos-sdk-cli + +# Creating a new project + +**cosmos-sdk-cli init ** _Your-Project-Name_ + +This will initialize a project, the dependencies, directory structures with the specified project name. + +### Example: +```shell +cosmos-sdk-cli init testzone +``` + +This will ask for a remote path for the project - usually github.com/your_user_name/testzone and will create a new testzone application under $GOPATH/src/github.com/your_user_name/testzone along with Makefile + +```shell +cd $GOPATH/src/github.com/your_user_name/testzone +make +``` +This will create two binaries(testzonecli and testzoned) under bin folder. testzoned is the full node of the application which you can run, and testzonecli is your light client. + From dedb2e4278e921787ed0bb463ddd0f7010857b2e Mon Sep 17 00:00:00 2001 From: Sridhar Ganesan Date: Mon, 16 Jul 2018 22:30:11 +0200 Subject: [PATCH 12/31] Formatting the command properly --- docs/sdk/cli.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/sdk/cli.md b/docs/sdk/cli.md index f9778668ba..c71d48e0b4 100644 --- a/docs/sdk/cli.md +++ b/docs/sdk/cli.md @@ -15,7 +15,7 @@ This will install a binary cosmos-sdk-cli # Creating a new project -**cosmos-sdk-cli init ** _Your-Project-Name_ +**cosmos-sdk-cli init** _Your-Project-Name_ This will initialize a project, the dependencies, directory structures with the specified project name. From 6410d4da1511d2bead444ffd0cb5b014c8ce98bd Mon Sep 17 00:00:00 2001 From: Sridhar Ganesan Date: Mon, 16 Jul 2018 22:53:48 +0200 Subject: [PATCH 13/31] Attempting to fix tests during pull request to master repo --- cmd/cosmos-sdk-cli/cmd/init.go | 2 +- cmd/cosmos-sdk-cli/template/Makefile | 2 +- cmd/cosmos-sdk-cli/template/app/app.go | 2 +- cmd/cosmos-sdk-cli/template/cmd/cli/main.go | 4 ++-- cmd/cosmos-sdk-cli/template/cmd/node/main.go | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/cmd/cosmos-sdk-cli/cmd/init.go b/cmd/cosmos-sdk-cli/cmd/init.go index f5ded2215a..17b048c2b3 100644 --- a/cmd/cosmos-sdk-cli/cmd/init.go +++ b/cmd/cosmos-sdk-cli/cmd/init.go @@ -53,7 +53,7 @@ var initCmd = &cobra.Command{ fmt.Print("configuring the project in " + projectPath + "\n\n") time.Sleep(2 * time.Second) box := packr.NewBox("../template") - var replacer = strings.NewReplacer("MyAwesomeProject", capitalizedProjectName, "myawesomeproject", shortProjectName, "github.com/cosmos/cosmos-sdk/cmd/cosmos-sdk-cli", remoteProjectPath) + var replacer = strings.NewReplacer("MyAwesomeProject", capitalizedProjectName, "myawesomeproject", shortProjectName, "github.com/cosmos/cosmos-sdk/cmd/cosmos-sdk-cli/template", remoteProjectPath) box.Walk(func(path string, file packr.File) error { actualPath := replacer.Replace(path) fmt.Println("Creating file: " + actualPath) diff --git a/cmd/cosmos-sdk-cli/template/Makefile b/cmd/cosmos-sdk-cli/template/Makefile index 77e62de8ed..87a3d7abe2 100644 --- a/cmd/cosmos-sdk-cli/template/Makefile +++ b/cmd/cosmos-sdk-cli/template/Makefile @@ -1,5 +1,5 @@ PACKAGES=$(shell go list ./... | grep -v '/vendor/') -#BUILD_FLAGS = -ldflags "-X github.com/cosmos/cosmos-sdk/cmd/cosmos-sdk-cli/version.GitCommit=`git rev-parse --short HEAD`" +#BUILD_FLAGS = -ldflags "-X github.com/cosmos/cosmos-sdk/cmd/cosmos-sdk-cli/template/version.GitCommit=`git rev-parse --short HEAD`" all: get_tools get_vendor_deps build test diff --git a/cmd/cosmos-sdk-cli/template/app/app.go b/cmd/cosmos-sdk-cli/template/app/app.go index d73d7df340..da78acf409 100644 --- a/cmd/cosmos-sdk-cli/template/app/app.go +++ b/cmd/cosmos-sdk-cli/template/app/app.go @@ -15,7 +15,7 @@ import ( "github.com/cosmos/cosmos-sdk/x/auth" "github.com/cosmos/cosmos-sdk/x/bank" - "github.com/cosmos/cosmos-sdk/cmd/cosmos-sdk-cli/types" + "github.com/cosmos/cosmos-sdk/cmd/cosmos-sdk-cli/template/types" ) const ( diff --git a/cmd/cosmos-sdk-cli/template/cmd/cli/main.go b/cmd/cosmos-sdk-cli/template/cmd/cli/main.go index 33a888f3ba..9ce51c9990 100644 --- a/cmd/cosmos-sdk-cli/template/cmd/cli/main.go +++ b/cmd/cosmos-sdk-cli/template/cmd/cli/main.go @@ -17,8 +17,8 @@ import ( authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli" bankcmd "github.com/cosmos/cosmos-sdk/x/bank/client/cli" - "github.com/cosmos/cosmos-sdk/cmd/cosmos-sdk-cli/app" - "github.com/cosmos/cosmos-sdk/cmd/cosmos-sdk-cli/types" + "github.com/cosmos/cosmos-sdk/cmd/cosmos-sdk-cli/template/app" + "github.com/cosmos/cosmos-sdk/cmd/cosmos-sdk-cli/template/types" ) // rootCmd is the entry point for this binary diff --git a/cmd/cosmos-sdk-cli/template/cmd/node/main.go b/cmd/cosmos-sdk-cli/template/cmd/node/main.go index 79722ae20f..f9f7a5ffe0 100644 --- a/cmd/cosmos-sdk-cli/template/cmd/node/main.go +++ b/cmd/cosmos-sdk-cli/template/cmd/node/main.go @@ -12,7 +12,7 @@ import ( "github.com/tendermint/tendermint/libs/log" tmtypes "github.com/tendermint/tendermint/types" - "github.com/cosmos/cosmos-sdk/cmd/cosmos-sdk-cli/app" + "github.com/cosmos/cosmos-sdk/cmd/cosmos-sdk-cli/template/app" "github.com/cosmos/cosmos-sdk/server" "github.com/cosmos/cosmos-sdk/wire" From c1553f57e964c4b2063f7ecd3b3b249b3bbe722d Mon Sep 17 00:00:00 2001 From: Sridhar Ganesan Date: Mon, 16 Jul 2018 23:06:16 +0200 Subject: [PATCH 14/31] Using func type instead of auth.Account as cosmos-sdk has changed --- cmd/cosmos-sdk-cli/template/app/app.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/cosmos-sdk-cli/template/app/app.go b/cmd/cosmos-sdk-cli/template/app/app.go index da78acf409..465acbf0f6 100644 --- a/cmd/cosmos-sdk-cli/template/app/app.go +++ b/cmd/cosmos-sdk-cli/template/app/app.go @@ -56,7 +56,7 @@ func NewMyAwesomeProjectApp(logger log.Logger, db dbm.DB) *MyAwesomeProjectApp { app.accountMapper = auth.NewAccountMapper( cdc, app.capKeyAccountStore, // target store - types.ProtoAppAccount(), // prototype + types.ProtoAppAccount, // prototype ) // Add handlers. From 1e16ba370f9ac5ba487a6ed1e2b664eba83a63e0 Mon Sep 17 00:00:00 2001 From: Sridhar Ganesan Date: Mon, 16 Jul 2018 23:25:51 +0200 Subject: [PATCH 15/31] Fixing bugs due to version upgrade of cosmos-sdk --- cmd/cosmos-sdk-cli/template/Gopkg.toml | 2 +- cmd/cosmos-sdk-cli/template/cmd/node/main.go | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/cmd/cosmos-sdk-cli/template/Gopkg.toml b/cmd/cosmos-sdk-cli/template/Gopkg.toml index d4c3b83520..551f1ca7fd 100644 --- a/cmd/cosmos-sdk-cli/template/Gopkg.toml +++ b/cmd/cosmos-sdk-cli/template/Gopkg.toml @@ -46,7 +46,7 @@ name = "github.com/tendermint/go-wire" [[constraint]] - version = "~0.20.0" + version = "~0.22.0" name = "github.com/cosmos/cosmos-sdk" [[override]] diff --git a/cmd/cosmos-sdk-cli/template/cmd/node/main.go b/cmd/cosmos-sdk-cli/template/cmd/node/main.go index f9f7a5ffe0..8a27744309 100644 --- a/cmd/cosmos-sdk-cli/template/cmd/node/main.go +++ b/cmd/cosmos-sdk-cli/template/cmd/node/main.go @@ -2,6 +2,7 @@ package main import ( "encoding/json" + "io" "os" "github.com/spf13/cobra" @@ -34,11 +35,11 @@ func MyAwesomeProjectAppGenState(cdc *wire.Codec, appGenTxs []json.RawMessage) ( return } -func newApp(logger log.Logger, db dbm.DB) abci.Application { +func newApp(logger log.Logger, db dbm.DB, _ io.Writer) abci.Application { return app.NewMyAwesomeProjectApp(logger, db) } -func exportAppStateAndTMValidators(logger log.Logger, db dbm.DB) (json.RawMessage, []tmtypes.GenesisValidator, error) { +func exportAppStateAndTMValidators(logger log.Logger, db dbm.DB, _ io.Writer) (json.RawMessage, []tmtypes.GenesisValidator, error) { dapp := app.NewMyAwesomeProjectApp(logger, db) return dapp.ExportAppStateAndValidators() } From a463089df1d6ada45149b5554b27349c47214fa0 Mon Sep 17 00:00:00 2001 From: Sridhar Ganesan Date: Mon, 16 Jul 2018 23:58:40 +0200 Subject: [PATCH 16/31] Using cosmos-sdk version from the library itself --- cmd/cosmos-sdk-cli/cmd/init.go | 5 +++++ cmd/cosmos-sdk-cli/template/Gopkg.toml | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/cmd/cosmos-sdk-cli/cmd/init.go b/cmd/cosmos-sdk-cli/cmd/init.go index 17b048c2b3..713f7e81a7 100644 --- a/cmd/cosmos-sdk-cli/cmd/init.go +++ b/cmd/cosmos-sdk-cli/cmd/init.go @@ -9,6 +9,7 @@ import ( "strings" "time" + "github.com/cosmos/cosmos-sdk/version" "github.com/gobuffalo/packr" "github.com/pkg/errors" "github.com/spf13/cobra" @@ -59,6 +60,10 @@ var initCmd = &cobra.Command{ fmt.Println("Creating file: " + actualPath) contents := box.String(path) contents = replacer.Replace(contents) + if actualPath == "Gopkg.toml" { + versionReplacer := strings.NewReplacer("_COSMOS_VERSION_", version.Version) + contents = versionReplacer.Replace(contents) + } lastIndex := strings.LastIndex(actualPath, string(os.PathSeparator)) rootDir := "" if lastIndex != -1 { diff --git a/cmd/cosmos-sdk-cli/template/Gopkg.toml b/cmd/cosmos-sdk-cli/template/Gopkg.toml index 551f1ca7fd..6b9f230497 100644 --- a/cmd/cosmos-sdk-cli/template/Gopkg.toml +++ b/cmd/cosmos-sdk-cli/template/Gopkg.toml @@ -46,7 +46,7 @@ name = "github.com/tendermint/go-wire" [[constraint]] - version = "~0.22.0" + version = "~_COSMOS_VERSION_" name = "github.com/cosmos/cosmos-sdk" [[override]] From bb32ebdd49a8a614f817cbe1930b86f7164b20b0 Mon Sep 17 00:00:00 2001 From: Sridhar Ganesan Date: Tue, 17 Jul 2018 00:17:38 +0200 Subject: [PATCH 17/31] Hardcoding cosmos-sdk version to 0.21.0 as both 0.20.0 and 0.22.0 breaks the resulting project/template --- cmd/cosmos-sdk-cli/template/Gopkg.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/cosmos-sdk-cli/template/Gopkg.toml b/cmd/cosmos-sdk-cli/template/Gopkg.toml index 6b9f230497..b0e7a0fef6 100644 --- a/cmd/cosmos-sdk-cli/template/Gopkg.toml +++ b/cmd/cosmos-sdk-cli/template/Gopkg.toml @@ -46,7 +46,7 @@ name = "github.com/tendermint/go-wire" [[constraint]] - version = "~_COSMOS_VERSION_" + version = "~0.21.0" name = "github.com/cosmos/cosmos-sdk" [[override]] From 1603804ca09c6bd6c705d7d34bbf8564d46b0664 Mon Sep 17 00:00:00 2001 From: Sridhar Ganesan Date: Tue, 17 Jul 2018 21:18:55 +0200 Subject: [PATCH 18/31] Using basecoin as the template and refactoring the command that creates the workspace setup Removing templates, making changes to makefile to remove dependency on packr --- Gopkg.lock | 8 +- Gopkg.toml | 4 - Makefile | 21 ++- cmd/cosmos-sdk-cli/cmd/init.go | 146 +++++++++++++------ cmd/cosmos-sdk-cli/cmd/root.go | 3 - cmd/cosmos-sdk-cli/template/Gopkg.toml | 58 -------- cmd/cosmos-sdk-cli/template/Makefile | 23 --- cmd/cosmos-sdk-cli/template/app/app.go | 144 ------------------ cmd/cosmos-sdk-cli/template/cmd/cli/main.go | 77 ---------- cmd/cosmos-sdk-cli/template/cmd/node/main.go | 69 --------- cmd/cosmos-sdk-cli/template/types/account.go | 77 ---------- tools/Makefile | 15 -- 12 files changed, 111 insertions(+), 534 deletions(-) delete mode 100644 cmd/cosmos-sdk-cli/template/Gopkg.toml delete mode 100644 cmd/cosmos-sdk-cli/template/Makefile delete mode 100644 cmd/cosmos-sdk-cli/template/app/app.go delete mode 100644 cmd/cosmos-sdk-cli/template/cmd/cli/main.go delete mode 100644 cmd/cosmos-sdk-cli/template/cmd/node/main.go delete mode 100644 cmd/cosmos-sdk-cli/template/types/account.go diff --git a/Gopkg.lock b/Gopkg.lock index 3d5f741794..66f6c04925 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -78,12 +78,6 @@ revision = "259ab82a6cad3992b4e21ff5cac294ccb06474bc" version = "v1.7.0" -[[projects]] - name = "github.com/gobuffalo/packr" - packages = ["."] - revision = "bd47f2894846e32edcf9aa37290fef76c327883f" - version = "v1.11.1" - [[projects]] name = "github.com/gogo/protobuf" packages = [ @@ -507,6 +501,6 @@ [solve-meta] analyzer-name = "dep" analyzer-version = 1 - inputs-digest = "2a655310c28a7f80cb2cfe5f2969182896aac09293344472de10e9135ddc364c" + inputs-digest = "71e86b1f1e9ec71901c20d8532dc8477df66eff37a407322379f6a8b03e5d91b" solver-name = "gps-cdcl" solver-version = 1 diff --git a/Gopkg.toml b/Gopkg.toml index 2a8ef4bc7a..3db675ebbd 100644 --- a/Gopkg.toml +++ b/Gopkg.toml @@ -67,10 +67,6 @@ name = "github.com/zondax/ledger-goclient" revision = "39ba4728c137c75718a21f9b4b3280fa31b9139b" -[[constraint]] - name = "github.com/gobuffalo/packr" - version = "1.11.1" - [prune] go-tests = true unused-packages = true diff --git a/Makefile b/Makefile index d8eab8e868..69890355ca 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ -PACKAGES=$(shell go list ./... | grep -v '/vendor/' | grep -v '/cosmos-sdk-cli/template') -PACKAGES_NOCLITEST=$(shell go list ./... | grep -v '/vendor/' | grep -v '/cosmos-sdk-cli/template' | grep -v github.com/cosmos/cosmos-sdk/cmd/gaia/cli_test) +PACKAGES=$(shell go list ./... | grep -v '/vendor/') +PACKAGES_NOCLITEST=$(shell go list ./... | grep -v '/vendor/' | grep -v github.com/cosmos/cosmos-sdk/cmd/gaia/cli_test) COMMIT_HASH := $(shell git rev-parse --short HEAD) BUILD_TAGS = netgo ledger BUILD_FLAGS = -tags "${BUILD_TAGS}" -ldflags "-X github.com/cosmos/cosmos-sdk/version.GitCommit=${COMMIT_HASH}" @@ -39,11 +39,9 @@ build-linux: build_cosmos-sdk-cli: ifeq ($(OS),Windows_NT) - packr build $(BUILD_FLAGS) -o build/cosmos-sdk-cli.exe ./cmd/cosmos-sdk-cli - packr clean + go build $(BUILD_FLAGS) -o build/cosmos-sdk-cli.exe ./cmd/cosmos-sdk-cli else - packr build $(BUILD_FLAGS) -o build/cosmos-sdk-cli ./cmd/cosmos-sdk-cli - packr clean + go build $(BUILD_FLAGS) -o build/cosmos-sdk-cli ./cmd/cosmos-sdk-cli endif build_examples: @@ -70,8 +68,7 @@ install_examples: go install $(BUILD_FLAGS) ./examples/democoin/cmd/democli install_cosmos-sdk-cli: - packr install $(BUILD_FLAGS) ./cmd/cosmos-sdk-cli - packr clean + go install $(BUILD_FLAGS) ./cmd/cosmos-sdk-cli install_debug: go install $(BUILD_FLAGS) ./cmd/gaia/cmd/gaiadebug @@ -95,7 +92,7 @@ get_tools: get_vendor_deps: @rm -rf vendor/ @echo "--> Running dep ensure" - @dep ensure -v --vendor-only + @dep ensure -v draw_deps: @# requires brew install graphviz or apt-get install graphviz @@ -129,9 +126,9 @@ test_cover: @bash tests/test_cover.sh test_lint: - gometalinter.v2 --config=tools/gometalinter.json ./... --exclude cmd/cosmos-sdk-cli/template - !(gometalinter.v2 --disable-all --enable='errcheck' --vendor ./... | grep -v "client/" | grep -v "cosmos-sdk-cli/template/") - find . -name '*.go' -type f -not -path "./vendor*" -not -path "./cmd/cosmos-sdk-cli/template" -not -path "*.git*" | xargs gofmt -d -s + gometalinter.v2 --config=tools/gometalinter.json ./... + !(gometalinter.v2 --disable-all --enable='errcheck' --vendor ./... | grep -v "client/" ) + find . -name '*.go' -type f -not -path "./vendor*" -not -path "*.git*" | xargs gofmt -d -s dep status >> /dev/null !(grep -n branch Gopkg.toml) diff --git a/cmd/cosmos-sdk-cli/cmd/init.go b/cmd/cosmos-sdk-cli/cmd/init.go index 713f7e81a7..2279dc8a17 100644 --- a/cmd/cosmos-sdk-cli/cmd/init.go +++ b/cmd/cosmos-sdk-cli/cmd/init.go @@ -1,21 +1,19 @@ package cmd import ( - "bufio" "fmt" "go/build" "io/ioutil" "os" "strings" - "time" "github.com/cosmos/cosmos-sdk/version" - "github.com/gobuffalo/packr" - "github.com/pkg/errors" "github.com/spf13/cobra" + "path/filepath" ) func init() { + initCmd.Flags().StringVarP(&remoteProjectPath, "project-path", "p", "", "Remote project path. eg: github.com/your_user_name/project_name") rootCmd.AddCommand(initCmd) } @@ -28,54 +26,112 @@ func resolveProjectPath(remoteProjectPath string) string { return gopath + string(os.PathSeparator) + "src" + string(os.PathSeparator) + remoteProjectPath } -var initCmd = &cobra.Command{ - Use: "init AwesomeProjectName", - Short: "Initialize your new cosmos zone", - RunE: func(cmd *cobra.Command, args []string) error { - if len(args) < 1 { - return errors.New("Project name is required") +func check(e error) { + if e != nil { + panic(e) + } +} + +var remoteBasecoinPath = "github.com/cosmos/cosmos-sdk/examples/basecoin" + +func copyBasecoinTemplate(projectName string, projectPath string, remoteProjectPath string) { + basecoinProjectPath := resolveProjectPath(remoteBasecoinPath) + filepath.Walk(basecoinProjectPath, func(path string, f os.FileInfo, err error) error { + if !f.IsDir() { + data, err := ioutil.ReadFile(path) + check(err) + contents := string(data) + // Extract relative file path eg: app/app.go instead of /Users/..../github.com/cosmos/...examples/basecoin/app/app.go + relativeFilePath := path[len(basecoinProjectPath)+1:] + // Evaluating the filepath in the new project folder + projectFilePath := projectPath + string(os.PathSeparator) + relativeFilePath + lengthOfRootDir := strings.LastIndex(projectFilePath, string(os.PathSeparator)) + // Extracting the path of root directory from the filepath + rootDir := projectFilePath[0:lengthOfRootDir] + // Creating the required directory first + os.MkdirAll(rootDir, os.ModePerm) + fmt.Println("Creating " + projectFilePath) + // Writing the contents to a file in the project folder + ioutil.WriteFile(projectFilePath, []byte(contents), os.ModePerm) } + return nil + }) + + //Copy the entire basecoin directory to the project path. + //os.MkdirAll(projectPath+string(os.PathSeparator)+rootDir, os.ModePerm) + //filePath := projectPath + string(os.PathSeparator) + actualPath + //ioutil.WriteFile(filePath, []byte(contents), os.ModePerm) +} + +func createGopkg(projectPath string) { + // Create gopkg.toml file + dependencies := map[string]string{ + "github.com/cosmos/cosmos-sdk": version.Version, + } + overrides := map[string]string{ + "github.com/golang/protobuf": "1.1.0", + } + contents := "" + for dependency, version := range dependencies { + contents += "[[constraint]]\n\tname = \"" + dependency + "\"\n\tversion = \"" + version + "\"\n\n" + } + for dependency, version := range overrides { + contents += "[[override]]\n\tname = \"" + dependency + "\"\n\tversion = \"=" + version + "\"\n\n" + } + contents += "[prune]\n\tgo-tests = true\n\tunused-packages = true" + ioutil.WriteFile(projectPath+"/Gopkg.toml", []byte(contents), os.ModePerm) +} + +func createMakefile(projectPath string) { + // Create makefile + makefileContents := `PACKAGES=$(shell go list ./... | grep -v '/vendor/') + +all: get_tools get_vendor_deps build test + +get_tools: + go get github.com/golang/dep/cmd/dep + +build: + go build -o bin/basecli cmd/basecli/main.go && go build -o bin/basecoind cmd/basecoind/main.go + +get_vendor_deps: + @rm -rf vendor/ + @dep ensure + +test: + @go test $(PACKAGES) + +benchmark: + @go test -bench=. $(PACKAGES) + +.PHONY: all build test benchmark` + ioutil.WriteFile(projectPath+"/Makefile", []byte(makefileContents), os.ModePerm) + +} + +func setupBasecoinWorkspace(projectName string, remoteProjectPath string) { + projectPath := resolveProjectPath(remoteProjectPath) + fmt.Println("Configuring your project in " + projectPath) + copyBasecoinTemplate(projectName, projectPath, remoteProjectPath) + createGopkg(projectPath) + createMakefile(projectPath) + fmt.Println("\nInitialized a new project at " + projectPath + ".\nHappy hacking!") +} + +var remoteProjectPath string +var initCmd = &cobra.Command{ + Use: "init [ProjectName]", + Short: "Initialize your new cosmos zone", + Args: cobra.ExactArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + fmt.Print("Thanks for choosing Cosmos-SDK to build your project.\n\n") projectName := args[0] - capitalizedProjectName := strings.Title(projectName) shortProjectName := strings.ToLower(projectName) - reader := bufio.NewReader(os.Stdin) - fmt.Println("Thank you for using cosmos-zone tool.") - fmt.Println("You are only a few steps away from creating your brand new blockchain project on Cosmos.") - fmt.Print("We will ask you a few more questions to guide you through this process\n\n") - fmt.Print("To configure this project we need a remote project path. If you are unsure you can leave this empty. ") - fmt.Print("Remote project path is usually something like github.com/your_user_name/project_name\n") - fmt.Print("Enter remote project path: ") - remoteProjectPath, _ := reader.ReadString('\n') remoteProjectPath = strings.ToLower(strings.TrimSpace(remoteProjectPath)) if remoteProjectPath == "" { remoteProjectPath = strings.ToLower(shortProjectName) } - projectPath := resolveProjectPath(remoteProjectPath) - fmt.Print("configuring the project in " + projectPath + "\n\n") - time.Sleep(2 * time.Second) - box := packr.NewBox("../template") - var replacer = strings.NewReplacer("MyAwesomeProject", capitalizedProjectName, "myawesomeproject", shortProjectName, "github.com/cosmos/cosmos-sdk/cmd/cosmos-sdk-cli/template", remoteProjectPath) - box.Walk(func(path string, file packr.File) error { - actualPath := replacer.Replace(path) - fmt.Println("Creating file: " + actualPath) - contents := box.String(path) - contents = replacer.Replace(contents) - if actualPath == "Gopkg.toml" { - versionReplacer := strings.NewReplacer("_COSMOS_VERSION_", version.Version) - contents = versionReplacer.Replace(contents) - } - lastIndex := strings.LastIndex(actualPath, string(os.PathSeparator)) - rootDir := "" - if lastIndex != -1 { - rootDir = actualPath[0:lastIndex] - } - // Create directory - os.MkdirAll(projectPath+string(os.PathSeparator)+rootDir, os.ModePerm) - filePath := projectPath + string(os.PathSeparator) + actualPath - ioutil.WriteFile(filePath, []byte(contents), os.ModePerm) - return nil - }) - fmt.Println("Initialized a new project at " + projectPath + ". Happy hacking!") + setupBasecoinWorkspace(shortProjectName, remoteProjectPath) return nil }, } diff --git a/cmd/cosmos-sdk-cli/cmd/root.go b/cmd/cosmos-sdk-cli/cmd/root.go index ae09169d90..2eddd79b5d 100644 --- a/cmd/cosmos-sdk-cli/cmd/root.go +++ b/cmd/cosmos-sdk-cli/cmd/root.go @@ -10,9 +10,6 @@ import ( var rootCmd = &cobra.Command{ Use: "cosmos-sdk-cli", Short: "Tools to develop on cosmos-sdk", - Run: func(cmd *cobra.Command, args []string) { - // Do Stuff Here - }, } // Execute the command diff --git a/cmd/cosmos-sdk-cli/template/Gopkg.toml b/cmd/cosmos-sdk-cli/template/Gopkg.toml deleted file mode 100644 index b0e7a0fef6..0000000000 --- a/cmd/cosmos-sdk-cli/template/Gopkg.toml +++ /dev/null @@ -1,58 +0,0 @@ -# Gopkg.toml example -# -# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md -# for detailed Gopkg.toml documentation. -# -# required = ["github.com/user/thing/cmd/thing"] -# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"] -# -# [[constraint]] -# name = "github.com/user/project" -# version = "1.0.0" -# -# [[constraint]] -# name = "github.com/user/project2" -# branch = "dev" -# source = "github.com/myfork/project2" -# -# [[override]] -# name = "github.com/x/y" -# version = "2.4.0" -# -# [prune] -# non-go = false -# go-tests = true -# unused-packages = true - -[[constraint]] - name = "github.com/pkg/errors" - version = "~0.8.0" - -[[constraint]] - name = "github.com/spf13/cobra" - version = "~0.0.1" - -[[constraint]] - name = "github.com/spf13/viper" - version = "~1.0.0" - -[[constraint]] - name = "github.com/stretchr/testify" - version = "~1.2.1" - -[[constraint]] - version = "~0.10.0" - source = "github.com/tendermint/go-amino" - name = "github.com/tendermint/go-wire" - -[[constraint]] - version = "~0.21.0" - name = "github.com/cosmos/cosmos-sdk" - -[[override]] - version = "=1.1.0" - name = "github.com/golang/protobuf" - -[prune] - go-tests = true - unused-packages = true diff --git a/cmd/cosmos-sdk-cli/template/Makefile b/cmd/cosmos-sdk-cli/template/Makefile deleted file mode 100644 index 87a3d7abe2..0000000000 --- a/cmd/cosmos-sdk-cli/template/Makefile +++ /dev/null @@ -1,23 +0,0 @@ -PACKAGES=$(shell go list ./... | grep -v '/vendor/') -#BUILD_FLAGS = -ldflags "-X github.com/cosmos/cosmos-sdk/cmd/cosmos-sdk-cli/template/version.GitCommit=`git rev-parse --short HEAD`" - -all: get_tools get_vendor_deps build test - -get_tools: - go get github.com/golang/dep/cmd/dep - -build: - go build -o bin/myawesomeprojectcli cmd/cli/main.go && go build -o bin/myawesomeprojectd cmd/node/main.go - -get_vendor_deps: - @rm -rf vendor/ - @dep ensure - -test: - @go test $(PACKAGES) - -benchmark: - @go test -bench=. $(PACKAGES) - -.PHONY: all build test benchmark - diff --git a/cmd/cosmos-sdk-cli/template/app/app.go b/cmd/cosmos-sdk-cli/template/app/app.go deleted file mode 100644 index 465acbf0f6..0000000000 --- a/cmd/cosmos-sdk-cli/template/app/app.go +++ /dev/null @@ -1,144 +0,0 @@ -package app - -import ( - "encoding/json" - - abci "github.com/tendermint/tendermint/abci/types" - cmn "github.com/tendermint/tendermint/libs/common" - dbm "github.com/tendermint/tendermint/libs/db" - "github.com/tendermint/tendermint/libs/log" - tmtypes "github.com/tendermint/tendermint/types" - - bam "github.com/cosmos/cosmos-sdk/baseapp" - sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/wire" - "github.com/cosmos/cosmos-sdk/x/auth" - "github.com/cosmos/cosmos-sdk/x/bank" - - "github.com/cosmos/cosmos-sdk/cmd/cosmos-sdk-cli/template/types" -) - -const ( - appName = "MyAwesomeProjectApp" -) - -// Extended ABCI application -type MyAwesomeProjectApp struct { - *bam.BaseApp - cdc *wire.Codec - - // keys to access the substores - capKeyMainStore *sdk.KVStoreKey - capKeyAccountStore *sdk.KVStoreKey - - // keepers - feeCollectionKeeper auth.FeeCollectionKeeper - coinKeeper bank.Keeper - - // Manage getting and setting accounts - accountMapper auth.AccountMapper -} - -func NewMyAwesomeProjectApp(logger log.Logger, db dbm.DB) *MyAwesomeProjectApp { - - // Create app-level codec for txs and accounts. - var cdc = MakeCodec() - - // Create your application object. - var app = &MyAwesomeProjectApp{ - BaseApp: bam.NewBaseApp(appName, cdc, logger, db), - cdc: cdc, - capKeyMainStore: sdk.NewKVStoreKey("main"), - capKeyAccountStore: sdk.NewKVStoreKey("acc"), - } - - // Define the accountMapper. - app.accountMapper = auth.NewAccountMapper( - cdc, - app.capKeyAccountStore, // target store - types.ProtoAppAccount, // prototype - ) - - // Add handlers. - app.coinKeeper = bank.NewKeeper(app.accountMapper) - app.Router(). - AddRoute("bank", bank.NewHandler(app.coinKeeper)) - - // Initialize BaseApp. - app.SetInitChainer(app.initChainerFn()) - app.MountStoresIAVL(app.capKeyMainStore, app.capKeyAccountStore) - app.SetAnteHandler(auth.NewAnteHandler(app.accountMapper, app.feeCollectionKeeper)) - err := app.LoadLatestVersion(app.capKeyMainStore) - if err != nil { - cmn.Exit(err.Error()) - } - return app -} - -// custom tx codec -func MakeCodec() *wire.Codec { - var cdc = wire.NewCodec() - wire.RegisterCrypto(cdc) // Register crypto. - sdk.RegisterWire(cdc) // Register Msgs - bank.RegisterWire(cdc) - - // Register AppAccount - cdc.RegisterInterface((*auth.Account)(nil), nil) - cdc.RegisterConcrete(&types.AppAccount{}, "myawesomeproject/Account", nil) - - cdc.Seal() - - return cdc -} - -// custom logic for myawesomeproject initialization -// nolint: unparam -func (app *MyAwesomeProjectApp) initChainerFn() sdk.InitChainer { - return func(ctx sdk.Context, req abci.RequestInitChain) abci.ResponseInitChain { - stateJSON := req.AppStateBytes - - genesisState := new(types.GenesisState) - err := app.cdc.UnmarshalJSON(stateJSON, genesisState) - if err != nil { - panic(err) // TODO https://github.com/cosmos/cosmos-sdk/issues/468 - // return sdk.ErrGenesisParse("").TraceCause(err, "") - } - - for _, gacc := range genesisState.Accounts { - acc, err := gacc.ToAppAccount() - if err != nil { - panic(err) // TODO https://github.com/cosmos/cosmos-sdk/issues/468 - // return sdk.ErrGenesisParse("").TraceCause(err, "") - } - app.accountMapper.SetAccount(ctx, acc) - } - - return abci.ResponseInitChain{} - } -} - -// Custom logic for state export -func (app *MyAwesomeProjectApp) ExportAppStateAndValidators() (appState json.RawMessage, validators []tmtypes.GenesisValidator, err error) { - ctx := app.NewContext(true, abci.Header{}) - - // iterate to get the accounts - accounts := []*types.GenesisAccount{} - appendAccount := func(acc auth.Account) (stop bool) { - account := &types.GenesisAccount{ - Address: acc.GetAddress(), - Coins: acc.GetCoins(), - } - accounts = append(accounts, account) - return false - } - app.accountMapper.IterateAccounts(ctx, appendAccount) - - genState := types.GenesisState{ - Accounts: accounts, - } - appState, err = wire.MarshalJSONIndent(app.cdc, genState) - if err != nil { - return nil, nil, err - } - return appState, validators, nil -} diff --git a/cmd/cosmos-sdk-cli/template/cmd/cli/main.go b/cmd/cosmos-sdk-cli/template/cmd/cli/main.go deleted file mode 100644 index 9ce51c9990..0000000000 --- a/cmd/cosmos-sdk-cli/template/cmd/cli/main.go +++ /dev/null @@ -1,77 +0,0 @@ -package main - -import ( - "os" - - "github.com/spf13/cobra" - - "github.com/tendermint/tendermint/libs/cli" - - "github.com/cosmos/cosmos-sdk/client" - "github.com/cosmos/cosmos-sdk/client/keys" - "github.com/cosmos/cosmos-sdk/client/lcd" - "github.com/cosmos/cosmos-sdk/client/rpc" - "github.com/cosmos/cosmos-sdk/client/tx" - - "github.com/cosmos/cosmos-sdk/version" - authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli" - bankcmd "github.com/cosmos/cosmos-sdk/x/bank/client/cli" - - "github.com/cosmos/cosmos-sdk/cmd/cosmos-sdk-cli/template/app" - "github.com/cosmos/cosmos-sdk/cmd/cosmos-sdk-cli/template/types" -) - -// rootCmd is the entry point for this binary -var ( - rootCmd = &cobra.Command{ - Use: "myawesomeprojectcli", - Short: "MyAwesomeProject light-client", - } -) - -func main() { - // disable sorting - cobra.EnableCommandSorting = false - - // get the codec - cdc := app.MakeCodec() - - // TODO: setup keybase, viper object, etc. to be passed into - // the below functions and eliminate global vars, like we do - // with the cdc - - // add standard rpc, and tx commands - rpc.AddCommands(rootCmd) - rootCmd.AddCommand(client.LineBreak) - tx.AddCommands(rootCmd, cdc) - rootCmd.AddCommand(client.LineBreak) - - // add query/post commands (custom to binary) - // start with commands common to basecoin - rootCmd.AddCommand( - client.GetCommands( - authcmd.GetAccountCmd("acc", cdc, types.GetAccountDecoder(cdc)), - )...) - rootCmd.AddCommand( - client.PostCommands( - bankcmd.SendTxCmd(cdc), - )...) - // and now myawesomeproject specific commands here - - // add proxy, version and key info - rootCmd.AddCommand( - client.LineBreak, - lcd.ServeCommand(cdc), - keys.Commands(), - client.LineBreak, - version.VersionCmd, - ) - - // prepare and add flags - executor := cli.PrepareMainCmd(rootCmd, "BC", os.ExpandEnv("$HOME/.myawesomeprojectcli")) - err := executor.Execute() - if err != nil { - // handle with #870 - panic(err) - } -} diff --git a/cmd/cosmos-sdk-cli/template/cmd/node/main.go b/cmd/cosmos-sdk-cli/template/cmd/node/main.go deleted file mode 100644 index 8a27744309..0000000000 --- a/cmd/cosmos-sdk-cli/template/cmd/node/main.go +++ /dev/null @@ -1,69 +0,0 @@ -package main - -import ( - "encoding/json" - "io" - "os" - - "github.com/spf13/cobra" - - abci "github.com/tendermint/tendermint/abci/types" - "github.com/tendermint/tendermint/libs/cli" - dbm "github.com/tendermint/tendermint/libs/db" - "github.com/tendermint/tendermint/libs/log" - tmtypes "github.com/tendermint/tendermint/types" - - "github.com/cosmos/cosmos-sdk/cmd/cosmos-sdk-cli/template/app" - - "github.com/cosmos/cosmos-sdk/server" - "github.com/cosmos/cosmos-sdk/wire" -) - -// init parameters -var MyAwesomeProjectAppInit = server.AppInit{ - AppGenState: MyAwesomeProjectAppGenState, - AppGenTx: server.SimpleAppGenTx, -} - -// GenAppParams sets up the app_state, append any other app specific components. -func MyAwesomeProjectAppGenState(cdc *wire.Codec, appGenTxs []json.RawMessage) (appState json.RawMessage, err error) { - appState, err = server.SimpleAppGenState(cdc, appGenTxs) - if err != nil { - return - } - - return -} - -func newApp(logger log.Logger, db dbm.DB, _ io.Writer) abci.Application { - return app.NewMyAwesomeProjectApp(logger, db) -} - -func exportAppStateAndTMValidators(logger log.Logger, db dbm.DB, _ io.Writer) (json.RawMessage, []tmtypes.GenesisValidator, error) { - dapp := app.NewMyAwesomeProjectApp(logger, db) - return dapp.ExportAppStateAndValidators() -} - -func main() { - cdc := app.MakeCodec() - ctx := server.NewDefaultContext() - - rootCmd := &cobra.Command{ - Use: "myawesomeprojectd", - Short: "MyAwesomeProject Daemon (server)", - PersistentPreRunE: server.PersistentPreRunEFn(ctx), - } - - server.AddCommands(ctx, cdc, rootCmd, MyAwesomeProjectAppInit, - server.ConstructAppCreator(newApp, "myawesomeproject"), - server.ConstructAppExporter(exportAppStateAndTMValidators, "myawesomeproject")) - - // prepare and add flags - rootDir := os.ExpandEnv("$HOME/.myawesomeprojectd") - executor := cli.PrepareBaseCmd(rootCmd, "BC", rootDir) - err := executor.Execute() - if err != nil { - // handle with #870 - panic(err) - } -} diff --git a/cmd/cosmos-sdk-cli/template/types/account.go b/cmd/cosmos-sdk-cli/template/types/account.go deleted file mode 100644 index 211aef2cf9..0000000000 --- a/cmd/cosmos-sdk-cli/template/types/account.go +++ /dev/null @@ -1,77 +0,0 @@ -package types - -import ( - sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/wire" - "github.com/cosmos/cosmos-sdk/x/auth" -) - -var _ auth.Account = (*AppAccount)(nil) - -// Custom extensions for this application. This is just an example of -// extending auth.BaseAccount with custom fields. -// -// This is compatible with the stock auth.AccountStore, since -// auth.AccountStore uses the flexible go-amino library. -type AppAccount struct { - auth.BaseAccount - Name string `json:"name"` -} - -// Constructor for AppAccount -func ProtoAppAccount() auth.Account { - return &AppAccount{} -} - -// nolint -func (acc AppAccount) GetName() string { return acc.Name } -func (acc *AppAccount) SetName(name string) { acc.Name = name } - -// Get the AccountDecoder function for the custom AppAccount -func GetAccountDecoder(cdc *wire.Codec) auth.AccountDecoder { - return func(accBytes []byte) (res auth.Account, err error) { - if len(accBytes) == 0 { - return nil, sdk.ErrTxDecode("accBytes are empty") - } - acct := new(AppAccount) - err = cdc.UnmarshalBinaryBare(accBytes, &acct) - if err != nil { - panic(err) - } - return acct, err - } -} - -//___________________________________________________________________________________ - -// State to Unmarshal -type GenesisState struct { - Accounts []*GenesisAccount `json:"accounts"` -} - -// GenesisAccount doesn't need pubkey or sequence -type GenesisAccount struct { - Name string `json:"name"` - Address sdk.AccAddress `json:"address"` - Coins sdk.Coins `json:"coins"` -} - -func NewGenesisAccount(aa *AppAccount) *GenesisAccount { - return &GenesisAccount{ - Name: aa.Name, - Address: aa.Address, - Coins: aa.Coins.Sort(), - } -} - -// convert GenesisAccount to AppAccount -func (ga *GenesisAccount) ToAppAccount() (acc *AppAccount, err error) { - baseAcc := auth.BaseAccount{ - Address: ga.Address, - Coins: ga.Coins.Sort(), - } - return &AppAccount{ - BaseAccount: baseAcc, - Name: ga.Name, - }, nil -} diff --git a/tools/Makefile b/tools/Makefile index 5dfbd08fa0..d58f52d1b1 100644 --- a/tools/Makefile +++ b/tools/Makefile @@ -13,7 +13,6 @@ MISSPELL = github.com/client9/misspell/cmd/misspell ERRCHECK = github.com/kisielk/errcheck UNPARAM = mvdan.cc/unparam GOCYCLO = github.com/alecthomas/gocyclo -PACKR = github.com/gobuffalo/packr DEP_CHECK := $(shell command -v dep 2> /dev/null) GOLINT_CHECK := $(shell command -v golint 2> /dev/null) @@ -24,7 +23,6 @@ MISSPELL_CHECK := $(shell command -v misspell 2> /dev/null) ERRCHECK_CHECK := $(shell command -v errcheck 2> /dev/null) UNPARAM_CHECK := $(shell command -v unparam 2> /dev/null) GOCYCLO_CHECK := $(shell command -v gocyclo 2> /dev/null) -PACKR_CHECK := $(shell command -v packr 2> /dev/null) check_tools: ifndef DEP_CHECK @@ -72,11 +70,6 @@ ifndef GOCYCLO_CHECK else @echo "Found gocyclo in path." endif -ifndef PACKR_CHECK - @echo "No packr in path. Install with make get_tools'." -else - @echo "Found packr in path." -endif get_tools: ifdef DEP_CHECK @@ -133,12 +126,6 @@ else @echo "Installing goyclo" go get -v $(GOCYCLO) endif -ifdef PACKR_CHECK - @echo "Packr is already installed. Run 'make update_tools' to update." -else - @echo "$(ansi_grn)Installing packr$(ansi_end)" - go get -v $(PACKR) -endif update_tools: @echo "Updating dep" @@ -159,8 +146,6 @@ update_tools: go get -u -v $(UNPARAM) @echo "Updating goyclo" go get -u -v $(GOCYCLO) - @echo "Updating packr" - go get -u -v $(PACKR) # To avoid unintended conflicts with file names, always add to .PHONY # unless there is a reason not to. From 8edac192e059ed31db570aaf56471def4310c01c Mon Sep 17 00:00:00 2001 From: Sridhar Ganesan Date: Tue, 17 Jul 2018 21:50:25 +0200 Subject: [PATCH 19/31] Moving cli.md to cosmos-sdk-cli.md --- docs/sdk/{cli.md => cosmos-sdk-cli.md} | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) rename docs/sdk/{cli.md => cosmos-sdk-cli.md} (56%) diff --git a/docs/sdk/cli.md b/docs/sdk/cosmos-sdk-cli.md similarity index 56% rename from docs/sdk/cli.md rename to docs/sdk/cosmos-sdk-cli.md index c71d48e0b4..5486230493 100644 --- a/docs/sdk/cli.md +++ b/docs/sdk/cosmos-sdk-cli.md @@ -21,14 +21,14 @@ This will initialize a project, the dependencies, directory structures with the ### Example: ```shell -cosmos-sdk-cli init testzone +cosmos-sdk-cli init testzone -p github.com/your_user_name/testzone ``` +-p remote project path (optional). If this is not provided, it creates testzone under $GOPATH/src/ -This will ask for a remote path for the project - usually github.com/your_user_name/testzone and will create a new testzone application under $GOPATH/src/github.com/your_user_name/testzone along with Makefile ```shell cd $GOPATH/src/github.com/your_user_name/testzone make ``` -This will create two binaries(testzonecli and testzoned) under bin folder. testzoned is the full node of the application which you can run, and testzonecli is your light client. +This will create two binaries(basecli and basecoind) under bin folder. basecoind is the full node of the application which you can run, and basecli is your light client. From 64729bb48d43a760b3fe6ba777c6e85f99e0637c Mon Sep 17 00:00:00 2001 From: Sridhar Ganesan Date: Tue, 17 Jul 2018 21:57:57 +0200 Subject: [PATCH 20/31] Removing unwanted lines in code --- cmd/cosmos-sdk-cli/cmd/init.go | 5 ----- 1 file changed, 5 deletions(-) diff --git a/cmd/cosmos-sdk-cli/cmd/init.go b/cmd/cosmos-sdk-cli/cmd/init.go index 2279dc8a17..b32e4b4121 100644 --- a/cmd/cosmos-sdk-cli/cmd/init.go +++ b/cmd/cosmos-sdk-cli/cmd/init.go @@ -56,11 +56,6 @@ func copyBasecoinTemplate(projectName string, projectPath string, remoteProjectP } return nil }) - - //Copy the entire basecoin directory to the project path. - //os.MkdirAll(projectPath+string(os.PathSeparator)+rootDir, os.ModePerm) - //filePath := projectPath + string(os.PathSeparator) + actualPath - //ioutil.WriteFile(filePath, []byte(contents), os.ModePerm) } func createGopkg(projectPath string) { From 1da8b25847586b236e5d61e63b6b42477771ca11 Mon Sep 17 00:00:00 2001 From: Sridhar Ganesan Date: Wed, 18 Jul 2018 07:58:13 +0200 Subject: [PATCH 21/31] Adding changes to changelog correctly --- CHANGELOG.md | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c6d173a2d1..4009dd5b91 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,14 +2,12 @@ ## PENDING -Features -* [cosmos-sdk-cli] Added support for cosmos-sdk-cli under cosmos-sdk/cmd - This allows SDK users to init a new project repository with a single command. -======= BREAKING CHANGES FEATURES * [lcd] Can now query governance proposals by ProposalStatus +* [cosmos-sdk-cli] Added support for cosmos-sdk-cli under cosmos-sdk/cmd + This allows SDK users to init a new project repository with a single command. IMPROVEMENTS * [baseapp] Allow any alphanumeric character in route From c2e5734c0b310acba874281a92a281dfd3ad3301 Mon Sep 17 00:00:00 2001 From: Sridhar Ganesan Date: Wed, 18 Jul 2018 08:01:49 +0200 Subject: [PATCH 22/31] Removing check function and checking for error in usual way --- cmd/cosmos-sdk-cli/cmd/init.go | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/cmd/cosmos-sdk-cli/cmd/init.go b/cmd/cosmos-sdk-cli/cmd/init.go index b32e4b4121..2008cb8963 100644 --- a/cmd/cosmos-sdk-cli/cmd/init.go +++ b/cmd/cosmos-sdk-cli/cmd/init.go @@ -26,12 +26,6 @@ func resolveProjectPath(remoteProjectPath string) string { return gopath + string(os.PathSeparator) + "src" + string(os.PathSeparator) + remoteProjectPath } -func check(e error) { - if e != nil { - panic(e) - } -} - var remoteBasecoinPath = "github.com/cosmos/cosmos-sdk/examples/basecoin" func copyBasecoinTemplate(projectName string, projectPath string, remoteProjectPath string) { @@ -39,7 +33,9 @@ func copyBasecoinTemplate(projectName string, projectPath string, remoteProjectP filepath.Walk(basecoinProjectPath, func(path string, f os.FileInfo, err error) error { if !f.IsDir() { data, err := ioutil.ReadFile(path) - check(err) + if err != nil { + return err + } contents := string(data) // Extract relative file path eg: app/app.go instead of /Users/..../github.com/cosmos/...examples/basecoin/app/app.go relativeFilePath := path[len(basecoinProjectPath)+1:] From fcab14435d46666b8ca4f3be44327e80fcfb14bb Mon Sep 17 00:00:00 2001 From: Sridhar Ganesan Date: Wed, 18 Jul 2018 10:52:55 +0200 Subject: [PATCH 23/31] Removing extra space that was accidentally added --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 69890355ca..7407b23b50 100644 --- a/Makefile +++ b/Makefile @@ -127,7 +127,7 @@ test_cover: test_lint: gometalinter.v2 --config=tools/gometalinter.json ./... - !(gometalinter.v2 --disable-all --enable='errcheck' --vendor ./... | grep -v "client/" ) + !(gometalinter.v2 --disable-all --enable='errcheck' --vendor ./... | grep -v "client/") find . -name '*.go' -type f -not -path "./vendor*" -not -path "*.git*" | xargs gofmt -d -s dep status >> /dev/null !(grep -n branch Gopkg.toml) From 9c5406891eba0e3f66d4e7848aa65a1a21defbdd Mon Sep 17 00:00:00 2001 From: Sridhar Ganesan Date: Wed, 18 Jul 2018 12:18:57 +0200 Subject: [PATCH 24/31] Changing docs for cosmos-sdk-cli - including $ before beginning shell command --- docs/sdk/cosmos-sdk-cli.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/docs/sdk/cosmos-sdk-cli.md b/docs/sdk/cosmos-sdk-cli.md index 5486230493..2a418534db 100644 --- a/docs/sdk/cosmos-sdk-cli.md +++ b/docs/sdk/cosmos-sdk-cli.md @@ -6,29 +6,29 @@ Create a new blockchain project based on cosmos-sdk with a single command. # Installation ```shell -go get github.com/cosmos/cosmos-sdk -cd $GOPATH/src/github.com/cosmos/cosmos-sdk -make all +$go get github.com/cosmos/cosmos-sdk +$cd $GOPATH/src/github.com/cosmos/cosmos-sdk +$make install_cosmos-sdk-cli ``` This will install a binary cosmos-sdk-cli # Creating a new project -**cosmos-sdk-cli init** _Your-Project-Name_ +**$cosmos-sdk-cli init** _Your-Project-Name_ This will initialize a project, the dependencies, directory structures with the specified project name. ### Example: ```shell -cosmos-sdk-cli init testzone -p github.com/your_user_name/testzone +$cosmos-sdk-cli init testzone -p github.com/your_user_name/testzone ``` -p remote project path (optional). If this is not provided, it creates testzone under $GOPATH/src/ ```shell -cd $GOPATH/src/github.com/your_user_name/testzone -make +$cd $GOPATH/src/github.com/your_user_name/testzone +$make ``` -This will create two binaries(basecli and basecoind) under bin folder. basecoind is the full node of the application which you can run, and basecli is your light client. +This will create two binaries(testzonecli and testzoned) under bin folder. testzoned is the full node of the application which you can run, and testzonecli is your light client. From 56e61e3f5a75d874df677d02dd2a3028bac7fb1f Mon Sep 17 00:00:00 2001 From: Sridhar Ganesan Date: Wed, 18 Jul 2018 13:44:29 +0200 Subject: [PATCH 25/31] Adding other dependencies to gopkg, moving code, fixing printf --- cmd/cosmos-sdk-cli/cmd/init.go | 46 ++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 19 deletions(-) diff --git a/cmd/cosmos-sdk-cli/cmd/init.go b/cmd/cosmos-sdk-cli/cmd/init.go index 2008cb8963..b44c2be343 100644 --- a/cmd/cosmos-sdk-cli/cmd/init.go +++ b/cmd/cosmos-sdk-cli/cmd/init.go @@ -8,15 +8,36 @@ import ( "strings" "github.com/cosmos/cosmos-sdk/version" + tmversion "github.com/tendermint/tendermint/version" "github.com/spf13/cobra" "path/filepath" ) +var remoteProjectPath string + func init() { initCmd.Flags().StringVarP(&remoteProjectPath, "project-path", "p", "", "Remote project path. eg: github.com/your_user_name/project_name") rootCmd.AddCommand(initCmd) } +var initCmd = &cobra.Command{ + Use: "init [ProjectName]", + Short: "Initialize your new cosmos zone", + Args: cobra.ExactArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + fmt.Print("Thanks for choosing Cosmos-SDK to build your project.\n\n") + projectName := args[0] + shortProjectName := strings.ToLower(projectName) + remoteProjectPath = strings.ToLower(strings.TrimSpace(remoteProjectPath)) + if remoteProjectPath == "" { + remoteProjectPath = strings.ToLower(shortProjectName) + } + setupBasecoinWorkspace(shortProjectName, remoteProjectPath) + return nil + }, +} + + func resolveProjectPath(remoteProjectPath string) string { gopath := os.Getenv("GOPATH") if gopath == "" { @@ -57,10 +78,14 @@ func copyBasecoinTemplate(projectName string, projectPath string, remoteProjectP func createGopkg(projectPath string) { // Create gopkg.toml file dependencies := map[string]string{ - "github.com/cosmos/cosmos-sdk": version.Version, + "github.com/cosmos/cosmos-sdk": "=" + version.Version, + "github.com/stretchr/testify": "=1.2.1", + "github.com/spf13/cobra": "=0.0.1", + "github.com/spf13/viper": "=1.0.0", } overrides := map[string]string{ "github.com/golang/protobuf": "1.1.0", + "github.com/tendermint/tendermint": tmversion.Version, } contents := "" for dependency, version := range dependencies { @@ -106,23 +131,6 @@ func setupBasecoinWorkspace(projectName string, remoteProjectPath string) { copyBasecoinTemplate(projectName, projectPath, remoteProjectPath) createGopkg(projectPath) createMakefile(projectPath) - fmt.Println("\nInitialized a new project at " + projectPath + ".\nHappy hacking!") + fmt.Printf("\nInitialized a new project at %s.\nHappy hacking!\n", projectPath) } -var remoteProjectPath string -var initCmd = &cobra.Command{ - Use: "init [ProjectName]", - Short: "Initialize your new cosmos zone", - Args: cobra.ExactArgs(1), - RunE: func(cmd *cobra.Command, args []string) error { - fmt.Print("Thanks for choosing Cosmos-SDK to build your project.\n\n") - projectName := args[0] - shortProjectName := strings.ToLower(projectName) - remoteProjectPath = strings.ToLower(strings.TrimSpace(remoteProjectPath)) - if remoteProjectPath == "" { - remoteProjectPath = strings.ToLower(shortProjectName) - } - setupBasecoinWorkspace(shortProjectName, remoteProjectPath) - return nil - }, -} From 0698a9535b1e727bdf849f6afebbdddcf5afcbda Mon Sep 17 00:00:00 2001 From: Sridhar Ganesan Date: Wed, 18 Jul 2018 18:36:49 +0200 Subject: [PATCH 26/31] Renaming Basecoin and other base* to projectname --- cmd/cosmos-sdk-cli/cmd/init.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/cmd/cosmos-sdk-cli/cmd/init.go b/cmd/cosmos-sdk-cli/cmd/init.go index b44c2be343..937ada5265 100644 --- a/cmd/cosmos-sdk-cli/cmd/init.go +++ b/cmd/cosmos-sdk-cli/cmd/init.go @@ -13,6 +13,8 @@ import ( "path/filepath" ) +var remoteBasecoinPath = "github.com/cosmos/cosmos-sdk/examples/basecoin" +var replacer *strings.Replacer var remoteProjectPath string func init() { @@ -27,11 +29,13 @@ var initCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) error { fmt.Print("Thanks for choosing Cosmos-SDK to build your project.\n\n") projectName := args[0] + capitalizedProjectName := strings.Title(projectName) shortProjectName := strings.ToLower(projectName) remoteProjectPath = strings.ToLower(strings.TrimSpace(remoteProjectPath)) if remoteProjectPath == "" { remoteProjectPath = strings.ToLower(shortProjectName) } + replacer = strings.NewReplacer("basecli", shortProjectName + "cli", "basecoind", shortProjectName + "d", "BasecoinApp", capitalizedProjectName + "App", "github.com/cosmos/cosmos-sdk/examples/basecoin/", remoteProjectPath + "/", "basecoin", shortProjectName) setupBasecoinWorkspace(shortProjectName, remoteProjectPath) return nil }, @@ -47,8 +51,6 @@ func resolveProjectPath(remoteProjectPath string) string { return gopath + string(os.PathSeparator) + "src" + string(os.PathSeparator) + remoteProjectPath } -var remoteBasecoinPath = "github.com/cosmos/cosmos-sdk/examples/basecoin" - func copyBasecoinTemplate(projectName string, projectPath string, remoteProjectPath string) { basecoinProjectPath := resolveProjectPath(remoteBasecoinPath) filepath.Walk(basecoinProjectPath, func(path string, f os.FileInfo, err error) error { @@ -62,6 +64,7 @@ func copyBasecoinTemplate(projectName string, projectPath string, remoteProjectP relativeFilePath := path[len(basecoinProjectPath)+1:] // Evaluating the filepath in the new project folder projectFilePath := projectPath + string(os.PathSeparator) + relativeFilePath + projectFilePath = replacer.Replace(projectFilePath) lengthOfRootDir := strings.LastIndex(projectFilePath, string(os.PathSeparator)) // Extracting the path of root directory from the filepath rootDir := projectFilePath[0:lengthOfRootDir] @@ -69,6 +72,7 @@ func copyBasecoinTemplate(projectName string, projectPath string, remoteProjectP os.MkdirAll(rootDir, os.ModePerm) fmt.Println("Creating " + projectFilePath) // Writing the contents to a file in the project folder + contents = replacer.Replace(contents) ioutil.WriteFile(projectFilePath, []byte(contents), os.ModePerm) } return nil @@ -121,6 +125,7 @@ benchmark: @go test -bench=. $(PACKAGES) .PHONY: all build test benchmark` + makefileContents = replacer.Replace(makefileContents) ioutil.WriteFile(projectPath+"/Makefile", []byte(makefileContents), os.ModePerm) } From 7255f8260cfe7c64f2203806c8f105c5440bbdc0 Mon Sep 17 00:00:00 2001 From: Sridhar Ganesan Date: Wed, 18 Jul 2018 18:39:58 +0200 Subject: [PATCH 27/31] Gofmt init.go --- cmd/cosmos-sdk-cli/cmd/init.go | 42 ++++++++++++++++------------------ 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/cmd/cosmos-sdk-cli/cmd/init.go b/cmd/cosmos-sdk-cli/cmd/init.go index 937ada5265..067afb91c0 100644 --- a/cmd/cosmos-sdk-cli/cmd/init.go +++ b/cmd/cosmos-sdk-cli/cmd/init.go @@ -8,8 +8,8 @@ import ( "strings" "github.com/cosmos/cosmos-sdk/version" - tmversion "github.com/tendermint/tendermint/version" "github.com/spf13/cobra" + tmversion "github.com/tendermint/tendermint/version" "path/filepath" ) @@ -23,25 +23,24 @@ func init() { } var initCmd = &cobra.Command{ - Use: "init [ProjectName]", - Short: "Initialize your new cosmos zone", - Args: cobra.ExactArgs(1), - RunE: func(cmd *cobra.Command, args []string) error { - fmt.Print("Thanks for choosing Cosmos-SDK to build your project.\n\n") - projectName := args[0] + Use: "init [ProjectName]", + Short: "Initialize your new cosmos zone", + Args: cobra.ExactArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + fmt.Print("Thanks for choosing Cosmos-SDK to build your project.\n\n") + projectName := args[0] capitalizedProjectName := strings.Title(projectName) - shortProjectName := strings.ToLower(projectName) - remoteProjectPath = strings.ToLower(strings.TrimSpace(remoteProjectPath)) - if remoteProjectPath == "" { - remoteProjectPath = strings.ToLower(shortProjectName) - } - replacer = strings.NewReplacer("basecli", shortProjectName + "cli", "basecoind", shortProjectName + "d", "BasecoinApp", capitalizedProjectName + "App", "github.com/cosmos/cosmos-sdk/examples/basecoin/", remoteProjectPath + "/", "basecoin", shortProjectName) - setupBasecoinWorkspace(shortProjectName, remoteProjectPath) - return nil - }, + shortProjectName := strings.ToLower(projectName) + remoteProjectPath = strings.ToLower(strings.TrimSpace(remoteProjectPath)) + if remoteProjectPath == "" { + remoteProjectPath = strings.ToLower(shortProjectName) + } + replacer = strings.NewReplacer("basecli", shortProjectName+"cli", "basecoind", shortProjectName+"d", "BasecoinApp", capitalizedProjectName+"App", "github.com/cosmos/cosmos-sdk/examples/basecoin/", remoteProjectPath+"/", "basecoin", shortProjectName) + setupBasecoinWorkspace(shortProjectName, remoteProjectPath) + return nil + }, } - func resolveProjectPath(remoteProjectPath string) string { gopath := os.Getenv("GOPATH") if gopath == "" { @@ -83,12 +82,12 @@ func createGopkg(projectPath string) { // Create gopkg.toml file dependencies := map[string]string{ "github.com/cosmos/cosmos-sdk": "=" + version.Version, - "github.com/stretchr/testify": "=1.2.1", - "github.com/spf13/cobra": "=0.0.1", - "github.com/spf13/viper": "=1.0.0", + "github.com/stretchr/testify": "=1.2.1", + "github.com/spf13/cobra": "=0.0.1", + "github.com/spf13/viper": "=1.0.0", } overrides := map[string]string{ - "github.com/golang/protobuf": "1.1.0", + "github.com/golang/protobuf": "1.1.0", "github.com/tendermint/tendermint": tmversion.Version, } contents := "" @@ -138,4 +137,3 @@ func setupBasecoinWorkspace(projectName string, remoteProjectPath string) { createMakefile(projectPath) fmt.Printf("\nInitialized a new project at %s.\nHappy hacking!\n", projectPath) } - From b7783a8a26c47fdb1f7d179a21bbb97813bb6574 Mon Sep 17 00:00:00 2001 From: svaishnavy Date: Wed, 18 Jul 2018 21:20:38 +0200 Subject: [PATCH 28/31] Making suggested changes --- docs/sdk/cosmos-sdk-cli.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/docs/sdk/cosmos-sdk-cli.md b/docs/sdk/cosmos-sdk-cli.md index 2a418534db..2cc0182cf5 100644 --- a/docs/sdk/cosmos-sdk-cli.md +++ b/docs/sdk/cosmos-sdk-cli.md @@ -6,9 +6,9 @@ Create a new blockchain project based on cosmos-sdk with a single command. # Installation ```shell -$go get github.com/cosmos/cosmos-sdk -$cd $GOPATH/src/github.com/cosmos/cosmos-sdk -$make install_cosmos-sdk-cli +$ go get github.com/cosmos/cosmos-sdk +$ cd $GOPATH/src/github.com/cosmos/cosmos-sdk +$ make install_cosmos-sdk-cli ``` This will install a binary cosmos-sdk-cli @@ -21,14 +21,14 @@ This will initialize a project, the dependencies, directory structures with the ### Example: ```shell -$cosmos-sdk-cli init testzone -p github.com/your_user_name/testzone +$ cosmos-sdk-cli init testzone -p github.com/your_user_name/testzone ``` --p remote project path (optional). If this is not provided, it creates testzone under $GOPATH/src/ +```-p [remote-project-path]```. If this is not provided, it creates testzone under $GOPATH/src/ ```shell -$cd $GOPATH/src/github.com/your_user_name/testzone -$make +$ cd $GOPATH/src/github.com/your_user_name/testzone +$ make ``` This will create two binaries(testzonecli and testzoned) under bin folder. testzoned is the full node of the application which you can run, and testzonecli is your light client. From 1d9415eae072cd3f1b526de24ca895d9a8119eff Mon Sep 17 00:00:00 2001 From: svaishnavy Date: Wed, 18 Jul 2018 21:37:11 +0200 Subject: [PATCH 29/31] Replacing with a single backtick --- docs/sdk/cosmos-sdk-cli.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/sdk/cosmos-sdk-cli.md b/docs/sdk/cosmos-sdk-cli.md index 2cc0182cf5..615d722522 100644 --- a/docs/sdk/cosmos-sdk-cli.md +++ b/docs/sdk/cosmos-sdk-cli.md @@ -23,7 +23,7 @@ This will initialize a project, the dependencies, directory structures with the ```shell $ cosmos-sdk-cli init testzone -p github.com/your_user_name/testzone ``` -```-p [remote-project-path]```. If this is not provided, it creates testzone under $GOPATH/src/ +`-p [remote-project-path]`. If this is not provided, it creates testzone under $GOPATH/src/ ```shell From e572513d9102f1b84220fabaaa47fc2a9f1e8f10 Mon Sep 17 00:00:00 2001 From: Sridhar Ganesan Date: Wed, 18 Jul 2018 22:06:08 +0200 Subject: [PATCH 30/31] Adding comments for variables and refactoring how replacer is initialized, adding comment about tools/ structure --- cmd/cosmos-sdk-cli/cmd/init.go | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/cmd/cosmos-sdk-cli/cmd/init.go b/cmd/cosmos-sdk-cli/cmd/init.go index 067afb91c0..5a0f3ddb38 100644 --- a/cmd/cosmos-sdk-cli/cmd/init.go +++ b/cmd/cosmos-sdk-cli/cmd/init.go @@ -14,7 +14,12 @@ import ( ) var remoteBasecoinPath = "github.com/cosmos/cosmos-sdk/examples/basecoin" + +// Replacer to replace all instances of basecoin/basecli/BasecoinApp to project specific names +// Gets initialized when initCmd is executing after getting the project name from user var replacer *strings.Replacer + +// Remote path for the project. var remoteProjectPath string func init() { @@ -35,7 +40,11 @@ var initCmd = &cobra.Command{ if remoteProjectPath == "" { remoteProjectPath = strings.ToLower(shortProjectName) } - replacer = strings.NewReplacer("basecli", shortProjectName+"cli", "basecoind", shortProjectName+"d", "BasecoinApp", capitalizedProjectName+"App", "github.com/cosmos/cosmos-sdk/examples/basecoin/", remoteProjectPath+"/", "basecoin", shortProjectName) + replacer = strings.NewReplacer("basecli", shortProjectName+"cli", + "basecoind", shortProjectName+"d", + "BasecoinApp", capitalizedProjectName+"App", + remoteBasecoinPath, remoteProjectPath, + "basecoin", shortProjectName) setupBasecoinWorkspace(shortProjectName, remoteProjectPath) return nil }, @@ -103,6 +112,7 @@ func createGopkg(projectPath string) { func createMakefile(projectPath string) { // Create makefile + // TODO: Should we use tools/ directory as in Cosmos-SDK to get tools for linting etc. makefileContents := `PACKAGES=$(shell go list ./... | grep -v '/vendor/') all: get_tools get_vendor_deps build test @@ -124,7 +134,10 @@ benchmark: @go test -bench=. $(PACKAGES) .PHONY: all build test benchmark` + + // Replacing instances of base* to project specific names makefileContents = replacer.Replace(makefileContents) + ioutil.WriteFile(projectPath+"/Makefile", []byte(makefileContents), os.ModePerm) } From fab4600c989878fee8a7a665df89767047d24ff7 Mon Sep 17 00:00:00 2001 From: Rigel Date: Wed, 18 Jul 2018 21:32:09 -0400 Subject: [PATCH 31/31] Update PENDING.md --- PENDING.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/PENDING.md b/PENDING.md index 3d6d0d92de..f297e6a4af 100644 --- a/PENDING.md +++ b/PENDING.md @@ -6,6 +6,8 @@ BREAKING CHANGES FEATURES * [lcd] Can now query governance proposals by ProposalStatus +* Added support for cosmos-sdk-cli tool under cosmos-sdk/cmd + * This allows SDK users to init a new project repository with a single command. IMPROVEMENTS * [baseapp] Allow any alphanumeric character in route