From 2b68ad09b25dfa2d6566cc4545d47415010cd881 Mon Sep 17 00:00:00 2001 From: Craig Gumbley Date: Fri, 14 Oct 2022 07:05:40 +0100 Subject: [PATCH] Environment detection This commit adds the environment package. It currently contains one function that is used to determine whether or not the app is running in CI. For most popular CI products this assertion is made by including an environment variable called `CI`. IsCI checks for the existence of this environment variable. If it is set to true then we return a boolean true. --- internal/environment/environment.go | 11 +++++++++++ internal/environment/environment_test.go | 17 +++++++++++++++++ internal/logging/logger.go | 7 +++++++ 3 files changed, 35 insertions(+) create mode 100644 internal/environment/environment.go create mode 100644 internal/environment/environment_test.go diff --git a/internal/environment/environment.go b/internal/environment/environment.go new file mode 100644 index 0000000..6541354 --- /dev/null +++ b/internal/environment/environment.go @@ -0,0 +1,11 @@ +// Package environment provides helper methods for working with the +// current environment. +package environment + +import "os" + +// IsCI returns true if the CI environment variable is set to true. +// This is used for most CI systems. +func IsCI() bool { + return os.Getenv("CI") == "true" +} diff --git a/internal/environment/environment_test.go b/internal/environment/environment_test.go new file mode 100644 index 0000000..621fc80 --- /dev/null +++ b/internal/environment/environment_test.go @@ -0,0 +1,17 @@ +package environment_test + +import ( + "os" + "testing" + + "github.com/chelnak/gh-changelog/internal/environment" + "github.com/stretchr/testify/assert" +) + +func TestIsCIReturnsTrueWhenRunningInCI(t *testing.T) { + _ = os.Setenv("CI", "true") + defer func() { + _ = os.Unsetenv("CI") + }() + assert.True(t, environment.IsCI()) +} diff --git a/internal/logging/logger.go b/internal/logging/logger.go index 77ad44d..23f8d4b 100644 --- a/internal/logging/logger.go +++ b/internal/logging/logger.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/chelnak/gh-changelog/internal/configuration" + "github.com/chelnak/gh-changelog/internal/environment" ) // LoggerType is an enum for the different types of logger @@ -41,6 +42,12 @@ func NewLogger(loggerType LoggerType) Logger { func GetLoggerType(name string) (LoggerType, error) { if name == "" { name = configuration.Config.Logger + + // If we're running in a CI environment then we don't want to + // use the spinner. + if environment.IsCI() { + name = "text" + } } var loggerType LoggerType