Skip to content

Commit

Permalink
(chore): Resolve lint errors
Browse files Browse the repository at this point in the history
Signed-off-by: schristoff <28318173+schristoff@users.noreply.github.com>
  • Loading branch information
schristoff committed Apr 30, 2024
1 parent 7422f37 commit 7d74683
Show file tree
Hide file tree
Showing 23 changed files with 257 additions and 98 deletions.
12 changes: 8 additions & 4 deletions mage/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@ func Docs() {
if baseURL != "" {
cmd.Args("-b", baseURL)
}
cmd.RunV()
err := cmd.RunV()
mgx.Must(err)
}

func removePreviewContainer() {
docker.RemoveContainer(PreviewContainer)
err := docker.RemoveContainer(PreviewContainer)
mgx.Must(err)
}

// Preview the website locally using a Docker container.
Expand All @@ -59,12 +61,13 @@ func DocsPreview() {
}
setDockerUser := fmt.Sprintf("--user=%s:%s", currentUser.Uid, currentUser.Gid)
pwd, _ := os.Getwd()
must.Run("docker", "run", "-d", "-v", pwd+":/src",
err = must.Run("docker", "run", "-d", "-v", pwd+":/src",
"-v", operatorDocs+":/src/docs/content/operator",
setDockerUser,
"-p", "1313:1313", "--name", PreviewContainer, "-w", "/src/docs",
"klakegg/hugo:0.78.1-ext-alpine", "server", "-D", "-F", "--noHTTPCache",
"--watch", "--bind=0.0.0.0")
mgx.Must(err)

for {
output, _ := must.OutputS("docker", "logs", "porter-docs")
Expand All @@ -74,7 +77,8 @@ func DocsPreview() {
time.Sleep(time.Second)
}

must.Run("open", "http://localhost:1313/docs/")
err = must.Run("open", "http://localhost:1313/docs/")
mgx.Must(err)
}

// Build a branch preview of the website.
Expand Down
14 changes: 8 additions & 6 deletions pkg/build/dockerfile-generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ func TestPorter_buildCustomDockerfile(t *testing.T) {
COPY mybin /cnab/app/
`
c.TestContext.AddTestFileContents([]byte(customFrom), "Dockerfile.template")

err = c.TestContext.AddTestFileContents([]byte(customFrom), "Dockerfile.template")
require.NoError(t, err)
mp := mixin.NewTestMixinProvider()
g := NewDockerfileGenerator(c.Config, m, tmpl, mp)
gotlines, err := g.buildDockerfile(context.Background())
Expand Down Expand Up @@ -94,8 +94,8 @@ COPY mybin /cnab/app/
COPY mybin /cnab/app/
`
c.TestContext.AddTestFileContents([]byte(customFrom), "Dockerfile.template")

err = c.TestContext.AddTestFileContents([]byte(customFrom), "Dockerfile.template")
require.NoError(t, err)
mp := mixin.NewTestMixinProvider()
g := NewDockerfileGenerator(c.Config, m, tmpl, mp)
gotlines, err := g.buildDockerfile(context.Background())
Expand Down Expand Up @@ -123,7 +123,8 @@ COPY mybin /cnab/app/
COPY mybin /cnab/app/
`
c.TestContext.AddTestFileContents([]byte(customFrom), "Dockerfile.template")
err = c.TestContext.AddTestFileContents([]byte(customFrom), "Dockerfile.template")
require.NoError(t, err)

mp := mixin.NewTestMixinProvider()
g := NewDockerfileGenerator(c.Config, m, tmpl, mp)
Expand Down Expand Up @@ -225,7 +226,8 @@ func TestPorter_appendBuildInstructionsIfMixinTokenIsNotPresent(t *testing.T) {
ARG BUNDLE_DIR
COPY mybin /cnab/app/
`
c.TestContext.AddTestFileContents([]byte(customFrom), "Dockerfile.template")
err = c.TestContext.AddTestFileContents([]byte(customFrom), "Dockerfile.template")
require.NoError(t, err)

mp := mixin.NewTestMixinProvider()
g := NewDockerfileGenerator(c.Config, m, tmpl, mp)
Expand Down
8 changes: 5 additions & 3 deletions pkg/cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,12 +266,14 @@ func TestCache_StoreBundle_Overwrite(t *testing.T) {
cb := CachedBundle{BundleReference: cnab.BundleReference{Reference: kahn1dot01}}
cb.SetCacheDir(cacheDir)
cfg.FileSystem.Create(cb.BuildManifestPath())
cfg.FileSystem.Create(cb.BuildRelocationFilePath())
_, err := cfg.FileSystem.Create(cb.BuildRelocationFilePath())
require.NoError(t, err)
junkPath := filepath.Join(cb.cacheDir, "junk.txt")
cfg.FileSystem.Create(junkPath)
_, err = cfg.FileSystem.Create(junkPath)
require.NoError(t, err)

// Refresh the cache
cb, err := c.StoreBundle(cb.BundleReference)
cb, err = c.StoreBundle(cb.BundleReference)
require.NoError(t, err, "StoreBundle failed")

exists, _ := cfg.FileSystem.Exists(cb.BuildBundlePath())
Expand Down
3 changes: 2 additions & 1 deletion pkg/cnab/config-adapter/stamp.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ func (c *ManifestConverter) GenerateStamp(ctx context.Context) (Stamp, error) {
if err != nil {
// The digest is only used to decide if we need to rebuild, it is not an error condition to not
// have a digest.
log.Error(fmt.Errorf("WARNING: Could not digest the porter manifest file: %w", err))
log.Warn(fmt.Sprint("WARNING: Could not digest the porter manifest file: %w", err))

stamp.ManifestDigest = "unknown"
} else {
stamp.ManifestDigest = digest
Expand Down
2 changes: 1 addition & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ func (c *Config) GetPorterPath(ctx context.Context) (string, error) {
// We try to resolve back to the original location
hardPath, err := evalSymlinks(porterPath)
if err != nil { // if we have trouble resolving symlinks, skip trying to help people who used symlinks
log.Error(fmt.Errorf("WARNING could not resolve %s for symbolic links: %w", porterPath, err))
return "", log.Error(fmt.Errorf("WARNING could not resolve %s for symbolic links: %w", porterPath, err))
} else if hardPath != porterPath {
log.Debugf("Resolved porter binary from %s to %s", porterPath, hardPath)
porterPath = hardPath
Expand Down
26 changes: 20 additions & 6 deletions pkg/config/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package config

import (
"context"
"log"
"os"
"path/filepath"
"strconv"
Expand Down Expand Up @@ -42,14 +43,27 @@ func (c *TestConfig) SetupUnitTest() {
c.SetHomeDir(home)

// Fake out the porter home directory
c.FileSystem.Create(filepath.Join(home, "porter"))
c.FileSystem.Create(filepath.Join(home, "runtimes", "porter-runtime"))
var err error
if _, err = c.FileSystem.Create(filepath.Join(home, "porter")); err != nil {
log.Fatal(err)
}
if _, err = c.FileSystem.Create(filepath.Join(home, "runtimes", "porter-runtime")); err != nil {
log.Fatal(err)
}

mixinsDir := filepath.Join(home, "mixins")
c.FileSystem.Create(filepath.Join(mixinsDir, "exec/exec"))
c.FileSystem.Create(filepath.Join(mixinsDir, "exec/runtimes/exec-runtime"))
c.FileSystem.Create(filepath.Join(mixinsDir, "testmixin/testmixin"))
c.FileSystem.Create(filepath.Join(mixinsDir, "testmixin/runtimes/testmixin-runtime"))
if _, err = c.FileSystem.Create(filepath.Join(mixinsDir, "exec/exec")); err != nil {
log.Fatal(err)
}
if _, err = c.FileSystem.Create(filepath.Join(mixinsDir, "exec/runtimes/exec-runtime")); err != nil {
log.Fatal(err)
}
if _, err = c.FileSystem.Create(filepath.Join(mixinsDir, "testmixin/testmixin")); err != nil {
log.Fatal(err)
}
if _, err = c.FileSystem.Create(filepath.Join(mixinsDir, "testmixin/runtimes/testmixin-runtime")); err != nil {
log.Fatal(err)
}
}

// SetupIntegrationTest initializes the filesystem with the supporting files in
Expand Down
34 changes: 23 additions & 11 deletions pkg/config/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package config
import (
"bytes"
"context"
"errors"
"fmt"
"sort"
"strings"

"get.porter.sh/porter/pkg/tracing"
"github.com/osteele/liquid"
Expand All @@ -30,19 +30,31 @@ func LoadFromEnvironment() DataStoreLoaderFunc {
}

func BindViperToEnvironmentVariables(v *viper.Viper) {
v.SetEnvPrefix("PORTER")
v.SetEnvKeyReplacer(strings.NewReplacer("-", "_", ".", "_"))
v.AutomaticEnv()

// Bind open telemetry environment variables
// See https://github.com/open-telemetry/opentelemetry-go/tree/main/exporters/otlp/otlptrace
v.BindEnv("telemetry.endpoint", "OTEL_EXPORTER_OTLP_ENDPOINT", "OTEL_EXPORTER_OTLP_TRACES_ENDPOINT")
v.BindEnv("telemetry.protocol", "OTEL_EXPORTER_OTLP_PROTOCOL", "OTEL_EXPORTER_OTLP_TRACES_PROTOCOL")
v.BindEnv("telemetry.insecure", "OTEL_EXPORTER_OTLP_INSECURE", "OTEL_EXPORTER_OTLP_TRACES_INSECURE")
v.BindEnv("telemetry.certificate", "OTEL_EXPORTER_OTLP_CERTIFICATE", "OTEL_EXPORTER_OTLP_TRACES_CERTIFICATE")
v.BindEnv("telemetry.headers", "OTEL_EXPORTER_OTLP_HEADERS", "OTEL_EXPORTER_OTLP_TRACES_HEADERS")
v.BindEnv("telemetry.compression", "OTEL_EXPORTER_OTLP_COMPRESSION", "OTEL_EXPORTER_OTLP_TRACES_COMPRESSION")
v.BindEnv("telemetry.timeout", "OTEL_EXPORTER_OTLP_TIMEOUT", "OTEL_EXPORTER_OTLP_TRACES_TIMEOUT")
var err error
if err = v.BindEnv("telemetry.endpoint", "OTEL_EXPORTER_OTLP_ENDPOINT", "OTEL_EXPORTER_OTLP_TRACES_ENDPOINT"); err != nil {
errors.Unwrap(err)
}
if err = v.BindEnv("telemetry.protocol", "OTEL_EXPORTER_OTLP_PROTOCOL", "OTEL_EXPORTER_OTLP_TRACES_PROTOCOL"); err != nil {
errors.Unwrap(err)
}
if err = v.BindEnv("telemetry.insecure", "OTEL_EXPORTER_OTLP_INSECURE", "OTEL_EXPORTER_OTLP_TRACES_INSECURE"); err != nil {
errors.Unwrap(err)
}
if err = v.BindEnv("telemetry.certificate", "OTEL_EXPORTER_OTLP_CERTIFICATE", "OTEL_EXPORTER_OTLP_TRACES_CERTIFICATE"); err != nil {
errors.Unwrap(err)
}
if err = v.BindEnv("telemetry.headers", "OTEL_EXPORTER_OTLP_HEADERS", "OTEL_EXPORTER_OTLP_TRACES_HEADERS"); err != nil {
errors.Unwrap(err)
}
if err = v.BindEnv("telemetry.compression", "OTEL_EXPORTER_OTLP_COMPRESSION", "OTEL_EXPORTER_OTLP_TRACES_COMPRESSION"); err != nil {
errors.Unwrap(err)
}
if err = v.BindEnv("telemetry.timeout", "OTEL_EXPORTER_OTLP_TIMEOUT", "OTEL_EXPORTER_OTLP_TRACES_TIMEOUT"); err != nil {
errors.Unwrap(err)
}
}

// LoadFromFilesystem loads data with the following precedence:
Expand Down
8 changes: 7 additions & 1 deletion pkg/editor/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,13 @@ func (e *Editor) Run(ctx context.Context) ([]byte, error) {
if err != nil {
return nil, err
}
defer e.FileSystem.Remove(tempFile.Name())

defer func() error {
if err = e.FileSystem.Remove(tempFile.Name()); err != nil {
return err
}
return nil
}()

_, err = tempFile.Write(e.contents)
if err != nil {
Expand Down
4 changes: 3 additions & 1 deletion pkg/exec/builder/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ func (h IgnoreErrorHandler) HandleError(ctx context.Context, err ExitError, stdo
for _, allowMatch := range h.Output.Regex {
expression, regexErr := regexp.Compile(allowMatch)
if regexErr != nil {
span.Error(fmt.Errorf("Could not ignore failed command because the Regex specified by the mixin step definition (%q) is invalid:%s", allowMatch, regexErr.Error()))
if err := span.Error(fmt.Errorf("Could not ignore failed command because the Regex specified by the mixin step definition (%q) is invalid:%s", allowMatch, regexErr.Error())); err != nil {
return err
}
return err
}

Expand Down
6 changes: 4 additions & 2 deletions pkg/exec/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ func TestPrintVersion(t *testing.T) {
opts := version.Options{}
err := opts.Validate()
require.NoError(t, err)
m.PrintVersion(opts)
err = m.PrintVersion(opts)
require.NoError(t, err)

gotOutput := m.TestConfig.TestContext.GetOutput()
wantOutput := "exec v1.2.3 (abc123) by Porter Authors"
Expand All @@ -38,7 +39,8 @@ func TestPrintJsonVersion(t *testing.T) {
opts.RawFormat = string(printer.FormatJson)
err := opts.Validate()
require.NoError(t, err)
m.PrintVersion(opts)
err = m.PrintVersion(opts)
require.NoError(t, err)

gotOutput := m.TestConfig.TestContext.GetOutput()
wantOutput := `{
Expand Down
10 changes: 7 additions & 3 deletions pkg/pkgmgmt/client/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,14 +200,18 @@ func (fs *FileSystem) downloadFile(ctx context.Context, url url.URL, destPath st
return log.Error(fmt.Errorf("unable to check if directory exists %s: %w", parentDir, err))
}

cleanup := func() {}
cleanup := func() error { return nil }
if !parentDirExists {
err = fs.FileSystem.MkdirAll(parentDir, pkg.FileModeDirectory)
if err != nil {
return log.Error(fmt.Errorf("unable to create parent directory %s: %w", parentDir, err))
}
cleanup = func() {
fs.FileSystem.RemoveAll(parentDir) // If we can't download the file, don't leave traces of it
cleanup = func() error {
// If we can't download the file, don't leave traces of it
if err = fs.FileSystem.RemoveAll(parentDir); err != nil {
return err
}
return nil
}
}

Expand Down
4 changes: 3 additions & 1 deletion pkg/pkgmgmt/client/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ func (r *Runner) Run(ctx context.Context, commandOpts pkgmgmt.CommandOptions) er
}
go func() {
defer stdin.Close()
io.WriteString(stdin, commandOpts.Input)
if _, err := io.WriteString(stdin, commandOpts.Input); err != nil {
span.Error(err)
}
}()
}

Expand Down
Loading

0 comments on commit 7d74683

Please sign in to comment.