oras-project / oras

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

feat: Use cobra.Command.OutOrStdout method for output #1288

Closed TerryHowe closed 3 months ago

TerryHowe commented 3 months ago

What this PR does / why we need it:

This contains the command refactor I did in another PR.

With this change, the output of a command can be set to something other than stdout. For example capture the output of a command:

    var stringWriter strings.Builder
    cmd := root.New()
    cmd.SetArgs([]string{"repo", "ls", "mcr.microsoft.com"})
    cmd.SetOutput(&stringWriter)
    _ := cmd.Execute()
    fmt.Println(stringWriter.String())
codecov[bot] commented 3 months ago

Codecov Report

Attention: Patch coverage is 91.48936% with 4 lines in your changes are missing coverage. Please review.

Project coverage is 82.04%. Comparing base (f1d319f) to head (fe953b1).

Files Patch % Lines
cmd/oras/root/login.go 66.66% 1 Missing and 2 partials :warning:
cmd/oras/root/discover.go 87.50% 1 Missing :warning:
Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #1288 +/- ## ========================================== + Coverage 81.94% 82.04% +0.09% ========================================== Files 83 83 Lines 4005 4016 +11 ========================================== + Hits 3282 3295 +13 + Misses 500 498 -2 Partials 223 223 ```

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

qweeah commented 3 months ago

Need to confirm which output should be covered in this PR.

The proposed change in this PR doesn't cover all output of ORAS. E.g. the output of oras push/attach --format xxx which is designed to be used for automation. (There are also undergoing efforts for other commands, see #638) We have a draft doc https://hackmd.io/@shizh/HyFZkEcca to categorize all types of output in ORAS.

@TerryHowe Looks like you are trying to do automation and the target output to be changed is metadata and raw output?

FeynmanZhou commented 3 months ago

Thanks @TerryHowe . This is a big PR. From users' perspective, I am curious on what's the impact to ORAS CLI output. There is not an issue associated with this PR to describe the problem or enhancement.

TerryHowe commented 3 months ago

This change has no effect on the format of the output and from my understanding no effect on CLI output. The change is really just for go scripting.

TerryHowe commented 3 months ago

The goal is to allow oras go scripting to be able to capture output from commands without messing with stdout

qweeah commented 3 months ago

The goal is to allow oras go scripting to be able to capture output from commands without messing with stdout

The output of --format won't go into STDOUT, e.g. localhost:5000/test@sha256:16ce5d4fb98496ec805e6b2401213598e710dbd936fb58cb7a325d2582924694 cannot captured by the go script.

> oras version
Version:        1.2.0-beta.1
Go version:     go1.21.6
Git commit:     9ffdb3eec60b969d842af1a9e699202e0827fa01
Git tree state: clean
> oras push localhost:5000/test:format --format {{.Ref}}
✓ Uploaded  application/vnd.oci.empty.v1+json                                                      2/2  B 100.00%   37ms
  └─ sha256:44136fa355b3678a1146ad16f7e8649e94fb4fc21fe77e8310c060f61caaff8a
✓ Uploaded  application/vnd.oci.image.manifest.v1+json                                         535/535  B 100.00%     0s
  └─ sha256:16ce5d4fb98496ec805e6b2401213598e710dbd936fb58cb7a325d2582924694
localhost:5000/test@sha256:16ce5d4fb98496ec805e6b2401213598e710dbd936fb58cb7a325d2582924694
TerryHowe commented 3 months ago

The output of --format won't go into STDOUT, e.g. localhost:5000/test@sha256:16ce5d4fb98496ec805e6b2401213598e710dbd936fb58cb7a325d2582924694 cannot captured by the go script.

I'd sooner handle format as a follow up.