Skip to content

Commit

Permalink
feat: basic piral support
Browse files Browse the repository at this point in the history
  • Loading branch information
nadilas committed Apr 2, 2024
1 parent 330ee77 commit c332270
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 17 deletions.
27 changes: 19 additions & 8 deletions internal/module.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package internal

import (
"encoding/json"
"fmt"
"sort"

Expand All @@ -16,6 +17,22 @@ type Module struct {
selectedVersion *Version
}

func FromBytes(bts []byte) (*Module, error) {
module := &Module{}
err := json.Unmarshal(bts, module)
if err != nil {
return nil, err
}
module.Init()
return module, nil
}

func (m *Module) Init() {
for _, version := range m.Versions {
version.Init()
}
}

// Prints the module name and its version selector
//
// If the module has a selected version, it is used instead of the default Latest()
Expand Down Expand Up @@ -43,7 +60,7 @@ func (m *Module) HasVersion(version *semver.Version) bool {
return false
}

//VersionStrings serializes the versions to string
// VersionStrings serializes the versions to string
func (m *Module) VersionStrings() []string {
l := make([]string, len(m.Versions))
for i, v := range m.Versions {
Expand All @@ -52,7 +69,7 @@ func (m *Module) VersionStrings() []string {
return l
}

//SelectVersion selects the highest version number which matches constraint
// SelectVersion selects the highest version number which matches constraint
func (m *Module) SelectVersion(constraint *semver.Constraints) *Version {
// descending sort of versions
sort.SliceStable(m.Versions, func(i, j int) bool { return m.Versions[i].Version().GreaterThan(m.Versions[j].Version()) })
Expand Down Expand Up @@ -90,9 +107,3 @@ func (m *Module) Latest() *Version {
sort.SliceStable(m.Versions, func(i, j int) bool { return m.Versions[i].Version().GreaterThan(m.Versions[j].Version()) })
return m.Versions[0]
}

func (m *Module) Init() {
for _, version := range m.Versions {
version.Init()
}
}
15 changes: 7 additions & 8 deletions internal/storage/s3/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"os"
"path"
"strings"
Expand Down Expand Up @@ -127,12 +127,14 @@ func (s *Storage) ModuleResources(ctx context.Context, module string, version st
Recursive: true,
}) {
var bts []byte
if loadData {
// always load meta.json as basic support for piral framework
// to avoid a second network call to get meta.json content per module
if loadData || strings.HasSuffix(object.Key, "meta.json") {
obj, err := s.minioClient.GetObject(ctx, s.bucket, object.Key, minio.GetObjectOptions{})
if err != nil {
return nil, err
}
bts, err = ioutil.ReadAll(obj)
bts, err = io.ReadAll(obj)
if err != nil {
errResp := minio.ToErrorResponse(err)
if errResp.Code == "NoSuchKey" {
Expand Down Expand Up @@ -188,21 +190,18 @@ func (s *Storage) loadModule(ctx context.Context, objectName string, loadData bo
if err != nil {
return nil, err
}
var module = &internal.Module{}
bts, err := ioutil.ReadAll(manifestObj)
bts, err := io.ReadAll(manifestObj)
if err != nil {
errResp := minio.ToErrorResponse(err)
if errResp.Code == "NoSuchKey" {
return nil, storage.ModuleNotFound
}
return nil, err
}
err = json.Unmarshal(bts, module)
module, err := internal.FromBytes(bts)
if err != nil {
return nil, err
}
// FIXME: remove this hardcoded dependency MUST be called first after unmarshaling
module.Init()
// load available resources
for _, version := range module.Versions {
version.Files, _ = s.ModuleResources(ctx, module.Name, version.Version().String(), loadData)
Expand Down
2 changes: 1 addition & 1 deletion rpc/get_url.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ func (s *Server) GetUrl(ctx context.Context, c *connect.Request[moarpb.GetUrlReq
selectedVersionString = version.Version().String()
default:
version = module.Latest()
selectedVersionString = "latest"
s.logger.Debugf("Module (%s) version is not specified in query, defaulting to latest", request.ModuleName)
selectedVersionString = version.Value
}

var resources []*moarpb.VersionResource
Expand Down

0 comments on commit c332270

Please sign in to comment.