Skip to content

Commit

Permalink
feat(ioctl): ipfs endpoint and ipfs gateway config
Browse files Browse the repository at this point in the history
  • Loading branch information
saitofun committed Dec 26, 2023
1 parent 2dbb478 commit 6383d7f
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 7 deletions.
18 changes: 18 additions & 0 deletions ioctl/cmd/ws/ws.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ var (
config.English: "set w3bsteram endpoint for once",
config.Chinese: "一次设置w3bstream端点",
}

_flagIPFSEndpointUsages = map[config.Language]string{
config.English: "set ipfs endpoint for resource uploading for once",
config.Chinese: "一次设置ipfs端点",
}

_flagIPFSGatewayUsages = map[config.Language]string{
config.English: "set ipfs gateway for resource fetching for once",
config.Chinese: "一次设置ipfs网关",
}
)

func init() {
Expand All @@ -43,4 +53,12 @@ func init() {
&config.ReadConfig.WsEndpoint, "ws-endpoint",
config.ReadConfig.WsEndpoint, config.TranslateInLang(_flagWsEndpointUsages, config.UILanguage),
)
WsCmd.PersistentFlags().StringVar(
&config.ReadConfig.IPFSEndpoint, "ipfs-endpoint",
config.ReadConfig.IPFSEndpoint, config.TranslateInLang(_flagIPFSEndpointUsages, config.UILanguage),
)
WsCmd.PersistentFlags().StringVar(
&config.ReadConfig.IPFSEndpoint, "ipfs-gateway",
config.ReadConfig.IPFSEndpoint, config.TranslateInLang(_flagIPFSGatewayUsages, config.UILanguage),
)
}
12 changes: 12 additions & 0 deletions ioctl/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ type Config struct {
AnalyserEndpoint string `json:"analyserEndpoint" yaml:"analyserEndpoint"`
// WsEndpoint w3bstream endpoint
WsEndpoint string `json:"wsEndpoint" yaml:"wsEndpoint"`
// IPFSEndpoint ipfs endpoint for uploading
IPFSEndpoint string `json:"ipfsEndpoint" yaml:"ipfsEndpoint"`
// IPFSGateway ipfs gateway for resource fetching (with scheme)
IPFSGateway string `json:"ipfsGateway" yaml:"ipfsGateway"`
}

var (
Expand Down Expand Up @@ -120,6 +124,14 @@ func init() {
ReadConfig.WsEndpoint = _defaultWsEndpoint
completeness = false
}
if ReadConfig.IPFSEndpoint == "" {
ReadConfig.IPFSEndpoint = _defaultIPFSEndpoint
completeness = false
}
if ReadConfig.IPFSGateway == "" {
ReadConfig.IPFSGateway = _defaultIPFSGateway
completeness = false
}
if !completeness {
err := writeConfig()
if err != nil {
Expand Down
21 changes: 18 additions & 3 deletions ioctl/config/configsetget.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,18 @@ const (
_localPattern = "localhost"
_endpointPattern = "(" + _ipPattern + "|(" + _domainPattern + ")" + "|(" + _localPattern + "))" + `(:\d{1,5})?`
_defaultAnalyserEndpoint = "https://iotex-analyser-api-mainnet.chainanalytics.org"
_defaultWsEndpoint = "sprout-staging.w3bstream.com:9000"
// _defaultWsEndpoint default w3bstream endpoint
_defaultWsEndpoint = "sprout-staging.w3bstream.com:9000"
// _defaultIPFSEndpoint default IPFS endpoint for uploading
_defaultIPFSEndpoint = "ipfs.mainnet.iotex.io"
// _defaultIPFSGateway default IPFS gateway for resource fetching
_defaultIPFSGateway = "https://ipfs.io"
)

var (
_supportedLanguage = []string{"English", "中文"}
_validArgs = []string{"endpoint", "wallet", "explorer", "defaultacc", "language", "nsv2height", "wsEndpoint"}
_validGetArgs = []string{"endpoint", "wallet", "explorer", "defaultacc", "language", "nsv2height", "analyserEndpoint", "wsEndpoint", "all"}
_validArgs = []string{"endpoint", "wallet", "explorer", "defaultacc", "language", "nsv2height", "wsEndpoint", "ipfsEndpoint", "ipfsGateway"}
_validGetArgs = []string{"endpoint", "wallet", "explorer", "defaultacc", "language", "nsv2height", "analyserEndpoint", "wsEndpoint", "ipfsEndpoint", "ipfsGateway", "all"}
_validExpl = []string{"iotexscan", "iotxplorer"}
_endpointCompile = regexp.MustCompile("^" + _endpointPattern + "$")
)
Expand Down Expand Up @@ -151,6 +156,10 @@ func Get(arg string) error {
fmt.Println(ReadConfig.AnalyserEndpoint)
case "wsEndpoint":
fmt.Println(ReadConfig.WsEndpoint)
case "ipfsEndpoint":
fmt.Println(ReadConfig.IPFSEndpoint)
case "ipfsGateway":
fmt.Println(ReadConfig.IPFSGateway)
case "all":
fmt.Println(ReadConfig.String())
}
Expand Down Expand Up @@ -281,6 +290,10 @@ func set(args []string) error {
ReadConfig.Nsv2height = height
case "wsEndpoint":
ReadConfig.WsEndpoint = args[1]
case "ipfsEndpoint":
ReadConfig.IPFSEndpoint = args[1]
case "ipfsGateway":
ReadConfig.IPFSGateway = args[1]
}
err := writeConfig()
if err != nil {
Expand All @@ -300,6 +313,8 @@ func reset() error {
ReadConfig.Language = "English"
ReadConfig.AnalyserEndpoint = _defaultAnalyserEndpoint
ReadConfig.WsEndpoint = _defaultWsEndpoint
ReadConfig.IPFSEndpoint = _defaultIPFSEndpoint
ReadConfig.IPFSGateway = _defaultIPFSGateway

err := writeConfig()
if err != nil {
Expand Down
27 changes: 24 additions & 3 deletions ioctl/newcmd/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,19 @@ const (
_localPattern = "localhost"
_endpointPattern = "(" + _ipPattern + "|(" + _domainPattern + ")" + "|(" + _localPattern + "))" + `(:\d{1,5})?`
_defaultAnalyserEndpoint = "https://iotex-analyser-api-mainnet.chainanalytics.org"
_defaultWsEndpoint = "sprout-staging.w3bstream.com:9000"
_defaultConfigFileName = "config.default"
// _defaultWsEndpoint default w3bstream endpoint
_defaultWsEndpoint = "sprout-staging.w3bstream.com:9000"
// _defaultIPFSEndpoint default IPFS endpoint for uploading
_defaultIPFSEndpoint = "ipfs.mainnet.iotex.io"
// _defaultIPFSGateway default IPFS gateway for resource fetching
_defaultIPFSGateway = "https://ipfs.io"
)

var (
_supportedLanguage = []string{"English", "中文"}
_validArgs = []string{"endpoint", "wallet", "explorer", "defaultacc", "language", "nsv2height"}
_validGetArgs = []string{"endpoint", "wallet", "explorer", "defaultacc", "language", "nsv2height", "analyserEndpoint", "all"}
_validArgs = []string{"endpoint", "wallet", "explorer", "defaultacc", "language", "nsv2height", "wsEndpoint", "ipfsEndpoint", "ipfsGateway"}
_validGetArgs = []string{"endpoint", "wallet", "explorer", "defaultacc", "language", "nsv2height", "wsEndpoint", "ipfsEndpoint", "ipfsGateway", "analyserEndpoint", "all"}
_validExpl = []string{"iotexscan", "iotxplorer"}
_endpointCompile = regexp.MustCompile("^" + _endpointPattern + "$")
_configDir = os.Getenv("HOME") + "/.config/ioctl/default"
Expand Down Expand Up @@ -120,6 +125,12 @@ func InitConfig() (config.Config, string, error) {
info.readConfig.WsEndpoint = _defaultWsEndpoint
completeness = false
}
if info.readConfig.IPFSEndpoint == "" {
info.readConfig.IPFSEndpoint = _defaultIPFSEndpoint
}
if info.readConfig.IPFSGateway == "" {
info.readConfig.IPFSGateway = _defaultIPFSGateway
}
if !completeness {
if err = info.writeConfig(); err != nil {
return info.readConfig, info.defaultConfigFile, err
Expand Down Expand Up @@ -150,6 +161,8 @@ func (c *info) reset() error {
c.readConfig.Language = _supportedLanguage[0]
c.readConfig.AnalyserEndpoint = _defaultAnalyserEndpoint
c.readConfig.WsEndpoint = _defaultWsEndpoint
c.readConfig.IPFSEndpoint = _defaultIPFSEndpoint
c.readConfig.IPFSGateway = _defaultIPFSGateway

err := c.writeConfig()
if err != nil {
Expand Down Expand Up @@ -210,6 +223,10 @@ func (c *info) set(args []string, insecure bool, client ioctl.Client) (string, e
c.readConfig.Nsv2height = height
case "wsEndpoint":
c.readConfig.WsEndpoint = args[1]
case "ipfsEndpoint":
c.readConfig.IPFSEndpoint = args[1]
case "ipfsGateway":
c.readConfig.IPFSGateway = args[1]
default:
return "", config.ErrConfigNotMatch
}
Expand Down Expand Up @@ -247,6 +264,10 @@ func (c *info) get(arg string) (string, error) {
return c.readConfig.AnalyserEndpoint, nil
case "wsEndpoint":
return c.readConfig.WsEndpoint, nil
case "ipfsEndpoint":
return c.readConfig.IPFSEndpoint, nil
case "ipfsGateway":
return c.readConfig.IPFSGateway, nil
case "all":
return jsonString(c.readConfig)
default:
Expand Down
32 changes: 31 additions & 1 deletion ioctl/newcmd/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ func TestInitConfig(t *testing.T) {
require.Equal(_validExpl[0], cfg.Explorer)
require.Equal(_supportedLanguage[0], cfg.Language)
require.Equal(filepath.Join(testPath, _defaultConfigFileName), cfgFilePath)
require.Equal(_defaultWsEndpoint, cfg.WsEndpoint)
require.Equal(_defaultIPFSEndpoint, cfg.IPFSEndpoint)
require.Equal(_defaultIPFSGateway, cfg.IPFSGateway)
}

func TestConfigGet(t *testing.T) {
Expand All @@ -55,6 +58,8 @@ func TestConfigGet(t *testing.T) {
Language: "English",
AnalyserEndpoint: "testAnalyser",
WsEndpoint: "testWsEndpoint",
IPFSEndpoint: "testIPFSEndpoint",
IPFSGateway: "testIPFSGateway",
}, testPath)

tcs := []struct {
Expand Down Expand Up @@ -93,9 +98,17 @@ func TestConfigGet(t *testing.T) {
"wsEndpoint",
"testWsEndpoint",
},
{
"ipfsEndpoint",
"testIPFSEndpoint",
},
{
"ipfsGateway",
"testIPFSGateway",
},
{
"all",
"\"endpoint\": \"\",\n \"secureConnect\": true,\n \"aliases\": {},\n \"defaultAccount\": {\n \"addressOrAlias\": \"test\"\n },\n \"explorer\": \"iotexscan\",\n \"language\": \"English\",\n \"nsv2height\": 0,\n \"analyserEndpoint\": \"testAnalyser\",\n \"wsEndpoint\": \"testWsEndpoint\"\n}",
"\"endpoint\": \"\",\n \"secureConnect\": true,\n \"aliases\": {},\n \"defaultAccount\": {\n \"addressOrAlias\": \"test\"\n },\n \"explorer\": \"iotexscan\",\n \"language\": \"English\",\n \"nsv2height\": 0,\n \"analyserEndpoint\": \"testAnalyser\",\n \"wsEndpoint\": \"testWsEndpoint\",\n \"ipfsEndpoint\": \"testIPFSEndpoint\",\n \"ipfsGateway\": \"testIPFSGateway\"\n}",
},
}

Expand Down Expand Up @@ -123,6 +136,8 @@ func TestConfigReset(t *testing.T) {
Language: "Croatian",
AnalyserEndpoint: "testAnalyser",
WsEndpoint: "testWsEndpoint",
IPFSEndpoint: "testIPFSEndpoint",
IPFSGateway: "testIPFSGateway",
}, cfgFile)

// write the config to the temp dir and then reset
Expand All @@ -138,6 +153,8 @@ func TestConfigReset(t *testing.T) {
require.Equal("explorer", cfg.Explorer)
require.Equal(config.Context{AddressOrAlias: ""}, cfg.DefaultAccount)
require.Equal("testWsEndpoint", cfg.WsEndpoint)
require.Equal("testIPFSEndpoint", cfg.IPFSEndpoint)
require.Equal("testIPFSGateway", cfg.IPFSGateway)

require.NoError(info.reset())
require.NoError(info.loadConfig())
Expand All @@ -150,6 +167,8 @@ func TestConfigReset(t *testing.T) {
require.Equal("English", resetCfg.Language)
require.Equal(_defaultAnalyserEndpoint, resetCfg.AnalyserEndpoint)
require.Equal(_defaultWsEndpoint, resetCfg.WsEndpoint)
require.Equal(_defaultIPFSEndpoint, resetCfg.IPFSEndpoint)
require.Equal(_defaultIPFSGateway, resetCfg.IPFSGateway)
require.Equal("iotexscan", resetCfg.Explorer)
require.Equal(*new(config.Context), resetCfg.DefaultAccount)
}
Expand All @@ -167,6 +186,9 @@ func TestConfigSet(t *testing.T) {
Explorer: "iotexscan",
Language: "English",
AnalyserEndpoint: "testAnalyser",
WsEndpoint: "testWsEndpoint",
IPFSEndpoint: "testIPFSEndpoint",
IPFSGateway: "testIPFSGateway",
}, cfgFile)

tcs := []struct {
Expand Down Expand Up @@ -221,6 +243,14 @@ func TestConfigSet(t *testing.T) {
[]string{"wsEndpoint", "testWsEndpoint"},
"testWsEndpoint",
},
{
[]string{"ipfsEndpoint", "testIPFSEndpoint"},
"testIPFSEndpoint",
},
{
[]string{"ipfsGateway", "testIPFSGateway"},
"testIPFSGateway",
},
}

for _, tc := range tcs {
Expand Down

0 comments on commit 6383d7f

Please sign in to comment.