Skip to content

Commit

Permalink
Support http and https for package dependencies.
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexandre Bourget committed May 17, 2022
1 parent 5112914 commit 2b7b28c
Showing 1 changed file with 29 additions and 7 deletions.
36 changes: 29 additions & 7 deletions manifest/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,49 @@ import (
"crypto/sha256"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"

pbsubstreams "github.com/streamingfast/substreams/pb/sf/substreams/v1"
"golang.org/x/mod/semver"
"google.golang.org/protobuf/proto"
)

func New(inputFile string) (m *pbsubstreams.Package, err error) {
if strings.HasSuffix(inputFile, ".yaml") {
return NewFromYAML(inputFile)
func New(input string) (m *pbsubstreams.Package, err error) {
if u, err := url.Parse(input); err == nil && u.Scheme == "http" || u.Scheme == "https" {
return NewFromURL(input)
}
return NewFromPackageFile(inputFile)
if strings.HasSuffix(input, ".yaml") {
return NewFromYAML(input)
}
return NewFromFile(input)
}

func NewFromPackageFile(inputFile string) (pkg *pbsubstreams.Package, err error) {
pkg = &pbsubstreams.Package{}
func NewFromFile(inputFile string) (pkg *pbsubstreams.Package, err error) {
cnt, err := ioutil.ReadFile(inputFile)
if err != nil {
return nil, err
}
if err := proto.Unmarshal(cnt, pkg); err != nil {

return NewFromContents(inputFile, cnt)
}

func NewFromURL(input string) (pkg *pbsubstreams.Package, err error) {
resp, err := http.DefaultClient.Get(input)
if err != nil {
return nil, fmt.Errorf("error downloading %q: %w", input, err)
}
cnt, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("error reading %q: %w", input, err)
}
return NewFromContents(input, cnt)
}

func NewFromContents(inputFile string, contents []byte) (pkg *pbsubstreams.Package, err error) {
pkg = &pbsubstreams.Package{}
if err := proto.Unmarshal(contents, pkg); err != nil {
return nil, err
}

Expand Down

0 comments on commit 2b7b28c

Please sign in to comment.