Skip to content

Commit

Permalink
refactored
Browse files Browse the repository at this point in the history
Signed-off-by: Xiaoxuan Wang <wangxiaoxuan119@gmail.com>
  • Loading branch information
wangxiaoxuan273 committed Mar 14, 2024
1 parent bae834a commit a090126
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
17 changes: 13 additions & 4 deletions cmd/oras/internal/option/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ type Common struct {
Verbose bool
TTY *os.File

NoTTY bool
noTTY bool
}

// ApplyFlags applies flags to a command flag set.
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, NoTTYFlag, "", 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 @@ -56,12 +56,21 @@ func (opts *Common) Parse() error {

// parseTTY gets target options from user input.
func (opts *Common) parseTTY(f *os.File) error {
if !opts.NoTTY {
if !opts.noTTY {
if opts.Debug {
opts.NoTTY = true
opts.noTTY = true
} else if term.IsTerminal(int(f.Fd())) {
opts.TTY = f
}
}
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 toSTDOUT && !ttyEnforced {
opts.TTY = nil
}
}
2 changes: 1 addition & 1 deletion cmd/oras/internal/option/common_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func TestCommon_parseTTY(t *testing.T) {
if err := opts.parseTTY(device); err != nil {
t.Errorf("unexpected error with --debug: %v", err)
}
if !opts.NoTTY {
if !opts.noTTY {
t.Errorf("expected --no-tty to be true with --debug")
}
}
7 changes: 1 addition & 6 deletions cmd/oras/root/blob/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,7 @@ Example - Fetch and print a blob from OCI image layout archive file 'layout.tar'
opts.RawReference = args[0]
err := option.Parse(&opts)
if err == nil {
// check if `--no-tty` is explicitly set false. ttyEnforced = true if the
// user puts `--no-tty=false`
ttyEnforced := cmd.Flags().Changed(option.NoTTYFlag) && !opts.NoTTY
if opts.outputPath == "-" && !ttyEnforced {
opts.TTY = nil
}
opts.UpdateTTY(cmd.Flags().Changed(option.NoTTYFlag), opts.outputPath == "-")
}
return err
},
Expand Down

0 comments on commit a090126

Please sign in to comment.