Skip to content

Commit

Permalink
fix: only prompt to confirm extension-less output file when stdout is…
Browse files Browse the repository at this point in the history
… the terminal
  • Loading branch information
jesteria committed Nov 27, 2024
1 parent 2d93f71 commit e7cd84f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
13 changes: 5 additions & 8 deletions internal/config/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
var flog = log.New(os.Stderr, "", log.LstdFlags)

func checkNakedOutPath() error {
if Force || OutPath == "-" || osUtil.PathDirectoryLike(OutPath) || filepath.Ext(OutPath) != "" {
if Force || OutPath == "-" || osUtil.PathDirectoryLike(OutPath) || filepath.Ext(OutPath) != "" || !term.IsTerm() {
return nil
}
core := errors.New("archive destination is ambiguous")
Expand All @@ -29,13 +29,10 @@ func checkNakedOutPath() error {
}

func checkTerminalOutput() error {
if !Force && OutPath == "-" {
// we're going to write to stdout --
// check that it's not the terminal
fileInfo, _ := os.Stdout.Stat()
if (fileInfo.Mode() & os.ModeCharDevice) == os.ModeCharDevice {
return errors.New("archive destination is character device (terminal)")
}
// if we're going to write to stdout,
// check that it's not the terminal
if !Force && OutPath == "-" && term.IsTerm() {
return errors.New("archive destination is character device (terminal)")
}
return nil
}
Expand Down
7 changes: 7 additions & 0 deletions internal/util/term/term.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,10 @@ func Confirm(prompt string) {
}
}
}

// IsTerm: whether stdout is attached to a character device (terminal)
func IsTerm() bool {
fileInfo, _ := os.Stdout.Stat()
fileMask := fileInfo.Mode() & os.ModeCharDevice
return fileMask == os.ModeCharDevice
}

0 comments on commit e7cd84f

Please sign in to comment.