Skip to content

Commit

Permalink
feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
julienrbrt committed Feb 6, 2025
1 parent ca316fa commit 483cc61
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 15 deletions.
2 changes: 1 addition & 1 deletion connect/chains/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
26 changes: 13 additions & 13 deletions connect/chains/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:]
Expand All @@ -128,7 +129,6 @@ func EnrichChain(chain *chainregistry.Chain) error {

cleanEntries = append(cleanEntries, api)
}
chain.APIs.Grpc = cleanEntries

return nil
return cleanEntries
}
71 changes: 71 additions & 0 deletions connect/chains/registry_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}
2 changes: 1 addition & 1 deletion connect/cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit 483cc61

Please sign in to comment.