This repository was archived by the owner on Oct 6, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
Add docker model package command
#52
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| package commands | ||
|
|
||
| import ( | ||
| "bufio" | ||
| "encoding/json" | ||
| "fmt" | ||
| "html" | ||
| "io" | ||
| "path/filepath" | ||
|
|
||
| "github.com/docker/model-cli/desktop" | ||
| "github.com/docker/model-distribution/builder" | ||
| "github.com/docker/model-distribution/registry" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| func newPackagedCmd() *cobra.Command { | ||
| var opts packageOptions | ||
|
|
||
| c := &cobra.Command{ | ||
| Use: "package --gguf <path> [--license <path>...] --push TARGET", | ||
| Short: "package a model", | ||
| Args: func(cmd *cobra.Command, args []string) error { | ||
| if len(args) != 1 { | ||
| return fmt.Errorf( | ||
| "'docker model package' requires 1 argument.\n\n"+ | ||
| "Usage: %s\n\n"+ | ||
| "See 'docker model package --help' for more information", | ||
| cmd.Use, | ||
| ) | ||
| } | ||
| if opts.push != true { | ||
| return fmt.Errorf( | ||
| "This version of 'docker model package' requires --push and will write the resulting package directly to the registry.\n\n" + | ||
| "See 'docker model package --help' for more information", | ||
| ) | ||
| } | ||
| if opts.ggufPath == "" { | ||
| return fmt.Errorf( | ||
| "GGUF path is required.\n\n" + | ||
| "See 'docker model package --help' for more information", | ||
| ) | ||
| } | ||
| if !filepath.IsAbs(opts.ggufPath) { | ||
| return fmt.Errorf( | ||
| "GGUF path must be absolute.\n\n" + | ||
| "See 'docker model package --help' for more information", | ||
| ) | ||
| } | ||
| opts.ggufPath = filepath.Clean(opts.ggufPath) | ||
|
|
||
| for i, l := range opts.licensePaths { | ||
| if !filepath.IsAbs(l) { | ||
| return fmt.Errorf( | ||
| "license path must be absolute.\n\n" + | ||
| "See 'docker model package --help' for more information", | ||
| ) | ||
| } | ||
| opts.licensePaths[i] = filepath.Clean(l) | ||
| } | ||
| return nil | ||
| }, | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| if err := packageModel(cmd, args[0], opts); err != nil { | ||
| cmd.PrintErrln("Failed to package model") | ||
| return fmt.Errorf("package model: %w", err) | ||
| } | ||
| return nil | ||
| }, | ||
| } | ||
|
|
||
| c.Flags().StringVar(&opts.ggufPath, "gguf", "", "absolute path to gguf file (required)") | ||
| c.Flags().StringArrayVarP(&opts.licensePaths, "license", "l", nil, "absolute path to a license file") | ||
| c.Flags().BoolVar(&opts.push, "push", false, "push to registry (required)") | ||
| return c | ||
| } | ||
|
|
||
| type packageOptions struct { | ||
| ggufPath string | ||
| licensePaths []string | ||
| push bool | ||
| } | ||
|
|
||
| func packageModel(cmd *cobra.Command, tag string, opts packageOptions) error { | ||
| // Parse the reference | ||
| cmd.PrintErrln("Packaging model %q\n", tag) | ||
| target, err := registry.NewClient( | ||
| registry.WithUserAgent("docker-model-cli/" + desktop.Version), | ||
| ).NewTarget(tag) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // Create package builder with GGUF file | ||
| cmd.PrintErrf("Adding GGUF file from %q\n", opts.ggufPath) | ||
| pkg, err := builder.FromGGUF(opts.ggufPath) | ||
| if err != nil { | ||
| return fmt.Errorf("add gguf file: %w", err) | ||
| } | ||
|
|
||
| // Add license files | ||
| for _, path := range opts.licensePaths { | ||
| cmd.PrintErrf("Adding license file from %q\n", path) | ||
| pkg, err = pkg.WithLicense(path) | ||
| if err != nil { | ||
| return fmt.Errorf("add license file: %w", err) | ||
| } | ||
| } | ||
|
|
||
| // Write the artifact to the registry | ||
| cmd.PrintErrln("Pushing to registry...") | ||
| pr, pw := io.Pipe() | ||
| done := make(chan error, 1) | ||
| go func() { | ||
| defer pw.Close() | ||
| done <- pkg.Build(cmd.Context(), target, pw) | ||
| }() | ||
|
|
||
| scanner := bufio.NewScanner(pr) | ||
| for scanner.Scan() { | ||
| progressLine := scanner.Text() | ||
| if progressLine == "" { | ||
| continue | ||
| } | ||
|
|
||
| // Parse the progress message | ||
| var progressMsg desktop.ProgressMessage | ||
| if err := json.Unmarshal([]byte(html.UnescapeString(progressLine)), &progressMsg); err != nil { | ||
| cmd.PrintErrln("Error displaying progress:", err) | ||
| } | ||
|
|
||
| // Print progress messages | ||
| TUIProgress(progressMsg.Message) | ||
| } | ||
| cmd.PrintErrln("") // newline after progress | ||
|
|
||
| if err := scanner.Err(); err != nil { | ||
| cmd.PrintErrln("Error streaming progress:", err) | ||
| } | ||
| if err := <-done; err != nil { | ||
| return fmt.Errorf("push: %w", err) | ||
| } | ||
| cmd.PrintErrln("Model pushed successfully") | ||
| return nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
vendor/github.com/docker/model-distribution/builder/builder.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.