Skip to content

Commit 1bd9957

Browse files
authored
feat: support raw format for attach command (#209)
Signed-off-by: chlins <[email protected]>
1 parent c55b6a0 commit 1bd9957

File tree

5 files changed

+29
-9
lines changed

5 files changed

+29
-9
lines changed

cmd/attach.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ func init() {
5959
flags.BoolVarP(&attachConfig.Force, "force", "f", false, "turning on this flag will force the attach, which will overwrite the layer if it already exists with same filepath")
6060
flags.BoolVar(&attachConfig.Nydusify, "nydusify", false, "[EXPERIMENTAL] nydusify the model artifact")
6161
flags.MarkHidden("nydusify")
62+
flags.BoolVar(&attachConfig.Raw, "raw", false, "turning on this flag will attach model artifact layer in raw format")
6263

6364
if err := viper.BindPFlags(flags); err != nil {
6465
panic(fmt.Errorf("bind cache list flags to viper: %w", err))

internal/pb/pb.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func NewProgressBar(writers ...io.Writer) *ProgressBar {
6262
mpbv8.PopCompletedMode(),
6363
mpbv8.WithAutoRefresh(),
6464
mpbv8.WithWidth(60),
65-
mpbv8.WithRefreshRate(200 * time.Millisecond),
65+
mpbv8.WithRefreshRate(300 * time.Millisecond),
6666
}
6767

6868
// If no writer specified, use stdout.

pkg/backend/attach.go

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"fmt"
2323
"io"
2424
"reflect"
25+
"slices"
2526
"sort"
2627

2728
modelspec "github.com/CloudNativeAI/model-spec/specs-go/v1"
@@ -87,13 +88,13 @@ func (b *backend) Attach(ctx context.Context, filepath string, cfg *config.Attac
8788
// Remove the found layer from the layers slice as we need to replace it with the new layer.
8889
for i, layer := range layers {
8990
if layer.Digest == foundLayer.Digest && layer.MediaType == foundLayer.MediaType {
90-
layers = append(layers[:i], layers[i+1:]...)
91+
layers = slices.Delete(layers, i, i+1)
9192
break
9293
}
9394
}
9495
}
9596

96-
proc := b.getProcessor(filepath)
97+
proc := b.getProcessor(filepath, cfg)
9798
if proc == nil {
9899
return fmt.Errorf("failed to get processor for file %s", filepath)
99100
}
@@ -260,21 +261,37 @@ func (b *backend) getModelConfig(ctx context.Context, reference string, desc oci
260261
return &model, nil
261262
}
262263

263-
func (b *backend) getProcessor(filepath string) processor.Processor {
264+
func (b *backend) getProcessor(filepath string, cfg *config.Attach) processor.Processor {
264265
if modelfile.IsFileType(filepath, modelfile.ConfigFilePatterns) {
265-
return processor.NewModelConfigProcessor(b.store, modelspec.MediaTypeModelWeightConfig, []string{filepath})
266+
mediaType := modelspec.MediaTypeModelWeightConfig
267+
if cfg.Raw {
268+
mediaType = modelspec.MediaTypeModelWeightConfigRaw
269+
}
270+
return processor.NewModelConfigProcessor(b.store, mediaType, []string{filepath})
266271
}
267272

268273
if modelfile.IsFileType(filepath, modelfile.ModelFilePatterns) {
269-
return processor.NewModelProcessor(b.store, modelspec.MediaTypeModelWeight, []string{filepath})
274+
mediaType := modelspec.MediaTypeModelWeight
275+
if cfg.Raw {
276+
mediaType = modelspec.MediaTypeModelWeightRaw
277+
}
278+
return processor.NewModelProcessor(b.store, mediaType, []string{filepath})
270279
}
271280

272281
if modelfile.IsFileType(filepath, modelfile.CodeFilePatterns) {
273-
return processor.NewCodeProcessor(b.store, modelspec.MediaTypeModelCode, []string{filepath})
282+
mediaType := modelspec.MediaTypeModelCode
283+
if cfg.Raw {
284+
mediaType = modelspec.MediaTypeModelCodeRaw
285+
}
286+
return processor.NewCodeProcessor(b.store, mediaType, []string{filepath})
274287
}
275288

276289
if modelfile.IsFileType(filepath, modelfile.DocFilePatterns) {
277-
return processor.NewDocProcessor(b.store, modelspec.MediaTypeModelDoc, []string{filepath})
290+
mediaType := modelspec.MediaTypeModelDoc
291+
if cfg.Raw {
292+
mediaType = modelspec.MediaTypeModelDocRaw
293+
}
294+
return processor.NewDocProcessor(b.store, mediaType, []string{filepath})
278295
}
279296

280297
return nil

pkg/backend/attach_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func TestGetProcessor(t *testing.T) {
7373

7474
for _, tt := range tests {
7575
t.Run(tt.filepath, func(t *testing.T) {
76-
proc := b.getProcessor(tt.filepath)
76+
proc := b.getProcessor(tt.filepath, &config.Attach{})
7777
if tt.wantType == "" {
7878
assert.Nil(t, proc)
7979
} else {

pkg/config/attach.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ type Attach struct {
2626
Insecure bool
2727
Nydusify bool
2828
Force bool
29+
Raw bool
2930
}
3031

3132
func NewAttach() *Attach {
@@ -37,6 +38,7 @@ func NewAttach() *Attach {
3738
Insecure: false,
3839
Nydusify: false,
3940
Force: false,
41+
Raw: false,
4042
}
4143
}
4244

0 commit comments

Comments
 (0)