Skip to content

Commit

Permalink
move panic from InitConfig() inside to caller aside
Browse files Browse the repository at this point in the history
  • Loading branch information
huof6829 committed Jun 22, 2022
1 parent 4d7d30e commit 9a75119
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
13 changes: 6 additions & 7 deletions ioctl/newcmd/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (

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

// Regexp patterns
Expand Down Expand Up @@ -49,22 +48,22 @@ type info struct {
}

// InitConfig load config data from default config file
func InitConfig() (config.Config, string) {
func InitConfig() (config.Config, string, error) {
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())
return info.readConfig, info.defaultConfigFile, err
}
info.defaultConfigFile = filepath.Join(configDir, _defaultConfigFileName)

// Load or reset config file
info.readConfig, err = config.LoadConfig()
if err != nil && os.IsNotExist(err) {
if os.IsNotExist(err) {
err = info.reset() // Config file doesn't exist
} else if err != nil {
log.L().Panic(err.Error())
return info.readConfig, info.defaultConfigFile, err
}

// Check completeness of config file
Expand All @@ -86,14 +85,14 @@ func InitConfig() (config.Config, string) {
}
if !completeness {
if err = info.writeConfig(); err != nil {
log.L().Panic(err.Error())
return info.readConfig, info.defaultConfigFile, err
}
}
// 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
return info.readConfig, info.defaultConfigFile, nil
}

// newInfo create config info
Expand Down
6 changes: 5 additions & 1 deletion tools/newioctl/ioctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@ import (
"github.com/iotexproject/iotex-core/ioctl"
"github.com/iotexproject/iotex-core/ioctl/newcmd"
"github.com/iotexproject/iotex-core/ioctl/newcmd/config"
"github.com/iotexproject/iotex-core/pkg/log"
)

func main() {
readConfig, defaultConfigFile := config.InitConfig()
readConfig, defaultConfigFile, err := config.InitConfig()
if err != nil {
log.L().Panic(err.Error())
}
client := ioctl.NewClient(readConfig, defaultConfigFile)
if err := newcmd.NewIoctl(client).Execute(); err != nil {
os.Exit(1)
Expand Down
6 changes: 5 additions & 1 deletion tools/newxctl/xctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@ import (
"github.com/iotexproject/iotex-core/ioctl"
"github.com/iotexproject/iotex-core/ioctl/newcmd"
"github.com/iotexproject/iotex-core/ioctl/newcmd/config"
"github.com/iotexproject/iotex-core/pkg/log"
)

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

0 comments on commit 9a75119

Please sign in to comment.