Skip to content

Commit

Permalink
fix: make --output - implies --no-tty (#1286)
Browse files Browse the repository at this point in the history
Signed-off-by: Xiaoxuan Wang <[email protected]>
  • Loading branch information
wangxiaoxuan273 authored Mar 15, 2024
1 parent 9dbac04 commit ec23755
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
13 changes: 12 additions & 1 deletion cmd/oras/internal/option/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
"oras.land/oras/internal/trace"
)

const NoTTYFlag = "no-tty"

// Common option struct.
type Common struct {
Debug bool
Expand All @@ -38,7 +40,7 @@ type Common struct {
func (opts *Common) ApplyFlags(fs *pflag.FlagSet) {
fs.BoolVarP(&opts.Debug, "debug", "d", false, "output debug logs (implies --no-tty)")
fs.BoolVarP(&opts.Verbose, "verbose", "v", false, "verbose output")
fs.BoolVarP(&opts.noTTY, "no-tty", "", false, "[Preview] do not show progress output")
fs.BoolVarP(&opts.noTTY, NoTTYFlag, "", false, "[Preview] do not show progress output")
}

// WithContext returns a new FieldLogger and an associated Context derived from ctx.
Expand All @@ -63,3 +65,12 @@ func (opts *Common) parseTTY(f *os.File) error {
}
return nil
}

// UpdateTTY updates the TTY value, given the status of --no-tty flag and output
// path value.
func (opts *Common) UpdateTTY(flagPresent bool, toSTDOUT bool) {
ttyEnforced := flagPresent && !opts.noTTY
if opts.noTTY || (toSTDOUT && !ttyEnforced) {
opts.TTY = nil
}
}
44 changes: 44 additions & 0 deletions cmd/oras/internal/option/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ limitations under the License.
package option

import (
"os"
"reflect"
"testing"

"github.com/spf13/pflag"
Expand All @@ -28,3 +30,45 @@ func TestCommon_FlagsInit(t *testing.T) {

ApplyFlags(&test, pflag.NewFlagSet("oras-test", pflag.ExitOnError))
}

func TestCommon_UpdateTTY(t *testing.T) {
testTTY := &os.File{}
tests := []struct {
name string
flagPresent bool
toSTDOUT bool
noTTY bool
expectedTTY *os.File
}{
{
"output to STDOUT, --no-tty flag not used, reset TTY", false, true, false, nil,
},
{
"output to STDOUT, --no-tty set to true, reset TTY", true, true, true, nil,
},
{
"output to STDOUT, --no-tty set to false", true, true, false, testTTY,
},
{
"not output to STDOUT, --no-tty flag not used", false, false, false, testTTY,
},
{
"not output to STDOUT, --no-tty set to true, reset TTY", true, false, true, nil,
},
{
"not output to STDOUT, --no-tty set to false", true, false, false, testTTY,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
opts := &Common{
noTTY: tt.noTTY,
TTY: testTTY,
}
opts.UpdateTTY(tt.flagPresent, tt.toSTDOUT)
if !reflect.DeepEqual(opts.TTY, tt.expectedTTY) {
t.Fatalf("tt.TTY got %v, want %v", opts.TTY, tt.expectedTTY)
}
})
}
}
6 changes: 5 additions & 1 deletion cmd/oras/root/blob/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,11 @@ Example - Fetch and print a blob from OCI image layout archive file 'layout.tar'
return errors.New("`--output -` cannot be used with `--descriptor` at the same time")
}
opts.RawReference = args[0]
return option.Parse(&opts)
err := option.Parse(&opts)
if err == nil {
opts.UpdateTTY(cmd.Flags().Changed(option.NoTTYFlag), opts.outputPath == "-")
}
return err
},
Aliases: []string{"get"},
RunE: func(cmd *cobra.Command, args []string) error {
Expand Down

0 comments on commit ec23755

Please sign in to comment.