@@ -19,13 +19,12 @@ package backend
1919import (
2020 "context"
2121 "fmt"
22- "os"
23- "path/filepath"
2422
2523 "github.com/CloudNativeAI/modctl/pkg/backend/build"
2624 "github.com/CloudNativeAI/modctl/pkg/backend/processor"
2725 "github.com/CloudNativeAI/modctl/pkg/modelfile"
2826
27+ modelspec "github.com/CloudNativeAI/model-spec/specs-go/v1"
2928 humanize "github.com/dustin/go-humanize"
3029 ocispec "github.com/opencontainers/image-spec/specs-go/v1"
3130)
@@ -45,7 +44,7 @@ func (b *backend) Build(ctx context.Context, modelfilePath, workDir, target stri
4544
4645 repo , tag := ref .Repository (), ref .Tag ()
4746 layers := []ocispec.Descriptor {}
48- layerDescs , err := b .process (ctx , workDir , repo , getProcessors (modelfile )... )
47+ layerDescs , err := b .process (ctx , workDir , repo , b . getProcessors (modelfile )... )
4948 if err != nil {
5049 return fmt .Errorf ("failed to process files: %w" , err )
5150 }
@@ -70,64 +69,41 @@ func (b *backend) Build(ctx context.Context, modelfilePath, workDir, target stri
7069 return nil
7170}
7271
73- func defaultProcessors () []processor.Processor {
72+ func ( b * backend ) defaultProcessors () []processor.Processor {
7473 return []processor.Processor {
75- processor .NewLicenseProcessor (),
76- processor .NewReadmeProcessor (),
74+ // by default process the readme and license file.
75+ processor .NewReadmeProcessor (b .store , modelspec .MediaTypeModelDoc , []string {"README.md" , "README" }),
76+ processor .NewLicenseProcessor (b .store , modelspec .MediaTypeModelDoc , []string {"LICENSE.txt" , "LICENSE" }),
7777 }
7878}
7979
80- func getProcessors (modelfile modelfile.Modelfile ) []processor.Processor {
81- processors := defaultProcessors ()
80+ func ( b * backend ) getProcessors (modelfile modelfile.Modelfile ) []processor.Processor {
81+ processors := b . defaultProcessors ()
8282
8383 if configs := modelfile .GetConfigs (); len (configs ) > 0 {
84- processors = append (processors , processor .NewModelConfigProcessor (configs ))
84+ processors = append (processors , processor .NewModelConfigProcessor (b . store , modelspec . MediaTypeModelWeightConfig , configs ))
8585 }
8686
8787 if models := modelfile .GetModels (); len (models ) > 0 {
88- processors = append (processors , processor .NewModelProcessor (models ))
88+ processors = append (processors , processor .NewModelProcessor (b . store , modelspec . MediaTypeModelWeight , models ))
8989 }
9090
9191 return processors
9292}
9393
9494// process walks the user work directory and process the identified files.
9595func (b * backend ) process (ctx context.Context , workDir string , repo string , processors ... processor.Processor ) ([]ocispec.Descriptor , error ) {
96- layers := []ocispec.Descriptor {}
97- // walk the user work directory and handle the default identified files.
98- if err := filepath . Walk ( workDir , func ( path string , info os. FileInfo , err error ) error {
96+ descriptors := []ocispec.Descriptor {}
97+ for _ , p := range processors {
98+ descs , err := p . Process ( ctx , workDir , repo )
9999 if err != nil {
100- return fmt .Errorf ("failed to walk directory: %w" , err )
101- }
102- // skip directories.
103- if info .IsDir () {
104- return nil
105- }
106- // get absolute path.
107- path , err = filepath .Abs (path )
108- if err != nil {
109- return fmt .Errorf ("failed to get absolute path: %w" , err )
110- }
111- // fan-in file processors.
112- for _ , p := range processors {
113- // process the file if it can be recognized.
114- if p .Identify (ctx , path , info ) {
115- desc , err := p .Process (ctx , b .store , repo , path , workDir )
116- if err != nil {
117- return fmt .Errorf ("failed to process file: %w" , err )
118- }
119-
120- fmt .Printf ("%-15s => %s (%s)\n " , "Built blob" , desc .Digest , humanize .IBytes (uint64 (desc .Size )))
121- layers = append (layers , desc )
122- }
100+ return nil , err
123101 }
124102
125- return nil
126- }); err != nil {
127- return nil , err
103+ descriptors = append (descriptors , descs ... )
128104 }
129105
130- return layers , nil
106+ return descriptors , nil
131107}
132108
133109// manifestAnnotation returns the annotations for the manifest.
0 commit comments