From 7d40b4519a92ac193958089bdd6136c1a4e126f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20M=C3=ADchal?= Date: Sat, 3 Jul 2021 12:32:01 +0200 Subject: [PATCH] cmd: Add function to determine if a spinner can be started The logic for determining if a spinner can be started is non-trivial and hard to replicate. https://github.com/containers/toolbox/pull/826 --- src/cmd/create.go | 9 ++------- src/cmd/utils.go | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 7 deletions(-) create mode 100644 src/cmd/utils.go diff --git a/src/cmd/create.go b/src/cmd/create.go index 87c721748..da822eb53 100644 --- a/src/cmd/create.go +++ b/src/cmd/create.go @@ -31,7 +31,6 @@ import ( "github.com/godbus/dbus/v5" "github.com/sirupsen/logrus" "github.com/spf13/cobra" - "golang.org/x/crypto/ssh/terminal" ) const ( @@ -432,9 +431,7 @@ func createContainer(container, image, release string, showCommandToEnter bool) s := spinner.New(spinner.CharSets[9], 500*time.Millisecond) - stdoutFd := os.Stdout.Fd() - stdoutFdInt := int(stdoutFd) - if logLevel := logrus.GetLevel(); logLevel < logrus.DebugLevel && terminal.IsTerminal(stdoutFdInt) { + if canStartSpinner() { s.Prefix = fmt.Sprintf("Creating container %s: ", container) s.Writer = os.Stdout s.Start() @@ -708,9 +705,7 @@ func pullImage(image, release string) (bool, error) { logrus.Debugf("Pulling image %s", imageFull) - stdoutFd := os.Stdout.Fd() - stdoutFdInt := int(stdoutFd) - if logLevel := logrus.GetLevel(); logLevel < logrus.DebugLevel && terminal.IsTerminal(stdoutFdInt) { + if canStartSpinner() { s := spinner.New(spinner.CharSets[9], 500*time.Millisecond) s.Prefix = fmt.Sprintf("Pulling %s: ", imageFull) s.Writer = os.Stdout diff --git a/src/cmd/utils.go b/src/cmd/utils.go new file mode 100644 index 000000000..06eb4fe24 --- /dev/null +++ b/src/cmd/utils.go @@ -0,0 +1,33 @@ +/* + * Copyright © 2019 – 2021 Red Hat Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package cmd + +import ( + "os" + + "github.com/sirupsen/logrus" + "golang.org/x/crypto/ssh/terminal" +) + +func canStartSpinner() bool { + stdoutFd := os.Stdout.Fd() + stdoutFdInt := int(stdoutFd) + + logLevel := logrus.GetLevel() + + return logLevel < logrus.DebugLevel && terminal.IsTerminal(stdoutFdInt) +}