internal/log: move IncludeStderr
Move IncludeStderr from package internal to log, since it is related to
logging errors.
Also replace uses of package log from the stdlib with
x/ecosystem/internal/log.
Change-Id: Iba25392c8f7d5ba36565fb59ae51dd174141b116
Reviewed-on: https://go-review.googlesource.com/c/pkgsite-metrics/+/465186
Run-TryBot: Julie Qiu <julieqiu@google.com>
Reviewed-by: Jonathan Amsterdam <jba@google.com>
Reviewed-by: Julie Qiu <julieqiu@google.com>
Auto-Submit: Julie Qiu <julieqiu@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
diff --git a/internal/log/log.go b/internal/log/log.go
index 7976bcf..778fdab 100644
--- a/internal/log/log.go
+++ b/internal/log/log.go
@@ -6,11 +6,14 @@
package log
import (
+ "bytes"
"context"
+ "errors"
"fmt"
"io"
"log"
"os"
+ "os/exec"
"reflect"
"strings"
"sync"
@@ -280,3 +283,13 @@
log.Printf("Error: %s is invalid LogLevel. Possible values are [debug, info, warning, error, fatal]", v)
return logging.Default
}
+
+// IncludeStderr includes the stderr with an *exec.ExitError.
+// If err is not an *exec.ExitError, it returns err.Error().
+func IncludeStderr(err error) string {
+ var eerr *exec.ExitError
+ if errors.As(err, &eerr) {
+ return fmt.Sprintf("%v: %s", eerr, bytes.TrimSpace(eerr.Stderr))
+ }
+ return err.Error()
+}
diff --git a/internal/sandbox/sandbox_test.go b/internal/sandbox/sandbox_test.go
index d88d652..b396652 100644
--- a/internal/sandbox/sandbox_test.go
+++ b/internal/sandbox/sandbox_test.go
@@ -12,7 +12,7 @@
"strings"
"testing"
- "golang.org/x/pkgsite-metrics/internal"
+ "golang.org/x/pkgsite-metrics/internal/log"
)
// These tests require a minimal bundle, in testdata/bundle.
@@ -32,7 +32,7 @@
t.Run("printargs", func(t *testing.T) {
out, err := sb.Run(ctx, "/printargs", "a", "b")
if err != nil {
- t.Fatal(internal.IncludeStderr(err))
+ t.Fatal(log.IncludeStderr(err))
}
want := `args:
diff --git a/internal/util.go b/internal/util.go
deleted file mode 100644
index 15395a6..0000000
--- a/internal/util.go
+++ /dev/null
@@ -1,22 +0,0 @@
-// Copyright 2022 The Go Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style
-// license that can be found in the LICENSE file.
-
-package internal
-
-import (
- "bytes"
- "errors"
- "fmt"
- "os/exec"
-)
-
-// IncludeStderr includes the stderr with an *exec.ExitError.
-// If err is not an *exec.ExitError, it returns err.Error().
-func IncludeStderr(err error) string {
- var eerr *exec.ExitError
- if errors.As(err, &eerr) {
- return fmt.Sprintf("%v: %s", eerr, bytes.TrimSpace(eerr.Stderr))
- }
- return err.Error()
-}