Skip to content

Commit

Permalink
print in table (oras-project#258)
Browse files Browse the repository at this point in the history
Signed-off-by: Shiwei Zhang <shizh@microsoft.com>
Signed-off-by: Sajay Antony <sajaya@microsoft.com>
  • Loading branch information
shizhMSFT authored and sajayantony committed Sep 13, 2021
1 parent 188c111 commit 8ba7ecf
Showing 1 changed file with 29 additions and 7 deletions.
36 changes: 29 additions & 7 deletions cmd/oras/discover.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"os"
"strings"

ctxo "github.com/deislabs/oras/pkg/context"
"github.com/deislabs/oras/pkg/oras"
Expand Down Expand Up @@ -81,22 +82,43 @@ func runDiscover(opts discoverOptions) error {
}

if opts.outputJSON {
printDiscoveredReferences(desc, refs)
printDiscoveredReferencesJSON(desc, refs)
} else {
fmt.Println("Discovered", len(refs), "artifacts referencing", opts.targetRef)
fmt.Println("Digest:", desc.Digest)
for _, ref := range refs {
fmt.Println("Reference:", ref.Digest)
if opts.verbose {
printJSON(ref.Artifact)
}

if len(refs) != 0 {
fmt.Println()
printDiscoveredReferencesTable(refs, opts.verbose)
}
}

return nil
}

func printDiscoveredReferences(desc ocispec.Descriptor, refs []remotes.DiscoveredArtifact) {
func printDiscoveredReferencesTable(refs []remotes.DiscoveredArtifact, verbose bool) {
typeNameTitle := "Artifact Type"
typeNameLength := len(typeNameTitle)
for _, ref := range refs {
if length := len(ref.Artifact.ArtifactType); length > typeNameLength {
typeNameLength = length
}
}

print := func(key string, value interface{}) {
fmt.Println(key, strings.Repeat(" ", typeNameLength-len(key)+1), value)
}

print(typeNameTitle, "Digest")
for _, ref := range refs {
print(ref.Artifact.ArtifactType, ref.Digest)
if verbose {
printJSON(ref.Artifact)
}
}
}

func printDiscoveredReferencesJSON(desc ocispec.Descriptor, refs []remotes.DiscoveredArtifact) {
type reference struct {
Digest digest.Digest `json:"digest"`
Manifest artifactspec.Artifact `json:"manifest"`
Expand Down

0 comments on commit 8ba7ecf

Please sign in to comment.