Skip to content

Commit

Permalink
Implement case insensitive for README
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnaudBger committed Mar 11, 2024
1 parent c895fac commit 75adc32
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 19 deletions.
40 changes: 21 additions & 19 deletions manifest/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -120,33 +119,36 @@ 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) {
doc := m.Package.Doc
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{
Expand Down
14 changes: 14 additions & 0 deletions manifest/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 75adc32

Please sign in to comment.