Skip to content

Commit

Permalink
Set commands globally to verbose if log level >= Debug
Browse files Browse the repository at this point in the history
We now use an atomic bool abstraction to be able to set commands into
verbose mode globally. This allows us to make commands automatically
verbose if the log level is higher than `Debug`.

Signed-off-by: Sascha Grunert <sgrunert@suse.com>
  • Loading branch information
saschagrunert committed Jul 6, 2020
1 parent 87fc05d commit 9980e14
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 5 deletions.
10 changes: 8 additions & 2 deletions pkg/command/BUILD.bazel
Expand Up @@ -2,7 +2,10 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")

go_library(
name = "go_default_library",
srcs = ["command.go"],
srcs = [
"command.go",
"global.go",
],
importpath = "k8s.io/release/pkg/command",
visibility = ["//visibility:public"],
deps = [
Expand All @@ -13,7 +16,10 @@ go_library(

go_test(
name = "go_default_test",
srcs = ["command_test.go"],
srcs = [
"command_test.go",
"global_test.go",
],
embed = [":go_default_library"],
deps = ["@com_github_stretchr_testify//require:go_default_library"],
)
Expand Down
8 changes: 7 additions & 1 deletion pkg/command/command.go
Expand Up @@ -103,6 +103,12 @@ func (c *Command) Verbose() *Command {
return c
}

// isVerbose returns true if the command is in verbose mode, either set locally
// or global
func (c *Command) isVerbose() bool {
return GetGlobalVerbose() || c.verbose
}

// Add a command with the same working directory as well as verbosity mode.
// Returns a new Commands instance.
func (c *Command) Add(cmd string, args ...string) Commands {
Expand Down Expand Up @@ -254,7 +260,7 @@ func (c *Command) run(printOutput bool) (res *Status, err error) {
}()
}

if c.verbose {
if c.isVerbose() {
fmt.Fprintf(stdOutWriter, "+ %s\n", c.String())
}

Expand Down
37 changes: 37 additions & 0 deletions pkg/command/global.go
@@ -0,0 +1,37 @@
/*
Copyright 2020 The Kubernetes Authors.
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 command

import (
"sync/atomic"
)

var atomicInt int32

// SetGlobalVerbose sets the global command verbosity to the specified value
func SetGlobalVerbose(to bool) {
var i int32 = 0
if to {
i = 1
}
atomic.StoreInt32(&atomicInt, i)
}

// GetGlobalVerbose returns the globally set command verbosity
func GetGlobalVerbose() bool {
return atomic.LoadInt32(&atomicInt) != 0
}
29 changes: 29 additions & 0 deletions pkg/command/global_test.go
@@ -0,0 +1,29 @@
/*
Copyright 2020 The Kubernetes Authors.
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 command

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestSetGlobalVerboseSuccess(t *testing.T) {
require.False(t, GetGlobalVerbose())
SetGlobalVerbose(true)
require.True(t, GetGlobalVerbose())
}
6 changes: 5 additions & 1 deletion pkg/log/BUILD.bazel
Expand Up @@ -8,7 +8,11 @@ go_library(
],
importpath = "k8s.io/release/pkg/log",
visibility = ["//visibility:public"],
deps = ["@com_github_sirupsen_logrus//:go_default_library"],
deps = [
"//pkg/command:go_default_library",
"@com_github_pkg_errors//:go_default_library",
"@com_github_sirupsen_logrus//:go_default_library",
],
)

filegroup(
Expand Down
9 changes: 8 additions & 1 deletion pkg/log/log.go
Expand Up @@ -20,7 +20,10 @@ import (
"io/ioutil"
"strings"

"github.com/pkg/errors"
"github.com/sirupsen/logrus"

"k8s.io/release/pkg/command"
)

const (
Expand All @@ -32,9 +35,13 @@ func SetupGlobalLogger(level string) error {
logrus.SetFormatter(&logrus.TextFormatter{DisableTimestamp: true})
lvl, err := logrus.ParseLevel(level)
if err != nil {
return err
return errors.Wrapf(err, "setting log level to %s", level)
}
logrus.SetLevel(lvl)
if lvl >= logrus.DebugLevel {
logrus.Debug("Setting commands globally into verbose mode")
command.SetGlobalVerbose(true)
}
logrus.AddHook(NewFilenameHook())
logrus.Debugf("Using log level %q", lvl)
return nil
Expand Down

0 comments on commit 9980e14

Please sign in to comment.