From e309b767602f41bb4910568f9b4f3ffa207cdd67 Mon Sep 17 00:00:00 2001 From: Leandro Dorileo Date: Mon, 1 Oct 2018 17:09:53 -0700 Subject: [PATCH] mass installer: print progress only if not piped With this we print only the new step getting started - not printing the actuall progress - if the process is piped/redirected. Signed-off-by: Leandro Dorileo --- massinstall/massinstall.go | 34 ++++++++++++++++++++++++++++++++++ utils/utils.go | 13 +++++++++++++ 2 files changed, 47 insertions(+) diff --git a/massinstall/massinstall.go b/massinstall/massinstall.go index 4fccebf..73af37e 100644 --- a/massinstall/massinstall.go +++ b/massinstall/massinstall.go @@ -15,6 +15,7 @@ import ( "github.com/clearlinux/clr-installer/log" "github.com/clearlinux/clr-installer/model" "github.com/clearlinux/clr-installer/progress" + "github.com/clearlinux/clr-installer/utils" ) // MassInstall is the frontend implementation for the "mass installer" it also @@ -22,6 +23,7 @@ import ( type MassInstall struct { prgDesc string prgIndex int + step int } // New creates a new instance of MassInstall frontend implementation @@ -29,8 +31,26 @@ func New() *MassInstall { return &MassInstall{} } +func printPipedStatus(mi *MassInstall) bool { + isStdoutTTY := utils.IsStdoutTTY() + mi.step++ + + if !isStdoutTTY && mi.step == 1 { + fmt.Println(mi.prgDesc) + return true + } else if !isStdoutTTY { + return true + } + + return false +} + // Step is the progress step implementation for progress.Client interface func (mi *MassInstall) Step() { + if printPipedStatus(mi) { + return + } + elms := []string{"|", "-", "\\", "|", "/", "-", "\\"} fmt.Printf("%s [%s]\r", mi.prgDesc, elms[mi.prgIndex]) @@ -57,6 +77,10 @@ func (mi *MassInstall) Desc(desc string) { // Partial is part of the progress.Client implementation and sets the progress bar based // on actual progression func (mi *MassInstall) Partial(total int, step int) { + if printPipedStatus(mi) { + return + } + line := fmt.Sprintf("%s %.0f%%\r", mi.prgDesc, (float64(step)/float64(total))*100) fmt.Printf("%s", line) } @@ -64,6 +88,11 @@ func (mi *MassInstall) Partial(total int, step int) { // Success is part of the progress.Client implementation and represents the // successful progress completion of a task func (mi *MassInstall) Success() { + if !utils.IsStdoutTTY() { + mi.step = 0 + return + } + mi.prgIndex = 0 fmt.Printf("%s [success]\n", mi.prgDesc) } @@ -71,6 +100,11 @@ func (mi *MassInstall) Success() { // Failure is part of the progress.Client implementation and represents the // unsuccessful progress completion of a task func (mi *MassInstall) Failure() { + if !utils.IsStdoutTTY() { + mi.step = 0 + return + } + mi.prgIndex = 0 fmt.Printf("%s [*failed*]\n", mi.prgDesc) } diff --git a/utils/utils.go b/utils/utils.go index 0d8fe32..3b1cd47 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -11,6 +11,8 @@ import ( "path" "path/filepath" "runtime" + "syscall" + "unsafe" "github.com/clearlinux/clr-installer/errors" ) @@ -139,3 +141,14 @@ func StringSliceContains(sl []string, str string) bool { func IsCheckCoverage() bool { return os.Getenv("CHECK_COVERAGE") != "" } + +// IsStdoutTTY returns true if the stdout is attached to a tty +func IsStdoutTTY() bool { + var termios syscall.Termios + + fd := os.Stdout.Fd() + ptr := uintptr(unsafe.Pointer(&termios)) + _, _, err := syscall.Syscall6(syscall.SYS_IOCTL, fd, syscall.TCGETS, ptr, 0, 0, 0) + + return err == 0 +}