From 483cc61459205e379756b6c6daced2b6caed1d8e Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Thu, 6 Feb 2025 13:22:51 +0100 Subject: [PATCH] feedback --- connect/chains/config.go | 2 +- connect/chains/registry.go | 26 ++++++------ connect/chains/registry_test.go | 71 +++++++++++++++++++++++++++++++++ connect/cmd/add.go | 2 +- 4 files changed, 86 insertions(+), 15 deletions(-) create mode 100644 connect/chains/registry_test.go diff --git a/connect/chains/config.go b/connect/chains/config.go index b196bda..03de3f1 100644 --- a/connect/chains/config.go +++ b/connect/chains/config.go @@ -78,7 +78,7 @@ func ConfigDir() (string, error) { return "", fmt.Errorf("failed to get ignite config directory: %w", err) } - dir := path.Join(igniteConfigDir, "connect") + dir := path.Join(igniteConfigDir, "apps", "connect") if err := os.MkdirAll(dir, 0o755); err != nil { return "", fmt.Errorf("failed to create config directory: %w", err) } diff --git a/connect/chains/registry.go b/connect/chains/registry.go index 5151e3a..5bc80e5 100644 --- a/connect/chains/registry.go +++ b/connect/chains/registry.go @@ -97,23 +97,24 @@ func EnrichChain(chain *chainregistry.Chain) error { return fmt.Errorf("failed to read response: %w", err) } - var cdOutput map[string]json.RawMessage - if err := json.Unmarshal(body, &cdOutput); err != nil { - return fmt.Errorf("failed to unmarshal cosmos.directory API response: %w", err) + apiResponseType := struct { + Chain *chainregistry.Chain `json:"chain"` + }{ + Chain: chain, } - rawChain, ok := cdOutput["chain"] - if !ok { - return fmt.Errorf("failed to get chains from response: cosmos.directory API may have changed") + if err := json.Unmarshal(body, &apiResponseType); err != nil { + return fmt.Errorf("failed to unmarshal cosmos.directory API response: %w", err) } - if err := json.Unmarshal(rawChain, chain); err != nil { - return fmt.Errorf("failed to unmarshal %s chain: %w", chain.ChainName, err) - } + chain.APIs.Grpc = cleanGRPCEntries(chain.APIs.Grpc) - // clean-up data (mainly grpc endpoints) + return nil +} + +func cleanGRPCEntries(entries []chainregistry.APIProvider) []chainregistry.APIProvider { cleanEntries := make([]chainregistry.APIProvider, 0) - for _, api := range chain.APIs.Grpc { + for _, api := range entries { // clean-up the http(s):// prefix if idx := strings.Index(api.Address, "://"); idx != -1 { api.Address = api.Address[idx+3:] @@ -128,7 +129,6 @@ func EnrichChain(chain *chainregistry.Chain) error { cleanEntries = append(cleanEntries, api) } - chain.APIs.Grpc = cleanEntries - return nil + return cleanEntries } diff --git a/connect/chains/registry_test.go b/connect/chains/registry_test.go new file mode 100644 index 0000000..eb77b83 --- /dev/null +++ b/connect/chains/registry_test.go @@ -0,0 +1,71 @@ +package chains + +import ( + "reflect" + "testing" + + "github.com/ignite/cli/v28/ignite/pkg/chainregistry" +) + +func TestCleanGRPCEntries(t *testing.T) { + tests := []struct { + name string + entries []chainregistry.APIProvider + expected []chainregistry.APIProvider + }{ + { + name: "Clean entries with http:// and https://", + entries: []chainregistry.APIProvider{ + {Address: "http://example1.com:1234/"}, + {Address: "https://example2.com:5678"}, + {Address: "http://example3.com:9012"}, + }, + expected: []chainregistry.APIProvider{ + {Address: "example1.com:1234"}, + {Address: "example2.com:5678"}, + {Address: "example3.com:9012"}, + }, + }, + { + name: "Remove trailing slashes", + entries: []chainregistry.APIProvider{ + {Address: "http://example.com:1234/"}, + {Address: "https://example.com:5678/"}, + }, + expected: []chainregistry.APIProvider{ + {Address: "example.com:1234"}, + {Address: "example.com:5678"}, + }, + }, + { + name: "Filter entries without ports", + entries: []chainregistry.APIProvider{ + {Address: "example.com"}, + {Address: "example.com:1234"}, + }, + expected: []chainregistry.APIProvider{ + {Address: "example.com:1234"}, + }, + }, + { + name: "Empty input", + entries: []chainregistry.APIProvider{}, + expected: []chainregistry.APIProvider{}, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + // Copy the entries so that modifications in one test case + // don't affect others + input := make([]chainregistry.APIProvider, len(tt.entries)) + copy(input, tt.entries) + + result := cleanGRPCEntries(input) + + if !reflect.DeepEqual(result, tt.expected) { + t.Errorf("cleanGRPCEntries() = %v, want %v", result, tt.expected) + } + }) + } +} diff --git a/connect/cmd/add.go b/connect/cmd/add.go index d3c36da..ffed466 100644 --- a/connect/cmd/add.go +++ b/connect/cmd/add.go @@ -203,7 +203,7 @@ func initChain(ctx context.Context, chain chainregistry.Chain, endpoint string) return err } - fmt.Printf("%s is ready to Connect!\n", strings.Title(chain.ChainName)) + fmt.Printf("%s is ready to Connect!\n", strings.Title(chain.ChainName)) //nolintlint:staticcheck // strings.Title has a better API. return nil }