Skip to content

Commit 9e3b71e

Browse files
committed
switch logging to slog
1 parent 3d740c5 commit 9e3b71e

21 files changed

+152
-55
lines changed

cli/boilerplate_cli.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ func CreateBoilerplateCli() *cli.App {
9696
Name: options.OptDisableDependencyPrompt,
9797
Usage: fmt.Sprintf("Do not prompt for confirmation to include dependencies. Has the same effect as --%s, without disabling variable prompts.", options.OptNonInteractive),
9898
},
99+
&cli.BoolFlag{
100+
Name: options.OptSilent,
101+
Usage: "Do not output any log messages",
102+
},
99103
}
100104

101105
// We pass JSON/YAML content to various CLI flags, such as --var, and this JSON/YAML content may contain commas or

config/config.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ func (config *BoilerplateConfig) GetVariablesMap() map[string]variables.Variable
4141

4242
// Implement the go-yaml unmarshal interface for BoilerplateConfig. We can't let go-yaml handle this itself because:
4343
//
44-
// 1. Variable is an interface
45-
// 2. We need to provide Defaults for optional fields, such as "type"
46-
// 3. We want to validate the variable as part of the unmarshalling process so we never have invalid Variable or
47-
// Dependency classes floating around
44+
// 1. Variable is an interface
45+
// 2. We need to provide Defaults for optional fields, such as "type"
46+
// 3. We want to validate the variable as part of the unmarshalling process so we never have invalid Variable or
47+
// Dependency classes floating around
4848
func (config *BoilerplateConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
4949
var fields map[string]interface{}
5050
if err := unmarshal(&fields); err != nil {
@@ -176,15 +176,15 @@ func LoadBoilerplateConfig(opts *options.BoilerplateOptions) (*BoilerplateConfig
176176
configPath := BoilerplateConfigPath(opts.TemplateFolder)
177177

178178
if util.PathExists(configPath) {
179-
util.Logger.Printf("Loading boilerplate config from %s", configPath)
179+
opts.Logger.Info(fmt.Sprintf("Loading boilerplate config from %s", configPath))
180180
bytes, err := ioutil.ReadFile(configPath)
181181
if err != nil {
182182
return nil, errors.WithStackTrace(err)
183183
}
184184

185185
return ParseBoilerplateConfig(bytes)
186186
} else if opts.OnMissingConfig == options.Ignore {
187-
util.Logger.Printf("Warning: boilerplate config file not found at %s. The %s flag is set, so ignoring. Note that no variables will be available while generating.", configPath, options.OptMissingConfigAction)
187+
opts.Logger.Info(fmt.Sprintf("Warning: boilerplate config file not found at %s. The %s flag is set, so ignoring. Note that no variables will be available while generating.", configPath, options.OptMissingConfigAction))
188188
return &BoilerplateConfig{}, nil
189189
} else {
190190
// If the template URL is similar to a git URL, surface in error message that there may be a misspelling/typo.

config/config_test.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package config
22

33
import (
44
"bytes"
5+
"io"
56
"io/ioutil"
7+
"log/slog"
68
"path"
79
"path/filepath"
810
"reflect"
@@ -620,7 +622,10 @@ func TestParseBoilerplateConfigMultipleHooks(t *testing.T) {
620622
func TestLoadBoilerplateConfigFullConfig(t *testing.T) {
621623
t.Parallel()
622624

623-
actual, err := LoadBoilerplateConfig(&options.BoilerplateOptions{TemplateFolder: "../test-fixtures/config-test/full-config"})
625+
actual, err := LoadBoilerplateConfig(&options.BoilerplateOptions{
626+
Logger: slog.New(slog.NewJSONHandler(io.Discard, nil)),
627+
TemplateFolder: "../test-fixtures/config-test/full-config",
628+
})
624629
expected := &BoilerplateConfig{
625630
Partials: []string{"../templates/foo"},
626631
Variables: []variables.Variable{
@@ -664,7 +669,10 @@ func TestLoadBoilerplateConfigNoConfigIgnore(t *testing.T) {
664669
t.Parallel()
665670

666671
templateFolder := "../test-fixtures/config-test/no-config"
667-
actual, err := LoadBoilerplateConfig(&options.BoilerplateOptions{TemplateFolder: templateFolder, OnMissingConfig: options.Ignore})
672+
actual, err := LoadBoilerplateConfig(&options.BoilerplateOptions{
673+
Logger: slog.New(slog.NewJSONHandler(io.Discard, nil)),
674+
TemplateFolder: templateFolder, OnMissingConfig: options.Ignore,
675+
})
668676
expected := &BoilerplateConfig{}
669677

670678
assert.Nil(t, err, "Unexpected error: %v", err)
@@ -674,7 +682,10 @@ func TestLoadBoilerplateConfigNoConfigIgnore(t *testing.T) {
674682
func TestLoadBoilerplateConfigInvalidConfig(t *testing.T) {
675683
t.Parallel()
676684

677-
_, err := LoadBoilerplateConfig(&options.BoilerplateOptions{TemplateFolder: "../test-fixtures/config-test/invalid-config"})
685+
_, err := LoadBoilerplateConfig(&options.BoilerplateOptions{
686+
Logger: slog.New(slog.NewJSONHandler(io.Discard, nil)),
687+
TemplateFolder: "../test-fixtures/config-test/invalid-config",
688+
})
678689

679690
assert.NotNil(t, err)
680691

config/get_variables.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,10 @@ func getVariable(variable variables.Variable, opts *options.BoilerplateOptions)
170170
valueFromVars, valueSpecifiedInVars := getVariableFromVars(variable, opts)
171171

172172
if valueSpecifiedInVars {
173-
util.Logger.Printf("Using value specified via command line options for variable '%s': %s", variable.FullName(), valueFromVars)
173+
opts.Logger.Info(fmt.Sprintf("Using value specified via command line options for variable '%s': %s", variable.FullName(), valueFromVars))
174174
return valueFromVars, nil
175175
} else if opts.NonInteractive && variable.Default() != nil {
176-
util.Logger.Printf("Using default value for variable '%s': %v", variable.FullName(), variable.Default())
176+
opts.Logger.Info(fmt.Sprintf("Using default value for variable '%s': %v", variable.FullName(), variable.Default()))
177177
return variable.Default(), nil
178178
} else if opts.NonInteractive {
179179
return nil, errors.WithStackTrace(MissingVariableWithNonInteractiveMode(variable.FullName()))
@@ -269,7 +269,7 @@ func getVariableFromUser(variable variables.Variable, opts *options.BoilerplateO
269269

270270
if value == "" {
271271
// TODO: what if the user wanted an empty string instead of the default?
272-
util.Logger.Printf("Using default value for variable '%s': %v", variable.FullName(), variable.Default())
272+
opts.Logger.Info(fmt.Sprintf("Using default value for variable '%s': %v", variable.FullName(), variable.Default()))
273273
return variable.Default(), nil
274274
}
275275

config/get_variables_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package config
22

33
import (
4+
"io"
5+
"log/slog"
46
"reflect"
57
"testing"
68

@@ -15,7 +17,7 @@ func TestGetVariableFromVarsEmptyVars(t *testing.T) {
1517
t.Parallel()
1618

1719
variable := variables.NewStringVariable("foo")
18-
opts := &options.BoilerplateOptions{}
20+
opts := &options.BoilerplateOptions{Logger: slog.New(slog.NewJSONHandler(io.Discard, nil))}
1921

2022
_, containsValue := getVariableFromVars(variable, opts)
2123
assert.False(t, containsValue)
@@ -108,6 +110,7 @@ func TestGetVariableInVarsNonInteractive(t *testing.T) {
108110

109111
variable := variables.NewStringVariable("foo")
110112
opts := &options.BoilerplateOptions{
113+
Logger: slog.New(slog.NewJSONHandler(io.Discard, nil)),
111114
NonInteractive: true,
112115
Vars: map[string]interface{}{
113116
"key1": "value1",
@@ -128,6 +131,7 @@ func TestGetVariableDefaultNonInteractive(t *testing.T) {
128131

129132
variable := variables.NewStringVariable("foo").WithDefault("bar")
130133
opts := &options.BoilerplateOptions{
134+
Logger: slog.New(slog.NewJSONHandler(io.Discard, nil)),
131135
NonInteractive: true,
132136
Vars: map[string]interface{}{
133137
"key1": "value1",
@@ -225,6 +229,7 @@ func TestGetVariablesMatchFromVarsAndDefaults(t *testing.T) {
225229
t.Parallel()
226230

227231
opts := &options.BoilerplateOptions{
232+
Logger: slog.New(slog.NewJSONHandler(io.Discard, nil)),
228233
NonInteractive: true,
229234
Vars: map[string]interface{}{
230235
"key1": "value1",

getter-helper/getter_helper.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package getter_helper
33
import (
44
"context"
55
"fmt"
6+
"log/slog"
67
"net/url"
78
"os"
89
"path/filepath"
@@ -12,7 +13,6 @@ import (
1213
urlhelper "github.com/hashicorp/go-getter/helper/url"
1314

1415
"github.com/gruntwork-io/boilerplate/errors"
15-
"github.com/gruntwork-io/boilerplate/util"
1616
)
1717

1818
var forcedRegexp = regexp.MustCompile(`^([A-Za-z0-9]+)::(.+)$`)
@@ -100,7 +100,7 @@ func NewGetterClient(src string, dst string) (*getter.Client, error) {
100100
// DownloadTemplatesToTemporaryFolder uses the go-getter library to fetch the templates from the configured URL to a
101101
// temporary folder and returns the path to that folder. If there is a subdir in the template URL, return the combined
102102
// path as well.
103-
func DownloadTemplatesToTemporaryFolder(templateUrl string) (string, string, error) {
103+
func DownloadTemplatesToTemporaryFolder(templateUrl string, logger *slog.Logger) (string, string, error) {
104104
workingDir, err := getTempFolder()
105105
if err != nil {
106106
return workingDir, workingDir, errors.WithStackTrace(err)
@@ -109,7 +109,7 @@ func DownloadTemplatesToTemporaryFolder(templateUrl string) (string, string, err
109109
// Always set a subdir path because go-getter can not clone into an existing dir.
110110
cloneDir := filepath.Join(workingDir, "wd")
111111

112-
util.Logger.Printf("Downloading templates from %s to %s", templateUrl, workingDir)
112+
logger.Info(fmt.Sprintf("Downloading templates from %s to %s", templateUrl, workingDir))
113113

114114
// If there is a subdir component, we download everything and combine the path at the end to return the working path
115115
mainPath, subDir := getter.SourceDirSubdir(templateUrl)

getter-helper/getter_helper_unix_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ package getter_helper
55

66
import (
77
"fmt"
8+
"io"
9+
"log/slog"
810
"os"
911
"path/filepath"
1012
"testing"
@@ -24,7 +26,7 @@ func TestDownloadTemplatesToTempDir(t *testing.T) {
2426

2527
branch := git.GetCurrentBranchName(t)
2628
templateUrl := fmt.Sprintf("git@github.com:gruntwork-io/boilerplate.git//examples/for-learning-and-testing/variables?ref=%s", branch)
27-
workingDir, workPath, err := DownloadTemplatesToTemporaryFolder(templateUrl)
29+
workingDir, workPath, err := DownloadTemplatesToTemporaryFolder(templateUrl, slog.New(slog.NewJSONHandler(io.Discard, nil)))
2830
defer os.RemoveAll(workingDir)
2931
require.NoError(t, err, errors.PrintErrorWithStackTrace(err))
3032

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/gruntwork-io/boilerplate
22

3-
go 1.18
3+
go 1.21
44

55
require (
66
github.com/AlecAivazis/survey/v2 v2.3.4

integration-tests/error_message_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ func TestMisspelledTemplateURLErrorMessage(t *testing.T) {
2828
"--output-folder",
2929
outputFolder,
3030
"--non-interactive",
31+
"--silent",
3132
}
3233
runErr := app.Run(args)
3334
assert.Error(t, runErr, errors.PrintErrorWithStackTrace(runErr))

integration-tests/examples_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ func testExample(t *testing.T, templateFolder string, outputFolder string, varFi
138138
"--non-interactive",
139139
"--missing-key-action",
140140
missingKeyAction,
141+
"--silent",
141142
}
142143

143144
// Special handling for the shell-disabled case, which we use to test that we can disable hooks and shell helpers

0 commit comments

Comments
 (0)