From 75adc32f4648fc141b54f940617daa437f85cab1 Mon Sep 17 00:00:00 2001 From: arnaudberger Date: Mon, 11 Mar 2024 16:02:44 -0400 Subject: [PATCH] Implement case insensitive for README --- manifest/package.go | 40 +++++++++++++++++++++------------------- manifest/utils.go | 14 ++++++++++++++ 2 files changed, 35 insertions(+), 19 deletions(-) diff --git a/manifest/package.go b/manifest/package.go index 69294ef6a..5078c1d6d 100644 --- a/manifest/package.go +++ b/manifest/package.go @@ -2,13 +2,12 @@ package manifest import ( "fmt" - "os" - "path" - "path/filepath" - "github.com/jhump/protoreflect/desc" "github.com/jhump/protoreflect/dynamic" pbsubstreams "github.com/streamingfast/substreams/pb/sf/substreams/v1" + "os" + "path" + "path/filepath" ) type manifestConverter struct { @@ -120,15 +119,17 @@ func (r *manifestConverter) manifestToPkg(manif *Manifest) (*pbsubstreams.Packag } func (m *Manifest) readFileFromName(filename string) ([]byte, error) { - if _, err := os.Stat(filepath.Join(m.Workdir, filename)); err == nil { - filePath := filepath.Join(m.Workdir, filename) - content, err := os.ReadFile(filePath) - if err != nil { - return nil, fmt.Errorf("failed to read %s: %w", filename, err) - } - return content, nil + fileNameFound, err := searchExistingCaseInsensitiveFileName(m.Workdir, filename) + if err != nil { + return nil, err } - return nil, nil + + filePath := filepath.Join(m.Workdir, fileNameFound) + content, err := os.ReadFile(filePath) + if err != nil { + return nil, fmt.Errorf("failed to read %s: %w", fileNameFound, err) + } + return content, nil } func (r *manifestConverter) convertToPkg(m *Manifest) (pkg *pbsubstreams.Package, err error) { @@ -136,17 +137,18 @@ func (r *manifestConverter) convertToPkg(m *Manifest) (pkg *pbsubstreams.Package if doc == "" { readmeContent, err := m.readFileFromName("README.md") if err != nil { - return nil, fmt.Errorf("reading file: %w", err) - } - if readmeContent == nil { + if !os.IsNotExist(err) { + return nil, fmt.Errorf("reading file: %w", err) + } readmeContent, err = m.readFileFromName("README") if err != nil { - return nil, fmt.Errorf("reading file: %w", err) + if !os.IsNotExist(err) { + return nil, fmt.Errorf("reading file: %w", err) + } } } - if readmeContent != nil { - doc = string(readmeContent) - } + + doc = string(readmeContent) } pkgMeta := &pbsubstreams.PackageMetadata{ diff --git a/manifest/utils.go b/manifest/utils.go index dc5aee9bd..4bb3052b1 100644 --- a/manifest/utils.go +++ b/manifest/utils.go @@ -60,6 +60,20 @@ func getNetworkEndpointFromEnvironment(networkName string) string { return networkEndpoint } +func searchExistingCaseInsensitiveFileName(dir, filename string) (string, error) { + files, err := os.ReadDir(dir) + if err != nil { + return "", fmt.Errorf("reading dir: %w", err) + } + + for _, file := range files { + if strings.EqualFold(file.Name(), filename) { + return file.Name(), nil + } + } + return "", os.ErrNotExist +} + // TODO: replace by the blockchain-based discovery when available var HardcodedEndpoints = map[string]string{ "mainnet": "mainnet.eth.streamingfast.io:443",