From 566c3aeb6034684cf9661cd74f3a34dfef830fb4 Mon Sep 17 00:00:00 2001 From: Peter Chung Date: Sun, 12 May 2019 14:56:33 +0800 Subject: [PATCH 1/3] [wallet] embed wallet.ini into wallet binary (#830) utilize go:generate to convert the default wallet.ini file into go constant `defaultWalletIni` defined in `cmd/client/wallet/generated_ini.go` which will be used when `.hmy/wallet.ini` doesn't exist under the current filesystem. --- cmd/client/wallet/generated_ini.go | 44 ++++++++++++++++++++++++++++++ cmd/client/wallet/main.go | 15 +++++++++- go.mod | 1 + internal/utils/configfile.go | 4 +-- internal/utils/configfile_test.go | 10 +++++-- scripts/go_executable_build.sh | 2 +- scripts/wallet_embed_ini_files.go | 30 ++++++++++++++++++++ 7 files changed, 100 insertions(+), 6 deletions(-) create mode 100644 cmd/client/wallet/generated_ini.go create mode 100644 scripts/wallet_embed_ini_files.go diff --git a/cmd/client/wallet/generated_ini.go b/cmd/client/wallet/generated_ini.go new file mode 100644 index 000000000..f9f1d73f1 --- /dev/null +++ b/cmd/client/wallet/generated_ini.go @@ -0,0 +1,44 @@ +package main + +const ( + defaultWalletIni = `[default] +bootnode = /ip4/100.26.90.187/tcp/9876/p2p/QmZJJx6AdaoEkGLrYG4JeLCKeCKDjnFz2wfHNHxAqFSGA9 +bootnode = /ip4/54.213.43.194/tcp/9876/p2p/QmQayinFSgMMw5cSpDUiD9pQ2WeP6WNmGxpZ6ou3mdVFJX +shards = 1 + +[default.shard0.rpc] +rpc = 34.217.179.222:14555 +rpc = 18.209.247.105:14555 +rpc = 100.25.248.42:14555 +rpc = 3.80.164.193:14555 +rpc = 54.87.237.93:14555 + +[local] +bootnode = /ip4/127.0.0.1/tcp/19876/p2p/Qmc1V6W7BwX8Ugb42Ti8RnXF1rY5PF7nnZ6bKBryCgi6cv +shards = 1 + +[local.shard0.rpc] +rpc = 127.0.0.1:14555 +rpc = 127.0.0.1:14556 + +[devnet] +bootnode = /ip4/100.26.90.187/tcp/9871/p2p/Qmdfjtk6hPoyrH1zVD9PEH4zfWLo38dP2mDvvKXfh3tnEv +bootnode = /ip4/54.213.43.194/tcp/9871/p2p/QmRVbTpEYup8dSaURZfF6ByrMTSKa4UyUzJhSjahFzRqNj +shards = 3 + +[devnet.shard0.rpc] +rpc = 13.57.196.136:14555 +rpc = 35.175.103.144:14555 +rpc = 54.245.176.36:14555 + +[devnet.shard1.rpc] +rpc = 35.163.188.234:14555 +rpc = 54.215.251.123:14555 +rpc = 54.153.11.146:14555 + +[devnet.shard2.rpc] +rpc = 52.201.246.212:14555 +rpc = 3.81.26.139:14555 +rpc = 18.237.42.209:14555 +` +) diff --git a/cmd/client/wallet/main.go b/cmd/client/wallet/main.go index 4b3c659d4..578ff88b5 100644 --- a/cmd/client/wallet/main.go +++ b/cmd/client/wallet/main.go @@ -197,10 +197,23 @@ ARG: } } +//go:generate go run ../../../scripts/wallet_embed_ini_files.go + func readProfile(profile string) { fmt.Printf("Using %s profile for wallet\n", profile) + + // try to load .hmy/wallet.ini from filesystem + // use default_wallet_ini if .hmy/wallet.ini doesn't exist var err error - walletProfile, err = utils.ReadWalletProfile(defaultConfigFile, profile) + var iniBytes []byte + + iniBytes, err = ioutil.ReadFile(defaultConfigFile) + if err != nil { + log.Debug(fmt.Sprintf("%s doesn't exist, using default ini\n", defaultConfigFile)) + iniBytes = []byte(defaultWalletIni) + } + + walletProfile, err = utils.ReadWalletProfile(iniBytes, profile) if err != nil { fmt.Printf("Read wallet profile error: %v\nExiting ...\n", err) os.Exit(2) diff --git a/go.mod b/go.mod index 0f547470c..ef6419f16 100644 --- a/go.mod +++ b/go.mod @@ -23,6 +23,7 @@ require ( github.com/harmony-ek/gencodec v0.0.0-20190215044613-e6740dbdd846 github.com/harmony-one/bls v0.0.1 github.com/hashicorp/golang-lru v0.5.1 + github.com/iancoleman/strcase v0.0.0-20190422225806-e506e3ef7365 github.com/ipfs/go-datastore v0.0.1 github.com/ipfs/go-log v0.0.1 github.com/karalabe/hid v0.0.0-20181128192157-d815e0c1a2e2 // indirect diff --git a/internal/utils/configfile.go b/internal/utils/configfile.go index 369fe522d..036f50358 100644 --- a/internal/utils/configfile.go +++ b/internal/utils/configfile.go @@ -18,8 +18,8 @@ type WalletProfile struct { } // ReadWalletProfile reads an ini file and return WalletProfile -func ReadWalletProfile(fn string, profile string) (*WalletProfile, error) { - cfg, err := ini.ShadowLoad(fn) +func ReadWalletProfile(iniBytes []byte, profile string) (*WalletProfile, error) { + cfg, err := ini.ShadowLoad(iniBytes) if err != nil { return nil, err } diff --git a/internal/utils/configfile_test.go b/internal/utils/configfile_test.go index bff0f4c03..2e7a08d67 100644 --- a/internal/utils/configfile_test.go +++ b/internal/utils/configfile_test.go @@ -1,6 +1,7 @@ package utils import ( + "io/ioutil" "reflect" "testing" @@ -95,14 +96,19 @@ func TestReadWalletProfile(t *testing.T) { }, } - config1, err := ReadWalletProfile("test.ini", "default") + testIniBytes, err := ioutil.ReadFile("test.ini") + if err != nil { + t.Fatalf("Failed to read test.ini: %v", err) + } + + config1, err := ReadWalletProfile(testIniBytes, "default") if err != nil { t.Fatalf("ReadWalletProfile Error: %v", err) } if !reflect.DeepEqual(config[0], config1) { t.Errorf("Got: %v\nExpect: %v\n", config1, config[0]) } - config2, err := ReadWalletProfile("test.ini", "testnet") + config2, err := ReadWalletProfile(testIniBytes, "testnet") if err != nil { t.Fatalf("ReadWalletProfile Error: %v", err) } diff --git a/scripts/go_executable_build.sh b/scripts/go_executable_build.sh index 5680f504f..e3862172a 100755 --- a/scripts/go_executable_build.sh +++ b/scripts/go_executable_build.sh @@ -6,7 +6,7 @@ declare -A SRC SRC[harmony]=cmd/harmony/main.go SRC[txgen]=cmd/client/txgen/main.go SRC[bootnode]=cmd/bootnode/main.go -SRC[wallet]=cmd/client/wallet/main.go +SRC[wallet]="cmd/client/wallet/main.go cmd/client/wallet/generated_ini.go" BINDIR=bin BUCKET=unique-bucket-bin diff --git a/scripts/wallet_embed_ini_files.go b/scripts/wallet_embed_ini_files.go new file mode 100644 index 000000000..0d9d732e6 --- /dev/null +++ b/scripts/wallet_embed_ini_files.go @@ -0,0 +1,30 @@ +package main + +import ( + "io" + "io/ioutil" + "os" + "path" + "strings" + + "github.com/iancoleman/strcase" +) + +// Reads all .ini files in the `.hmy` folder of root path +// then encodes them as strings literals in `cmd/client/wallet/generated_ini.go` +func main() { + const root = "../../../" + hmy := path.Join(root, ".hmy") + fs, _ := ioutil.ReadDir(hmy) + out, _ := os.Create("generated_ini.go") + out.Write([]byte("package main\n\nconst (\n")) + for _, f := range fs { + if strings.HasSuffix(f.Name(), ".ini") { + out.Write([]byte("\t" + strcase.ToLowerCamel("default_"+strings.TrimSuffix(f.Name(), ".ini")+"_ini") + " = `")) + f, _ := os.Open(path.Join(hmy, f.Name())) + io.Copy(out, f) + out.Write([]byte("`\n")) + } + } + out.Write([]byte(")\n")) +} From 7d101692aa7cd65c69b6be9280126fe6572d0c6b Mon Sep 17 00:00:00 2001 From: Peter Chung Date: Mon, 13 May 2019 13:36:59 +0800 Subject: [PATCH 2/3] [internal/utils] add EmbedFile function utils.EmbedFile is a text file embed script for go:generate to embed text files into generated golang source. related to #830 --- ...nerated_ini.go => generated_wallet.ini.go} | 0 internal/utils/file_embed.go | 64 +++++++++++++++++++ scripts/wallet_embed_ini_files.go | 28 +------- 3 files changed, 67 insertions(+), 25 deletions(-) rename cmd/client/wallet/{generated_ini.go => generated_wallet.ini.go} (100%) create mode 100644 internal/utils/file_embed.go diff --git a/cmd/client/wallet/generated_ini.go b/cmd/client/wallet/generated_wallet.ini.go similarity index 100% rename from cmd/client/wallet/generated_ini.go rename to cmd/client/wallet/generated_wallet.ini.go diff --git a/internal/utils/file_embed.go b/internal/utils/file_embed.go new file mode 100644 index 000000000..c5cc63ab7 --- /dev/null +++ b/internal/utils/file_embed.go @@ -0,0 +1,64 @@ +package utils + +import ( + "fmt" + "io" + "os" + "path/filepath" + + "github.com/iancoleman/strcase" +) + +// EmbedFile Text file embed script for go:generate. +// This script embeds a text file located at filePath into a string constant +// named as constName defined in a golang source file located at the current +// go:generate path. +func EmbedFile(filePath string, constName string) { + // validate inputs + if _, err := os.Stat(filePath); os.IsNotExist(err) { + panic(fmt.Sprintf("File %s does not exist", filePath)) + } + + if strcase.ToLowerCamel(constName) != constName { + panic(fmt.Sprintf("constName %s is not in lower camel-case", constName)) + } + + // generate go file + var err error + fileName := filepath.Base(filePath) + generatedFileName := "generated_" + strcase.ToSnake(fileName) + ".go" + out, err := os.Create(generatedFileName) + if err != nil { + panic(err) + } + + _, err = out.Write([]byte(fmt.Sprintf("package %s\n\nconst (\n", os.Getenv("GOPACKAGE")))) + if err != nil { + panic(err) + } + + _, err = out.Write([]byte("\t" + constName + " = `")) + if err != nil { + panic(err) + } + + f, err := os.Open(filePath) + if err != nil { + panic(err) + } + + _, err = io.Copy(out, f) + if err != nil { + panic(err) + } + + _, err = out.Write([]byte("`\n")) + if err != nil { + panic(err) + } + + _, err = out.Write([]byte(")\n")) + if err != nil { + panic(err) + } +} diff --git a/scripts/wallet_embed_ini_files.go b/scripts/wallet_embed_ini_files.go index 0d9d732e6..9b50bdab9 100644 --- a/scripts/wallet_embed_ini_files.go +++ b/scripts/wallet_embed_ini_files.go @@ -1,30 +1,8 @@ package main -import ( - "io" - "io/ioutil" - "os" - "path" - "strings" +import "github.com/harmony-one/harmony/internal/utils" - "github.com/iancoleman/strcase" -) - -// Reads all .ini files in the `.hmy` folder of root path -// then encodes them as strings literals in `cmd/client/wallet/generated_ini.go` +// Embed the default wallet.ini file into defaultWalletIni string literal constant func main() { - const root = "../../../" - hmy := path.Join(root, ".hmy") - fs, _ := ioutil.ReadDir(hmy) - out, _ := os.Create("generated_ini.go") - out.Write([]byte("package main\n\nconst (\n")) - for _, f := range fs { - if strings.HasSuffix(f.Name(), ".ini") { - out.Write([]byte("\t" + strcase.ToLowerCamel("default_"+strings.TrimSuffix(f.Name(), ".ini")+"_ini") + " = `")) - f, _ := os.Open(path.Join(hmy, f.Name())) - io.Copy(out, f) - out.Write([]byte("`\n")) - } - } - out.Write([]byte(")\n")) + utils.EmbedFile("../../../.hmy/wallet.ini", "defaultWalletIni") } From c3860b750c875ad7d2c1add1eed9f78900fff803 Mon Sep 17 00:00:00 2001 From: Peter Chung Date: Tue, 14 May 2019 03:56:36 +0800 Subject: [PATCH 3/3] [scripts] fix wallet build script --- scripts/go_executable_build.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/go_executable_build.sh b/scripts/go_executable_build.sh index e3862172a..3cfcd8859 100755 --- a/scripts/go_executable_build.sh +++ b/scripts/go_executable_build.sh @@ -6,7 +6,7 @@ declare -A SRC SRC[harmony]=cmd/harmony/main.go SRC[txgen]=cmd/client/txgen/main.go SRC[bootnode]=cmd/bootnode/main.go -SRC[wallet]="cmd/client/wallet/main.go cmd/client/wallet/generated_ini.go" +SRC[wallet]="cmd/client/wallet/main.go cmd/client/wallet/generated_wallet.ini.go" BINDIR=bin BUCKET=unique-bucket-bin