Skip to content

Commit

Permalink
Clean up imports; fixup a couple missed errors
Browse files Browse the repository at this point in the history
* Add aliases to oci imports (i.e. use 'ocispec' instead of 'v1')
* Replace functions that create empty structs (same thing as just
  creating the struct directly)
* Catch a missed error in build
  • Loading branch information
amisevsk authored and gorkem committed Feb 8, 2024
1 parent 0cecbd4 commit ea74211
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 20 deletions.
4 changes: 0 additions & 4 deletions pkg/artifact/jozu_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ type (
}
)

func NewJozuFile() *JozuFile {
return &JozuFile{}
}

func (jf *JozuFile) LoadModel(file *os.File) error {
// Read the file
data, err := io.ReadAll(file)
Expand Down
4 changes: 2 additions & 2 deletions pkg/artifact/model-layer.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import (
"path/filepath"
"strings"

"github.com/opencontainers/image-spec/specs-go/v1"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
)

type ModelLayer struct {
ContextDir string
Descriptor v1.Descriptor
Descriptor ocispec.Descriptor
}

func NewLayer(context string) *ModelLayer {
Expand Down
24 changes: 12 additions & 12 deletions pkg/artifact/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
_ "crypto/sha512"

"github.com/opencontainers/go-digest"
"github.com/opencontainers/image-spec/specs-go"
v1 "github.com/opencontainers/image-spec/specs-go/v1"
specs "github.com/opencontainers/image-spec/specs-go"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"oras.land/oras-go/v2/content/oci"
)

Expand All @@ -31,7 +31,7 @@ func NewArtifactStore() *Store {
}
}

func (store *Store) saveContentLayer(layer *ModelLayer) (*v1.Descriptor, error) {
func (store *Store) saveContentLayer(layer *ModelLayer) (*ocispec.Descriptor, error) {
ctx := context.Background()

buf := &bytes.Buffer{}
Expand All @@ -41,7 +41,7 @@ func (store *Store) saveContentLayer(layer *ModelLayer) (*v1.Descriptor, error)
}

// Create a descriptor for the layer
desc := v1.Descriptor{
desc := ocispec.Descriptor{
MediaType: ModelLayerMediaType,
Digest: digest.FromBytes(buf.Bytes()),
Size: int64(buf.Len()),
Expand All @@ -55,13 +55,13 @@ func (store *Store) saveContentLayer(layer *ModelLayer) (*v1.Descriptor, error)
return &desc, nil
}

func (store *Store) saveConfigFile(model *JozuFile) (*v1.Descriptor, error) {
func (store *Store) saveConfigFile(model *JozuFile) (*ocispec.Descriptor, error) {
ctx := context.Background()
buf, err := model.MarshalToJSON()
if err != nil {
return nil, err
}
desc := v1.Descriptor{
desc := ocispec.Descriptor{
MediaType: ModelConfigMediaType,
Digest: digest.FromBytes(buf),
Size: int64(len(buf)),
Expand All @@ -73,12 +73,12 @@ func (store *Store) saveConfigFile(model *JozuFile) (*v1.Descriptor, error) {
return &desc, nil
}

func (store *Store) saveModelManifest(model *Model, config *v1.Descriptor) (*v1.Manifest, error) {
func (store *Store) saveModelManifest(model *Model, config *ocispec.Descriptor) (*ocispec.Manifest, error) {
ctx := context.Background()
manifest := v1.Manifest{
manifest := ocispec.Manifest{
Versioned: specs.Versioned{SchemaVersion: 2},
Config: *config,
Layers: []v1.Descriptor{},
Layers: []ocispec.Descriptor{},
}
// Add the layers to the manifest
for _, layer := range model.Layers {
Expand All @@ -90,8 +90,8 @@ func (store *Store) saveModelManifest(model *Model, config *v1.Descriptor) (*v1.
return nil, err
}
// Push the manifest to the store
desc := v1.Descriptor{
MediaType: v1.MediaTypeImageManifest,
desc := ocispec.Descriptor{
MediaType: ocispec.MediaTypeImageManifest,
Digest: digest.FromBytes(manifestBytes),
Size: int64(len(manifestBytes)),
}
Expand All @@ -102,7 +102,7 @@ func (store *Store) saveModelManifest(model *Model, config *v1.Descriptor) (*v1.
return &manifest, nil
}

func (store *Store) SaveModel(model *Model) (*v1.Manifest, error) {
func (store *Store) SaveModel(model *Model) (*ocispec.Manifest, error) {
config, err := store.saveConfigFile(model.Config)
if err != nil {
return nil, err
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func NewCmdBuild() *cobra.Command {
fmt.Println(err)
return
}
options.RunBuild()
err = options.RunBuild()
if err != nil {
fmt.Println(err)
return
Expand Down Expand Up @@ -88,7 +88,7 @@ func (options *BuildOptions) RunBuild() error {
return err
}
defer modelfile.Close()
jozufile := artifact.NewJozuFile()
jozufile := &artifact.JozuFile{}
if err = jozufile.LoadModel(modelfile); err != nil {
fmt.Println(err)
return err
Expand Down

0 comments on commit ea74211

Please sign in to comment.