-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f98e5c4
commit 2a8a7e4
Showing
3 changed files
with
127 additions
and
0 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,119 @@ | ||
package app | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/docker/labs-brown-tape/oci" | ||
) | ||
|
||
type TapeViewCommand struct { | ||
tape *TapeCommand | ||
OutputFormatOptions | ||
|
||
Image string `short:"I" long:"image" description:"Name of the image to view" required:"true"` | ||
} | ||
|
||
type artefactInfo struct { | ||
RawManifests struct { | ||
Index rawManifest[oci.IndexManifest] `json:"index"` | ||
Content rawManifest[oci.Manifest] `json:"content"` | ||
Attest rawManifest[oci.Manifest] `json:"attest"` | ||
} `json:"rawManifests"` | ||
} | ||
|
||
type rawManifest[T oci.Manifest | oci.IndexManifest] struct { | ||
Digest string `json:"digest,omitempty"` | ||
Manifest *T `json:"manifest,omitempty"` | ||
} | ||
|
||
func (c *TapeViewCommand) Execute(args []string) error { | ||
ctx := context.WithValue(c.tape.ctx, "command", "view") | ||
if len(args) != 0 { | ||
return fmt.Errorf("unexpected arguments: %v", args) | ||
} | ||
|
||
if err := c.tape.Init(); err != nil { | ||
return err | ||
} | ||
|
||
client := oci.NewClient(nil) | ||
|
||
outputInfo, err := c.CollectInfo(ctx, client) | ||
if err != nil { | ||
return fmt.Errorf("failed to collect info about artifact: %w", err) | ||
} | ||
|
||
if err := c.PrintInfo(ctx, outputInfo); err != nil { | ||
return fmt.Errorf("failed to print info about artifact: %w", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (c *TapeViewCommand) CollectInfo(ctx context.Context, client *oci.Client) (*artefactInfo, error) { | ||
artefactInfo := &artefactInfo{} | ||
|
||
imageIndex, indexManifest, _, err := client.GetIndexOrImage(ctx, c.Image) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if indexManifest == nil { | ||
return nil, fmt.Errorf("no index manifest found for %q", c.Image) | ||
} | ||
|
||
imageIndexDigest, err := imageIndex.Digest() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
artefactInfo.RawManifests.Index = rawManifest[oci.IndexManifest]{ | ||
Digest: imageIndexDigest.String(), | ||
Manifest: indexManifest, | ||
} | ||
|
||
imageInfo, manifests, err := client.FetchFromIndexOrImage(ctx, imageIndex, indexManifest, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if len(imageInfo) == 0 { | ||
return nil, fmt.Errorf("no images found in index %q", c.Image) | ||
} | ||
|
||
for i := range imageInfo { | ||
info := imageInfo[i] | ||
switch info.MediaType { | ||
case oci.ContentMediaType: | ||
case oci.AttestMediaType: | ||
} | ||
} | ||
|
||
for digest := range manifests { | ||
m := rawManifest[oci.Manifest]{ | ||
Digest: digest.String(), | ||
Manifest: manifests[digest], | ||
} | ||
switch m.Manifest.Config.MediaType { | ||
case oci.ContentMediaType: | ||
artefactInfo.RawManifests.Content = m | ||
case oci.AttestMediaType: | ||
artefactInfo.RawManifests.Attest = m | ||
} | ||
} | ||
return artefactInfo, nil | ||
} | ||
|
||
func (c *TapeViewCommand) PrintInfo(ctx context.Context, outputInfo *artefactInfo) error { | ||
stdj := json.NewEncoder(os.Stdout) | ||
switch c.OutputFormat { | ||
case OutputFormatDirectJSON: | ||
stdj.SetIndent("", " ") | ||
if err := stdj.Encode(outputInfo); err != nil { | ||
return fmt.Errorf("failed to marshal output: %w", err) | ||
} | ||
} | ||
return nil | ||
} |