From f0a962d6dde3a96e06970ba5f37de58efa5ed26f Mon Sep 17 00:00:00 2001 From: Oleksandr Redko Date: Fri, 24 Feb 2023 17:03:03 +0200 Subject: [PATCH] dev: NewBuffer/NewBufferString to shorten init --- pkg/commands/executor.go | 6 ++---- pkg/golinters/gofumpt.go | 8 ++------ scripts/expand_website_templates/main.go | 3 +-- 3 files changed, 5 insertions(+), 12 deletions(-) diff --git a/pkg/commands/executor.go b/pkg/commands/executor.go index 4f5a9e306246..347a0d19486b 100644 --- a/pkg/commands/executor.go +++ b/pkg/commands/executor.go @@ -157,8 +157,7 @@ func (e *Executor) initHashSalt(version string) error { return fmt.Errorf("failed to calculate config salt: %w", err) } - var b bytes.Buffer - b.Write(binSalt) + b := bytes.NewBuffer(binSalt) b.Write(configSalt) cache.SetSalt(b.Bytes()) return nil @@ -198,8 +197,7 @@ func computeConfigSalt(cfg *config.Config) ([]byte, error) { return nil, fmt.Errorf("failed to json marshal config linter settings: %w", err) } - var configData bytes.Buffer - configData.WriteString("linters-settings=") + configData := bytes.NewBufferString("linters-settings=") configData.Write(lintersSettingsBytes) configData.WriteString("\nbuild-tags=%s" + strings.Join(cfg.Run.BuildTags, ",")) diff --git a/pkg/golinters/gofumpt.go b/pkg/golinters/gofumpt.go index c080bb726d3a..276dab5f27e4 100644 --- a/pkg/golinters/gofumpt.go +++ b/pkg/golinters/gofumpt.go @@ -88,13 +88,9 @@ func runGofumpt(lintCtx *linter.Context, pass *analysis.Pass, diff differ, optio } if !bytes.Equal(input, output) { - out := bytes.Buffer{} - _, err = out.WriteString(fmt.Sprintf("--- %[1]s\n+++ %[1]s\n", f)) - if err != nil { - return nil, fmt.Errorf("error while running gofumpt: %w", err) - } + out := bytes.NewBufferString(fmt.Sprintf("--- %[1]s\n+++ %[1]s\n", f)) - err = diff.Diff(&out, bytes.NewReader(input), bytes.NewReader(output)) + err := diff.Diff(out, bytes.NewReader(input), bytes.NewReader(output)) if err != nil { return nil, fmt.Errorf("error while running gofumpt: %w", err) } diff --git a/scripts/expand_website_templates/main.go b/scripts/expand_website_templates/main.go index a9d39ddec441..1d49885fc4c0 100644 --- a/scripts/expand_website_templates/main.go +++ b/scripts/expand_website_templates/main.go @@ -64,8 +64,7 @@ func updateStateFile(replacements map[string]string) error { return err } - var contentBuf bytes.Buffer - contentBuf.WriteString("This file stores hash of website templates to trigger " + + contentBuf := bytes.NewBufferString("This file stores hash of website templates to trigger " + "Netlify rebuild when something changes, e.g. new linter is added.\n") contentBuf.WriteString(hex.EncodeToString(h.Sum(nil)))