Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ioctl] create main for ioctl/newcmd #3296

Merged
merged 41 commits into from
Jun 23, 2022
Merged
Show file tree
Hide file tree
Changes from 39 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
9e53445
create main for ioctl/newcmd
huof6829 Apr 13, 2022
a03f342
add comment
huof6829 Apr 13, 2022
f27e99c
add comment
huof6829 Apr 13, 2022
c09614c
Merge branch 'master' into dev_ioctl_main
huof6829 Apr 15, 2022
ca91803
add make newioctl
huof6829 Apr 15, 2022
d2a241f
Merge branch 'master' into dev_ioctl_main
huof6829 Apr 22, 2022
9383cf3
use SelectTranslation
huof6829 Apr 22, 2022
75bdd5a
Merge branch 'master' into dev_ioctl_main
huof6829 Apr 24, 2022
70e895c
mkdir tools/newioctl
huof6829 Apr 24, 2022
748931a
Merge branch 'master' into dev_ioctl_main
huof6829 Apr 26, 2022
381522e
Merge branch 'master' into dev_ioctl_main
huof6829 Apr 26, 2022
d8a0e50
Merge branch 'master' into dev_ioctl_main
huof6829 Apr 29, 2022
b843f92
Merge branch 'master' into dev_ioctl_main
huof6829 Apr 29, 2022
b162956
Merge branch 'master' into dev_ioctl_main
huof6829 May 5, 2022
d923f3d
Merge branch 'iotexproject:master' into dev_ioctl_main
huof6829 May 6, 2022
968902b
Merge branch 'master' into dev_ioctl_main
huof6829 May 7, 2022
a44f6f3
Merge branch 'master' into dev_ioctl_main
huof6829 May 12, 2022
1b3c14b
Merge branch 'master' into dev_ioctl_main
huof6829 May 12, 2022
59e6d42
Merge branch 'master' into dev_ioctl_main
huof6829 May 13, 2022
233c6b6
Merge branch 'master' into dev_ioctl_main
huof6829 May 14, 2022
563bbde
Merge branch 'master' into dev_ioctl_main
huof6829 May 18, 2022
e6912a1
Merge branch 'master' into dev_ioctl_main
huof6829 May 23, 2022
24f9fb9
Merge branch 'master' into dev_ioctl_main
huof6829 May 23, 2022
45172d1
Merge branch 'master' into dev_ioctl_main
huof6829 May 24, 2022
3d59975
Merge branch 'master' into dev_ioctl_main
huof6829 May 26, 2022
bfce58b
add _ in file name
huof6829 May 26, 2022
4c24ee8
Merge branch 'master' into dev_ioctl_main
huof6829 May 27, 2022
fd20e2b
Merge branch 'master' into dev_ioctl_main
huof6829 May 31, 2022
9a49780
Merge branch 'master' into dev_ioctl_main
huof6829 Jun 6, 2022
7219bf2
Merge branch 'master' into dev_ioctl_main
huof6829 Jun 13, 2022
23254d6
Merge branch 'master' into dev_ioctl_main
huof6829 Jun 14, 2022
bb11a7f
modify filename
huof6829 Jun 14, 2022
594fb24
return err for InitConfig()
huof6829 Jun 14, 2022
461b5bc
delete sub command function for new pr creating
huof6829 Jun 14, 2022
bc0b05c
Merge branch 'master' into dev_ioctl_main
huof6829 Jun 15, 2022
bc68fed
Merge branch 'master' into dev_ioctl_main
huof6829 Jun 17, 2022
ed3f8fc
Merge branch 'master' into dev_ioctl_main
huof6829 Jun 20, 2022
3d915ba
Merge branch 'master' into dev_ioctl_main
huof6829 Jun 20, 2022
4d7d30e
modify InitConfig return value
huof6829 Jun 20, 2022
9a75119
move panic from InitConfig() inside to caller aside
huof6829 Jun 22, 2022
3f2bb7a
add test
huof6829 Jun 22, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ BUILD_TARGET_SERVER=server
BUILD_TARGET_ACTINJV2=actioninjectorv2
BUILD_TARGET_ADDRGEN=addrgen
BUILD_TARGET_IOCTL=ioctl
BUILD_TARGET_NEWIOCTL=newioctl
BUILD_TARGET_XCTL=xctl
BUILD_TARGET_NEWXCTL=newxctl
BUILD_TARGET_MINICLUSTER=minicluster
BUILD_TARGET_RECOVER=recover
BUILD_TARGET_IOMIGRATER=iomigrater
Expand Down Expand Up @@ -218,6 +220,10 @@ recover:
.PHONY: ioctl
ioctl:
$(GOBUILD) -ldflags "$(PackageFlags)" -o ./bin/$(BUILD_TARGET_IOCTL) -v ./tools/ioctl

.PHONY: newioctl
huof6829 marked this conversation as resolved.
Show resolved Hide resolved
newioctl:
$(GOBUILD) -ldflags "$(PackageFlags)" -o ./bin/$(BUILD_TARGET_NEWIOCTL) -v ./tools/newioctl

.PHONY: ioctl-cross
ioctl-cross:
Expand All @@ -229,10 +235,24 @@ ioctl-cross:
cd $(GOPATH)/src && $(GOPATH)/bin/xgo --targets=$(BUILD_TARGET_OS)/$(BUILD_TARGET_ARCH) .
mkdir -p ./bin/$(BUILD_TARGET_IOCTL) && sudo mv $(GOPATH)/src/main-$(BUILD_TARGET_OS)-$(BUILD_TARGET_ARCH) ./bin/$(BUILD_TARGET_IOCTL)/ioctl-$(BUILD_TARGET_OS)-$(BUILD_TARGET_ARCH)

.PHONY: newioctl-cross
newioctl-cross:
$(DOCKERCMD) pull techknowlogick/xgo:latest
$(GOCMD) get src.techknowlogick.com/xgo
mkdir -p $(GOPATH)/src
sudo cp ./tools/newioctl/ioctl.go $(GOPATH)/src
cd $(GOPATH)/src && sudo rm -f go.mod && $(GOCMD) mod init main && $(GOCMD) mod tidy
cd $(GOPATH)/src && $(GOPATH)/bin/xgo --targets=$(BUILD_TARGET_OS)/$(BUILD_TARGET_ARCH) .
mkdir -p ./bin/$(BUILD_TARGET_NEWIOCTL) && sudo mv $(GOPATH)/src/main-$(BUILD_TARGET_OS)-$(BUILD_TARGET_ARCH) ./bin/$(BUILD_TARGET_NEWIOCTL)/ioctl-$(BUILD_TARGET_OS)-$(BUILD_TARGET_ARCH)

.PHONY: xctl
xctl:
$(GOBUILD) -ldflags "$(PackageFlags)" -o ./bin/$(BUILD_TARGET_XCTL) -v ./tools/xctl

.PHONY: newxctl
newxctl:
$(GOBUILD) -ldflags "$(PackageFlags)" -o ./bin/$(BUILD_TARGET_NEWXCTL) -v ./tools/newxctl

.PHONY: iomigrater
iomigrater:
$(GOBUILD) -ldflags "$(PackageFlags)" -o ./bin/$(BUILD_TARGET_IOMIGRATER) -v ./tools/iomigrater
2 changes: 2 additions & 0 deletions ioctl/newcmd/account/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func NewAccountCmd(client ioctl.Client) *cobra.Command {
Short: accountShorts,
}
ac.AddCommand(NewAccountCreate(client))
ac.AddCommand(NewAccountCreateAdd(client))
ac.AddCommand(NewAccountDelete(client))
ac.AddCommand(NewAccountNonce(client))
ac.AddCommand(NewAccountList(client))
Expand All @@ -73,6 +74,7 @@ func NewAccountCmd(client ioctl.Client) *cobra.Command {
ac.AddCommand(NewAccountExportPublic(client))
ac.AddCommand(NewAccountExport(client))
ac.AddCommand(NewAccountImportCmd(client))
ac.AddCommand(NewAccountBalance(client))

client.SetEndpointWithFlag(ac.PersistentFlags().StringVar)
client.SetInsecureWithFlag(ac.PersistentFlags().BoolVar)
Expand Down
149 changes: 149 additions & 0 deletions ioctl/newcmd/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
// Copyright (c) 2022 IoTeX Foundation
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these 4 new files file another PR like "add config command"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, just add function, but no specific implementation. this will be done after merged

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

delete

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, delete command function and create new pr for it

// This is an alpha (internal) release and is not suitable for production. This source code is provided 'as is' and no
// warranties are given as to title or non-infringement, merchantability or fitness for purpose and, to the extent
// permitted by law, all liability for your use of the code is disclaimed. This source code is governed by Apache
// License 2.0 that can be found in the LICENSE file.

package config

import (
"fmt"
"os"
"path"
"path/filepath"
"regexp"
"strconv"
"strings"

"github.com/pkg/errors"
"gopkg.in/yaml.v2"

serverCfg "github.com/iotexproject/iotex-core/config"
"github.com/iotexproject/iotex-core/ioctl/config"
"github.com/iotexproject/iotex-core/pkg/log"
)

// Regexp patterns
const (
_ipPattern = `((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)`
_domainPattern = `[a-zA-Z0-9][a-zA-Z0-9_-]{0,62}(\.[a-zA-Z0-9][a-zA-Z0-9_-]{0,62})*(\.[a-zA-Z][a-zA-Z0-9]{0,10}){1}`
_urlPattern = `[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)`
_localPattern = "localhost"
_endpointPattern = "(" + _ipPattern + "|(" + _domainPattern + ")" + "|(" + _localPattern + "))" + `(:\d{1,5})?`
_defaultAnalyserEndpoint = "https://iotex-analyser-api-mainnet.chainanalytics.org"
_defaultConfigFileName = "config.default"
)

var (
_supportedLanguage = []string{"English", "中文"}
_validArgs = []string{"endpoint", "wallet", "explorer", "defaultacc", "language", "nsv2height"}
_validGetArgs = []string{"endpoint", "wallet", "explorer", "defaultacc", "language", "nsv2height", "analyserEndpoint", "all"}
_validExpl = []string{"iotexscan", "iotxplorer"}
_endpointCompile = regexp.MustCompile("^" + _endpointPattern + "$")
)

// info contains the information of config file
type info struct {
readConfig config.Config
defaultConfigFile string // Path to config file
}

// InitConfig load config data from default config file
func InitConfig() (config.Config, string) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return error

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to return err. because if err!=nil, panic.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Multiple places return error
  2. the error handling logic is the same, panic

Thus, returning the error and handle it in the caller side is a better solution

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know, Thanks.

info := &info{}
configDir := os.Getenv("HOME") + "/.config/ioctl/default"
// Create path to config directory
err := os.MkdirAll(configDir, 0700)
if err != nil {
log.L().Panic(err.Error())
}
info.defaultConfigFile = filepath.Join(configDir, _defaultConfigFileName)

// Load or reset config file
info.readConfig, err = config.LoadConfig()
if err != nil && os.IsNotExist(err) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

err = info.reset() // Config file doesn't exist
} else if err != nil {
log.L().Panic(err.Error())
}

// Check completeness of config file
completeness := true
if info.readConfig.Wallet == "" {
info.readConfig.Wallet = configDir
completeness = false
}
if info.readConfig.Language == "" {
info.readConfig.Language = _supportedLanguage[0]
completeness = false
}
if info.readConfig.Nsv2height == 0 {
info.readConfig.Nsv2height = serverCfg.Default.Genesis.FairbankBlockHeight
}
if info.readConfig.AnalyserEndpoint == "" {
info.readConfig.AnalyserEndpoint = _defaultAnalyserEndpoint
completeness = false
}
if !completeness {
if err = info.writeConfig(); err != nil {
log.L().Panic(err.Error())
}
}
// Set language for ioctl
if info.isSupportedLanguage(info.readConfig.Language) == -1 {
fmt.Printf("Warn: Language %s is not supported, English instead.\n", info.readConfig.Language)
}
return info.readConfig, info.defaultConfigFile
}

// newInfo create config info
func newInfo(readConfig config.Config, defaultConfigFile string) *info {
return &info{
readConfig: readConfig,
defaultConfigFile: defaultConfigFile,
}
}

// reset resets all values of config
func (c *info) reset() error {
c.readConfig.Wallet = path.Dir(c.defaultConfigFile)
c.readConfig.Endpoint = ""
c.readConfig.SecureConnect = true
c.readConfig.DefaultAccount = *new(config.Context)
c.readConfig.Explorer = "iotexscan"
c.readConfig.Language = "English"
c.readConfig.AnalyserEndpoint = _defaultAnalyserEndpoint

err := c.writeConfig()
if err != nil {
return err
}

fmt.Println("Config set to default values")
return nil
}

// isSupportedLanguage checks if the language is a supported option and returns index when supported
func (c *info) isSupportedLanguage(arg string) config.Language {
if index, err := strconv.Atoi(arg); err == nil && index >= 0 && index < len(_supportedLanguage) {
return config.Language(index)
}
for i, lang := range _supportedLanguage {
if strings.EqualFold(arg, lang) {
return config.Language(i)
}
}
return config.Language(-1)
}

// writeConfig writes to config file
func (c *info) writeConfig() error {
out, err := yaml.Marshal(&c.readConfig)
if err != nil {
return errors.Wrap(err, "failed to marshal config")
}
if err := os.WriteFile(c.defaultConfigFile, out, 0600); err != nil {
return errors.Wrap(err, fmt.Sprintf("failed to write to config file %s", c.defaultConfigFile))
}
return nil
}
7 changes: 7 additions & 0 deletions ioctl/newcmd/config/config_get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Copyright (c) 2022 IoTeX Foundation
// This is an alpha (internal) release and is not suitable for production. This source code is provided 'as is' and no
// warranties are given as to title or non-infringement, merchantability or fitness for purpose and, to the extent
// permitted by law, all liability for your use of the code is disclaimed. This source code is governed by Apache
// License 2.0 that can be found in the LICENSE file.

package config
7 changes: 7 additions & 0 deletions ioctl/newcmd/config/config_reset.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Copyright (c) 2022 IoTeX Foundation
// This is an alpha (internal) release and is not suitable for production. This source code is provided 'as is' and no
// warranties are given as to title or non-infringement, merchantability or fitness for purpose and, to the extent
// permitted by law, all liability for your use of the code is disclaimed. This source code is governed by Apache
// License 2.0 that can be found in the LICENSE file.

package config
7 changes: 7 additions & 0 deletions ioctl/newcmd/config/config_set.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Copyright (c) 2022 IoTeX Foundation
// This is an alpha (internal) release and is not suitable for production. This source code is provided 'as is' and no
// warranties are given as to title or non-infringement, merchantability or fitness for purpose and, to the extent
// permitted by law, all liability for your use of the code is disclaimed. This source code is governed by Apache
// License 2.0 that can be found in the LICENSE file.

package config
79 changes: 79 additions & 0 deletions ioctl/newcmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Copyright (c) 2022 IoTeX Foundation
// This is an alpha (internal) release and is not suitable for production. This source code is provided 'as is' and no
// warranties are given as to title or non-infringement, merchantability or fitness for purpose and, to the extent
// permitted by law, all liability for your use of the code is disclaimed. This source code is governed by Apache
// License 2.0 that can be found in the LICENSE file.

package newcmd

import (
"github.com/spf13/cobra"

"github.com/iotexproject/iotex-core/ioctl"
"github.com/iotexproject/iotex-core/ioctl/config"
"github.com/iotexproject/iotex-core/ioctl/newcmd/account"
)

// Multi-language support
var (
_ioctlRootCmdShorts = map[config.Language]string{
config.English: "Command-line interface for IoTeX blockchain",
config.Chinese: "IoTeX区块链命令行工具",
}
_ioctlRootCmdLongs = map[config.Language]string{
config.English: `ioctl is a command-line interface for interacting with IoTeX blockchain.`,
config.Chinese: `ioctl 是用于与IoTeX区块链进行交互的命令行工具`,
}
_ioctlRootCmdUses = map[config.Language]string{
config.English: "ioctl",
config.Chinese: "ioctl",
}
_xctlRootCmdShorts = map[config.Language]string{
config.English: "Command-line interface for consortium blockchain",
config.Chinese: "联盟链命令行工具",
}
_xctlRootCmdLongs = map[config.Language]string{
config.English: `xctl is a command-line interface for interacting with consortium blockchain.`,
config.Chinese: `xctl 是用于与联盟链进行交互的命令行工具`,
}
_xctlRootCmdUses = map[config.Language]string{
config.English: "xctl",
config.Chinese: "xctl",
}
)

// NewIoctl returns ioctl root cmd
func NewIoctl(client ioctl.Client) *cobra.Command {
rootUses, _ := client.SelectTranslation(_ioctlRootCmdUses)
rootShorts, _ := client.SelectTranslation(_ioctlRootCmdShorts)
rootLongs, _ := client.SelectTranslation(_ioctlRootCmdLongs)

rootCmd := &cobra.Command{
Use: rootUses,
Short: rootShorts,
Long: rootLongs,
}

rootCmd.AddCommand(config.ConfigCmd)
rootCmd.AddCommand(account.NewAccountCmd(client))

return rootCmd
}

// NewXctl returns xctl root cmd
func NewXctl(client ioctl.Client) *cobra.Command {
rootUses, _ := client.SelectTranslation(_xctlRootCmdUses)
rootShorts, _ := client.SelectTranslation(_xctlRootCmdShorts)
rootLongs, _ := client.SelectTranslation(_xctlRootCmdLongs)

var rootCmd = &cobra.Command{
Use: rootUses,
Short: rootShorts,
Long: rootLongs,
}

rootCmd.AddCommand(config.ConfigCmd)
rootCmd.AddCommand(account.NewAccountCmd(client))

return rootCmd
}
23 changes: 23 additions & 0 deletions tools/newioctl/ioctl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (c) 2022 IoTeX Foundation
// This is an alpha (internal) release and is not suitable for production. This source code is provided 'as is' and no
// warranties are given as to title or non-infringement, merchantability or fitness for purpose and, to the extent
// permitted by law, all liability for your use of the code is disclaimed. This source code is governed by Apache
// License 2.0 that can be found in the LICENSE file.

package main

import (
"os"

"github.com/iotexproject/iotex-core/ioctl"
"github.com/iotexproject/iotex-core/ioctl/newcmd"
"github.com/iotexproject/iotex-core/ioctl/newcmd/config"
)

func main() {
readConfig, defaultConfigFile := config.InitConfig()
client := ioctl.NewClient(readConfig, defaultConfigFile)
if err := newcmd.NewIoctl(client).Execute(); err != nil {
os.Exit(1)
}
}
23 changes: 23 additions & 0 deletions tools/newxctl/xctl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (c) 2022 IoTeX Foundation
// This is an alpha (internal) release and is not suitable for production. This source code is provided 'as is' and no
// warranties are given as to title or non-infringement, merchantability or fitness for purpose and, to the extent
// permitted by law, all liability for your use of the code is disclaimed. This source code is governed by Apache
// License 2.0 that can be found in the LICENSE file.

package main

import (
"os"

"github.com/iotexproject/iotex-core/ioctl"
"github.com/iotexproject/iotex-core/ioctl/newcmd"
"github.com/iotexproject/iotex-core/ioctl/newcmd/config"
)

func main() {
readConfig, defaultConfigFile := config.InitConfig()
client := ioctl.NewClient(readConfig, defaultConfigFile, ioctl.EnableCryptoSm2())
if err := newcmd.NewXctl(client).Execute(); err != nil {
os.Exit(1)
}
}