Skip to content

Commit

Permalink
feat(ioctl): ipfs endpoint and ipfs gateway config (#4038)
Browse files Browse the repository at this point in the history
  • Loading branch information
saitofun committed Jan 3, 2024
1 parent efcc7eb commit ef7053a
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 28 deletions.
19 changes: 19 additions & 0 deletions ioctl/cmd/ws/ws.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ 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网关",
}

_flagContractAddressUsages = map[config.Language]string{
config.English: "set w3bsteram project register contract address for once",
config.Chinese: "一次设置w3bstream项目注册合约地址",
Expand All @@ -47,6 +58,14 @@ 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.IPFSGateway, "ipfs-gateway",
config.ReadConfig.IPFSGateway, config.TranslateInLang(_flagIPFSGatewayUsages, config.UILanguage),
)
WsCmd.PersistentFlags().StringVar(
&config.ReadConfig.WsRegisterContract, "contract-address",
config.ReadConfig.WsRegisterContract, config.TranslateInLang(_flagContractAddressUsages, config.UILanguage),
Expand Down
16 changes: 14 additions & 2 deletions ioctl/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,12 @@ 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"`
// WsRegisterContract w3bstream project register contract address
WsRegisterContract string `json:"wsProjectRegisterContract" yaml:"wsProjectRegisterContract"`
WsRegisterContract string `json:"wsRegisterContract" yaml:"wsRegisterContract"`
}

var (
Expand Down Expand Up @@ -122,8 +126,16 @@ func init() {
ReadConfig.WsEndpoint = _defaultWsEndpoint
completeness = false
}
if ReadConfig.IPFSEndpoint == "" {
ReadConfig.IPFSEndpoint = _defaultIPFSEndpoint
completeness = false
}
if ReadConfig.IPFSGateway == "" {
ReadConfig.IPFSGateway = _defaultIPFSGateway
completeness = false
}
if ReadConfig.WsRegisterContract == "" {
ReadConfig.WsRegisterContract = _defaultProjectRegisterContract
ReadConfig.WsRegisterContract = _defaultWsRegisterContract
completeness = false
}
if !completeness {
Expand Down
31 changes: 23 additions & 8 deletions ioctl/config/configsetget.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,20 @@ const (
_localPattern = "localhost"
_endpointPattern = "(" + _ipPattern + "|(" + _domainPattern + ")" + "|(" + _localPattern + "))" + `(:\d{1,5})?`
_defaultAnalyserEndpoint = "https://iotex-analyser-api-mainnet.chainanalytics.org"
_defaultWsEndpoint = "sprout-staging.w3bstream.com:9000"
// _defaultProjectRegisterContract default project register contract address
_defaultProjectRegisterContract = "0x4F7e678B0203e0444E17512108dba4B08B39512e"
// _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"
// _defaultWsRegisterContract default project register contract address
_defaultWsRegisterContract = "0x4F7e678B0203e0444E17512108dba4B08B39512e"
)

var (
_supportedLanguage = []string{"English", "中文"}
_validArgs = []string{"endpoint", "wallet", "explorer", "defaultacc", "language", "nsv2height", "wsEndpoint", "projectRegisterContract"}
_validGetArgs = []string{"endpoint", "wallet", "explorer", "defaultacc", "language", "nsv2height", "analyserEndpoint", "wsEndpoint", "projectRegisterContract", "all"}
_validArgs = []string{"endpoint", "wallet", "explorer", "defaultacc", "language", "nsv2height", "wsEndpoint", "ipfsEndpoint", "ipfsGateway", "wsRegisterContract"}
_validGetArgs = []string{"endpoint", "wallet", "explorer", "defaultacc", "language", "nsv2height", "analyserEndpoint", "wsEndpoint", "ipfsEndpoint", "ipfsGateway", "wsRegisterContract", "all"}
_validExpl = []string{"iotexscan", "iotxplorer"}
_endpointCompile = regexp.MustCompile("^" + _endpointPattern + "$")
)
Expand Down Expand Up @@ -153,7 +158,11 @@ func Get(arg string) error {
fmt.Println(ReadConfig.AnalyserEndpoint)
case "wsEndpoint":
fmt.Println(ReadConfig.WsEndpoint)
case "projectRegisterContract":
case "ipfsEndpoint":
fmt.Println(ReadConfig.IPFSEndpoint)
case "ipfsGateway":
fmt.Println(ReadConfig.IPFSGateway)
case "wsRegisterContract":
fmt.Println(ReadConfig.WsRegisterContract)
case "all":
fmt.Println(ReadConfig.String())
Expand Down Expand Up @@ -285,7 +294,11 @@ func set(args []string) error {
ReadConfig.Nsv2height = height
case "wsEndpoint":
ReadConfig.WsEndpoint = args[1]
case "projectRegisterContract":
case "ipfsEndpoint":
ReadConfig.IPFSEndpoint = args[1]
case "ipfsGateway":
ReadConfig.IPFSGateway = args[1]
case "wsRegisterContract":
ReadConfig.WsRegisterContract = args[1]
}
err := writeConfig()
Expand All @@ -306,7 +319,9 @@ func reset() error {
ReadConfig.Language = "English"
ReadConfig.AnalyserEndpoint = _defaultAnalyserEndpoint
ReadConfig.WsEndpoint = _defaultWsEndpoint
ReadConfig.WsRegisterContract = _defaultProjectRegisterContract
ReadConfig.IPFSEndpoint = _defaultIPFSEndpoint
ReadConfig.IPFSGateway = _defaultIPFSGateway
ReadConfig.WsRegisterContract = _defaultWsRegisterContract

err := writeConfig()
if err != nil {
Expand Down
39 changes: 30 additions & 9 deletions ioctl/newcmd/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,21 @@ 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"
// _defaultWsProjectRegisterContract default w3bstream project register contract address
_defaultWsProjectRegisterContract = "0x4F7e678B0203e0444E17512108dba4B08B39512e"
// _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"
// _defaultWsRegisterContract default w3bstream project register contract address
_defaultWsRegisterContract = "0x4F7e678B0203e0444E17512108dba4B08B39512e"
)

var (
_supportedLanguage = []string{"English", "中文"}
_validArgs = []string{"endpoint", "wallet", "explorer", "defaultacc", "language", "nsv2height", "wsEndpoint", "wsProjectRegisterContract"}
_validGetArgs = []string{"endpoint", "wallet", "explorer", "defaultacc", "language", "nsv2height", "analyserEndpoint", "wsEndpoint", "wsProjectRegisterContract", "all"}
_validArgs = []string{"endpoint", "wallet", "explorer", "defaultacc", "language", "nsv2height", "wsEndpoint", "ipfsEndpoint", "ipfsGateway", "wsRegisterContract"}
_validGetArgs = []string{"endpoint", "wallet", "explorer", "defaultacc", "language", "nsv2height", "wsEndpoint", "ipfsEndpoint", "ipfsGateway", "analyserEndpoint", "wsRegisterContract", "all"}
_validExpl = []string{"iotexscan", "iotxplorer"}
_endpointCompile = regexp.MustCompile("^" + _endpointPattern + "$")
_configDir = os.Getenv("HOME") + "/.config/ioctl/default"
Expand Down Expand Up @@ -122,8 +127,14 @@ 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 info.readConfig.WsRegisterContract == "" {
info.readConfig.WsRegisterContract = _defaultWsProjectRegisterContract
info.readConfig.WsRegisterContract = _defaultWsRegisterContract
}
if !completeness {
if err = info.writeConfig(); err != nil {
Expand Down Expand Up @@ -155,7 +166,9 @@ func (c *info) reset() error {
c.readConfig.Language = _supportedLanguage[0]
c.readConfig.AnalyserEndpoint = _defaultAnalyserEndpoint
c.readConfig.WsEndpoint = _defaultWsEndpoint
c.readConfig.WsRegisterContract = _defaultWsProjectRegisterContract
c.readConfig.IPFSEndpoint = _defaultIPFSEndpoint
c.readConfig.IPFSGateway = _defaultIPFSGateway
c.readConfig.WsRegisterContract = _defaultWsRegisterContract

err := c.writeConfig()
if err != nil {
Expand Down Expand Up @@ -216,7 +229,11 @@ 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 "wsProjectRegisterContract":
case "ipfsEndpoint":
c.readConfig.IPFSEndpoint = args[1]
case "ipfsGateway":
c.readConfig.IPFSGateway = args[1]
case "wsRegisterContract":
c.readConfig.WsRegisterContract = args[1]
default:
return "", config.ErrConfigNotMatch
Expand Down Expand Up @@ -255,7 +272,11 @@ func (c *info) get(arg string) (string, error) {
return c.readConfig.AnalyserEndpoint, nil
case "wsEndpoint":
return c.readConfig.WsEndpoint, nil
case "wsProjectRegisterContract":
case "ipfsEndpoint":
return c.readConfig.IPFSEndpoint, nil
case "ipfsGateway":
return c.readConfig.IPFSGateway, nil
case "wsRegisterContract":
return c.readConfig.WsRegisterContract, nil
case "all":
return jsonString(c.readConfig)
Expand Down
48 changes: 39 additions & 9 deletions 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,7 +58,9 @@ func TestConfigGet(t *testing.T) {
Language: "English",
AnalyserEndpoint: "testAnalyser",
WsEndpoint: "testWsEndpoint",
WsRegisterContract: "testWsProjectRegisterContract",
IPFSEndpoint: "testIPFSEndpoint",
IPFSGateway: "testIPFSGateway",
WsRegisterContract: "testWsRegisterContract",
}, testPath)

tcs := []struct {
Expand Down Expand Up @@ -95,12 +100,20 @@ func TestConfigGet(t *testing.T) {
"testWsEndpoint",
},
{
"wsProjectRegisterContract",
"testWsProjectRegisterContract",
"ipfsEndpoint",
"testIPFSEndpoint",
},
{
"ipfsGateway",
"testIPFSGateway",
},
{
"wsRegisterContract",
"testWsRegisterContract",
},
{
"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 \"wsProjectRegisterContract\": \"testWsProjectRegisterContract\"\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 \"wsRegisterContract\": \"testWsRegisterContract\"\n}",
},
}

Expand Down Expand Up @@ -128,7 +141,9 @@ func TestConfigReset(t *testing.T) {
Language: "Croatian",
AnalyserEndpoint: "testAnalyser",
WsEndpoint: "testWsEndpoint",
WsRegisterContract: "testProjectRegisterContract",
IPFSEndpoint: "testIPFSEndpoint",
IPFSGateway: "testIPFSGateway",
WsRegisterContract: "testWsRegisterContract",
}, cfgFile)

// write the config to the temp dir and then reset
Expand All @@ -144,7 +159,9 @@ func TestConfigReset(t *testing.T) {
require.Equal("explorer", cfg.Explorer)
require.Equal(config.Context{AddressOrAlias: ""}, cfg.DefaultAccount)
require.Equal("testWsEndpoint", cfg.WsEndpoint)
require.Equal("testProjectRegisterContract", cfg.WsRegisterContract)
require.Equal("testIPFSEndpoint", cfg.IPFSEndpoint)
require.Equal("testIPFSGateway", cfg.IPFSGateway)
require.Equal("testWsRegisterContract", cfg.WsRegisterContract)

require.NoError(info.reset())
require.NoError(info.loadConfig())
Expand All @@ -157,7 +174,9 @@ func TestConfigReset(t *testing.T) {
require.Equal("English", resetCfg.Language)
require.Equal(_defaultAnalyserEndpoint, resetCfg.AnalyserEndpoint)
require.Equal(_defaultWsEndpoint, resetCfg.WsEndpoint)
require.Equal(_defaultWsProjectRegisterContract, resetCfg.WsRegisterContract)
require.Equal(_defaultIPFSEndpoint, resetCfg.IPFSEndpoint)
require.Equal(_defaultIPFSGateway, resetCfg.IPFSGateway)
require.Equal(_defaultWsRegisterContract, resetCfg.WsRegisterContract)
require.Equal("iotexscan", resetCfg.Explorer)
require.Equal(*new(config.Context), resetCfg.DefaultAccount)
}
Expand All @@ -175,6 +194,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 @@ -230,8 +252,16 @@ func TestConfigSet(t *testing.T) {
"testWsEndpoint",
},
{
[]string{"wsProjectRegisterContract", "testWsProjectRegisterContract"},
"testWsProjectRegisterContract",
[]string{"ipfsEndpoint", "testIPFSEndpoint"},
"testIPFSEndpoint",
},
{
[]string{"ipfsGateway", "testIPFSGateway"},
"testIPFSGateway",
},
{
[]string{"wsRegisterContract", "testWsRegisterContract"},
"testWsRegisterContract",
},
}

Expand Down

0 comments on commit ef7053a

Please sign in to comment.