-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability to list remote repository models via 'jmm models' command
Add optional argument to jmm models: jmm models <reference> to optionally list models in a remote repository. If <reference> includes a tag, models only lists the model with that tag, if it exists.
- Loading branch information
Showing
5 changed files
with
188 additions
and
54 deletions.
There are no files selected for viewing
This file contains 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 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 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,96 @@ | ||
package models | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"jmm/pkg/artifact" | ||
|
||
ocispec "github.com/opencontainers/image-spec/specs-go/v1" | ||
"oras.land/oras-go/v2/registry" | ||
"oras.land/oras-go/v2/registry/remote" | ||
) | ||
|
||
func listRemoteModels(ctx context.Context, remoteRef *registry.Reference, useHttp bool) ([]string, error) { | ||
remoteRegistry, err := remote.NewRegistry(remoteRef.Registry) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to read registry: %w", err) | ||
} | ||
remoteRegistry.PlainHTTP = useHttp | ||
|
||
repo, err := remoteRegistry.Repository(ctx, remoteRef.Repository) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to read repository: %w", err) | ||
} | ||
if remoteRef.Reference != "" { | ||
return listImageTag(ctx, repo, remoteRef) | ||
} | ||
return listTags(ctx, repo, remoteRef) | ||
} | ||
|
||
func listTags(ctx context.Context, repo registry.Repository, ref *registry.Reference) ([]string, error) { | ||
var tags []string | ||
err := repo.Tags(ctx, "", func(tagsPage []string) error { | ||
tags = append(tags, tagsPage...) | ||
return nil | ||
}) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to list tags on repostory: %w", err) | ||
} | ||
|
||
var allLines []string | ||
for _, tag := range tags { | ||
tagRef := ®istry.Reference{ | ||
Registry: ref.Registry, | ||
Repository: ref.Repository, | ||
Reference: tag, | ||
} | ||
infoLines, err := listImageTag(ctx, repo, tagRef) | ||
if err != nil { | ||
return nil, err | ||
} | ||
allLines = append(allLines, infoLines...) | ||
} | ||
|
||
return allLines, nil | ||
} | ||
|
||
func listImageTag(ctx context.Context, repo registry.Repository, ref *registry.Reference) ([]string, error) { | ||
manifestDesc, manifestReader, err := repo.FetchReference(ctx, ref.Reference) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to read reference: %w", err) | ||
} | ||
manifestBytes, err := io.ReadAll(manifestReader) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to read manifest: %w", err) | ||
} | ||
manifest := &ocispec.Manifest{} | ||
if err := json.Unmarshal(manifestBytes, manifest); err != nil { | ||
return nil, fmt.Errorf("failed to parse manifest: %w", err) | ||
} | ||
|
||
configReader, err := repo.Fetch(ctx, manifest.Config) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to read config reference: %w", err) | ||
} | ||
configBytes, err := io.ReadAll(configReader) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to read config: %w", err) | ||
} | ||
config := &artifact.JozuFile{} | ||
if err := json.Unmarshal(configBytes, config); err != nil { | ||
return nil, fmt.Errorf("failed to parse config: %w", err) | ||
} | ||
|
||
// Manifest descriptor may not have annotation for tag, add it here for safety | ||
if _, ok := manifestDesc.Annotations[ocispec.AnnotationRefName]; !ok { | ||
if manifestDesc.Annotations == nil { | ||
manifestDesc.Annotations = map[string]string{} | ||
} | ||
manifestDesc.Annotations[ocispec.AnnotationRefName] = ref.Reference | ||
} | ||
|
||
info := getManifestInfoLine(ref.Repository, manifestDesc, manifest, config) | ||
return []string{info}, nil | ||
} |
This file contains 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 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