Skip to content

Commit 8296a0e

Browse files
gbraadopenshift-merge-robot
authored andcommitted
Fixes #3546 #3550 always show progress during uncompression of the bundle
As part of `crc setup` we show the progress of the download and uncompression. However, the download progress is always shown, while the uncompression is suppressed when this is not a standard terninal. For consistency we remove the suppression and add additional complexity to allow this to be forced.
1 parent bdda869 commit 8296a0e

File tree

10 files changed

+51
-23
lines changed

10 files changed

+51
-23
lines changed

cmd/crc/cmd/setup.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,22 @@ import (
1111
"github.com/crc-org/crc/pkg/crc/input"
1212
"github.com/crc-org/crc/pkg/crc/preflight"
1313
"github.com/crc-org/crc/pkg/crc/validation"
14+
crcTerminal "github.com/crc-org/crc/pkg/os/terminal"
1415

1516
"github.com/spf13/cobra"
1617
"k8s.io/client-go/util/exec"
1718
)
1819

1920
var (
20-
checkOnly bool
21+
checkOnly bool
22+
forceShowProgressbars bool
2123
)
2224

2325
func init() {
2426
setupCmd.Flags().Bool(crcConfig.ExperimentalFeatures, false, "Allow the use of experimental features")
2527
setupCmd.Flags().StringP(crcConfig.Bundle, "b", constants.GetDefaultBundlePath(crcConfig.GetPreset(config)), "Bundle to use for instance")
2628
setupCmd.Flags().BoolVar(&checkOnly, "check-only", false, "Only run the preflight checks, don't try to fix any misconfiguration")
29+
setupCmd.Flags().BoolVar(&forceShowProgressbars, "show-progressbars", false, "Always show the progress bars for download and extraction")
2730
addOutputFormatFlag(setupCmd)
2831
rootCmd.AddCommand(setupCmd)
2932
}
@@ -63,6 +66,8 @@ func runSetup(arguments []string) error {
6366
}
6467
}
6568

69+
// set global variable to force terminal output
70+
crcTerminal.ForceShowOutput = forceShowProgressbars
6671
err := preflight.SetupHost(config, checkOnly)
6772
if err != nil && checkOnly {
6873
err = exec.CodeExitError{

pkg/crc/cache/cache.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ func (c *Cache) cacheExecutable() error {
120120
// Check the file is tarball or not
121121
if isTarball(assetTmpFile) {
122122
// Extract the tarball and put it the cache directory.
123-
extractedFiles, err = extract.UncompressWithFilter(assetTmpFile, tmpDir, false,
123+
extractedFiles, err = extract.UncompressWithFilter(assetTmpFile, tmpDir,
124124
func(filename string) bool { return filepath.Base(filename) == c.GetExecutableName() })
125125
if err != nil {
126126
return errors.Wrapf(err, "Cannot uncompress '%s'", assetTmpFile)

pkg/crc/cluster/pullsecret.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
"github.com/crc-org/crc/pkg/crc/logging"
1616
"github.com/crc-org/crc/pkg/crc/preset"
1717
"github.com/crc-org/crc/pkg/crc/validation"
18-
crcos "github.com/crc-org/crc/pkg/os"
18+
crcTerminal "github.com/crc-org/crc/pkg/os/terminal"
1919

2020
"github.com/AlecAivazis/survey/v2"
2121
"github.com/zalando/go-keyring"
@@ -171,7 +171,7 @@ You can copy it from the Pull Secret section of %s.
171171
// promptUserForSecret can be used for any kind of secret like image pull
172172
// secret or for password.
173173
func promptUserForSecret() (string, error) {
174-
if !crcos.RunningInTerminal() {
174+
if !crcTerminal.IsRunningInTerminal() {
175175
return "", errors.New("cannot ask for secret, crc not launched by a terminal")
176176
}
177177

pkg/crc/input/input.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ import (
44
"fmt"
55
"strings"
66

7-
crcos "github.com/crc-org/crc/pkg/os"
7+
crcTerminal "github.com/crc-org/crc/pkg/os/terminal"
88
)
99

1010
func PromptUserForYesOrNo(message string, force bool) bool {
1111
if force {
1212
return true
1313
}
14-
if !crcos.RunningInTerminal() {
14+
if !crcTerminal.IsRunningInTerminal() {
1515
return false
1616
}
1717
var response string

pkg/crc/segment/segment.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"github.com/crc-org/crc/pkg/crc/telemetry"
2323
"github.com/crc-org/crc/pkg/crc/version"
2424
crcos "github.com/crc-org/crc/pkg/os"
25+
crcTerminal "github.com/crc-org/crc/pkg/os/terminal"
2526
"github.com/pborman/uuid"
2627
"github.com/segmentio/analytics-go/v3"
2728
"github.com/spf13/cast"
@@ -197,7 +198,7 @@ func properties(ctx context.Context, err error, duration time.Duration) analytic
197198
properties = properties.
198199
Set("success", err == nil).
199200
Set("duration", duration.Milliseconds()).
200-
Set("tty", crcos.RunningInTerminal()).
201+
Set("tty", crcTerminal.IsRunningInTerminal()).
201202
Set("remote", crcos.RunningUsingSSH())
202203
if err != nil {
203204
properties = properties.Set("error", telemetry.SetError(err)).

pkg/download/download.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
"github.com/crc-org/crc/pkg/crc/logging"
1313
"github.com/crc-org/crc/pkg/crc/network"
14+
"github.com/crc-org/crc/pkg/os/terminal"
1415

1516
"github.com/cavaliergopher/grab/v3"
1617
"github.com/cheggaaa/pb/v3"
@@ -28,15 +29,20 @@ func doRequest(client *grab.Client, req *grab.Request) (string, error) {
2829

2930
t := time.NewTicker(500 * time.Millisecond)
3031
defer t.Stop()
31-
bar := pb.Start64(resp.Size())
32-
bar.Set(pb.Bytes, true)
33-
defer bar.Finish()
32+
var bar *pb.ProgressBar
33+
if terminal.IsShowTerminalOutput() {
34+
bar = pb.Start64(resp.Size())
35+
bar.Set(pb.Bytes, true)
36+
defer bar.Finish()
37+
}
3438

3539
loop:
3640
for {
3741
select {
3842
case <-t.C:
39-
bar.SetCurrent(resp.BytesComplete())
43+
if terminal.IsShowTerminalOutput() {
44+
bar.SetCurrent(resp.BytesComplete())
45+
}
4046
case <-resp.Done:
4147
break loop
4248
}

pkg/extract/extract.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,29 @@ import (
1313
"github.com/cheggaaa/pb/v3"
1414
"github.com/crc-org/crc/pkg/crc/logging"
1515
crcos "github.com/crc-org/crc/pkg/os"
16+
"github.com/crc-org/crc/pkg/os/terminal"
17+
1618
"github.com/h2non/filetype"
1719
"github.com/klauspost/compress/zstd"
1820
"github.com/pkg/errors"
1921
"github.com/xi2/xz"
20-
terminal "golang.org/x/term"
2122
)
2223

2324
const minSizeForProgressBar = 100_000_000
2425

25-
func UncompressWithFilter(tarball, targetDir string, showProgress bool, fileFilter func(string) bool) ([]string, error) {
26-
return uncompress(tarball, targetDir, fileFilter, showProgress && terminal.IsTerminal(int(os.Stdout.Fd())))
26+
func UncompressWithFilter(tarball, targetDir string, fileFilter func(string) bool) ([]string, error) {
27+
return uncompress(tarball, targetDir, fileFilter, false) // never show detailed output
2728
}
2829

2930
func Uncompress(tarball, targetDir string, showProgress bool) ([]string, error) {
30-
return uncompress(tarball, targetDir, nil, showProgress && terminal.IsTerminal(int(os.Stdout.Fd())))
31+
return uncompress(tarball, targetDir, nil, terminal.IsShowTerminalOutput())
3132
}
3233

3334
func uncompress(tarball, targetDir string, fileFilter func(string) bool, showProgress bool) ([]string, error) {
3435
logging.Debugf("Uncompressing %s to %s", tarball, targetDir)
3536

3637
if strings.HasSuffix(tarball, ".zip") {
37-
return unzip(tarball, targetDir, fileFilter, showProgress)
38+
return unzip(tarball, targetDir, fileFilter, terminal.IsShowTerminalOutput())
3839
}
3940

4041
file, err := os.Open(filepath.Clean(tarball))

pkg/extract/extract_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ func testUncompress(t *testing.T, archiveName string, fileFilter func(string) bo
132132
var fileList []string
133133
var err error
134134
if fileFilter != nil {
135-
fileList, err = UncompressWithFilter(archiveName, destDir, false, fileFilter)
135+
fileList, err = UncompressWithFilter(archiveName, destDir, fileFilter)
136136
} else {
137137
fileList, err = Uncompress(archiveName, destDir, false)
138138
}

pkg/os/terminal/terminal.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package terminal
2+
3+
import (
4+
"os"
5+
6+
xterminal "golang.org/x/term"
7+
)
8+
9+
var (
10+
// Global variable to force output regardless if terninal
11+
ForceShowOutput = false
12+
)
13+
14+
func IsShowTerminalOutput() bool {
15+
// if this is a terminal or set to force output
16+
return IsRunningInTerminal() || ForceShowOutput
17+
}
18+
19+
func IsRunningInTerminal() bool {
20+
return xterminal.IsTerminal(int(os.Stdout.Fd()))
21+
}

pkg/os/util.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ import (
99
"strings"
1010

1111
"github.com/crc-org/crc/pkg/crc/logging"
12-
13-
terminal "golang.org/x/term"
1412
)
1513

1614
// ReplaceOrAddEnv changes the value of an environment variable if it exists otherwise add the new variable
@@ -112,10 +110,6 @@ func RemoveFileIfExists(path string) error {
112110
return nil
113111
}
114112

115-
func RunningInTerminal() bool {
116-
return terminal.IsTerminal(int(os.Stdin.Fd()))
117-
}
118-
119113
func RunningUsingSSH() bool {
120114
return os.Getenv("SSH_TTY") != ""
121115
}

0 commit comments

Comments
 (0)