oras-project / oras

OCI registry client - managing content like artifacts, images, packages
https://oras.land
Apache License 2.0
1.43k stars 174 forks source link

chore: Add format type text #1397

Closed TerryHowe closed 3 months ago

TerryHowe commented 4 months ago

What this PR does / why we need it:

Introduce text as default format type for pull, push, manifest fetch and attach commands.

The only change in E2E experience is in the help document. Taking oras attach as an example:

Before

> oras attach -h
...
--format string                               [Experimental] Format output using a custom template:
                                                    'json':         Print in JSON format
                                                    'go-template':  Print output using the given Go template
...

After

> oras attach -h
...
      --format string                               [Experimental] Format output using a custom template:
                                                    'json':         Print in JSON format
                                                    'go-template':  Print output using the given Go template
                                                    'text':         Print in text format (default "text")
...
codecov[bot] commented 4 months ago

Codecov Report

Attention: Patch coverage is 88.00000% with 3 lines in your changes missing coverage. Please review.

Project coverage is 85.32%. Comparing base (2aa005c) to head (40ca076).

Files Patch % Lines
cmd/oras/internal/display/handler.go 62.50% 0 Missing and 3 partials :warning:
Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #1397 +/- ## ========================================== + Coverage 85.14% 85.32% +0.17% ========================================== Files 107 107 Lines 3790 3795 +5 ========================================== + Hits 3227 3238 +11 + Misses 336 333 -3 + Partials 227 224 -3 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

qweeah commented 3 months ago

Since this PR unifies default value for format type, I am thinking maybe we should take a step further and add a new function to set default and optional allowed types, e.g.

// Format contains input and parsed options for formatted output flags.
type Format struct {
    FormatFlag   string
    Type         string
    Template     string
    defaultType  *FormatType
    allowedTypes []*FormatType
}

// SetTypes sets the default format type and allowed format types.
func (opts *Format) SetTypes(defaultType *FormatType, allowedTypes ...*FormatType) {
    opts.defaultType = defaultType
    opts.allowedTypes = append(allowedTypes, defaultType)
}

So taking oras attach as an example, code changes in command with default text output would be like:

- opts.AllowedTypes = []*option.FormatType{option.FormatTypeJSON, option.FormatTypeGoTemplate}
+ opts.SetTypes(option.FormatTypeText, option.FormatTypeJSON, option.FormatTypeGoTemplate)

For oras discover, the changes can be:

-   opts.AllowedTypes = []*option.FormatType{
+   opts.SetType(
        option.FormatTypeTree,
        option.FormatTypeTable,
        option.FormatTypeJSON.WithUsage("Get direct referrers and output in JSON format"),
        option.FormatTypeGoTemplate.WithUsage("Print direct referrers using the given Go template"),
-   }
+   )