Skip to content

Commit

Permalink
Environment detection
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
chelnak committed Oct 14, 2022
1 parent c940d82 commit 2b68ad0
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
11 changes: 11 additions & 0 deletions internal/environment/environment.go
Original file line number Diff line number Diff line change
@@ -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"
}
17 changes: 17 additions & 0 deletions internal/environment/environment_test.go
Original file line number Diff line number Diff line change
@@ -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())
}
7 changes: 7 additions & 0 deletions internal/logging/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 2b68ad0

Please sign in to comment.