Skip to content
Merged
3 changes: 1 addition & 2 deletions cmd/deadgate/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,7 @@ func trimDots(skipped []string) []string {
// sh is a command's standard output; what it said on standard error is in the error.
func sh(name string, args ...string) (string, error) {
out, err := exec.Command(name, args...).Output() //nolint:gosec // go and the deadcode binary the caller names, over its own module
var exit *exec.ExitError
if errors.As(err, &exit) {
if exit, ok := errors.AsType[*exec.ExitError](err); ok {
return "", fmt.Errorf("%s %s: %w: %s", name, strings.Join(args, " "), err, strings.TrimSpace(string(exit.Stderr)))
}
if err != nil {
Expand Down
9 changes: 3 additions & 6 deletions cmd/ghatrace/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ import (

func main() {
err := run(context.Background(), os.Args[1:]) // mutate-exempt: main's way in; run is what the tests drive
var exit exitError
if errors.As(err, &exit) {
if exit, ok := errors.AsType[exitError](err); ok {
os.Exit(int(exit))
}
if err != nil { // mutate-exempt: main's way out; run is what the tests drive
Expand Down Expand Up @@ -266,9 +265,7 @@ func span(ctx context.Context, args []string) error {
if len(argv) == 0 {
return errors.New("span: a command is needed after --")
}
if *name == "" {
*name = strings.Join(argv, " ")
}
*name = cmp.Or(*name, strings.Join(argv, " "))
cmd := exec.CommandContext(ctx, argv[0], argv[1:]...) //nolint:gosec // the command the caller asked to run
cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
return timed(cmd, *name, nil)
Expand Down Expand Up @@ -337,7 +334,7 @@ func withJSON(argv []string) []string {
// go test command in one argument.
func verbose(args []string) bool {
for _, a := range args {
for _, word := range strings.Fields(a) {
for word := range strings.FieldsSeq(a) {
switch strings.TrimLeft(word, "-") {
case "v", "v=true", "test.v", "test.v=true":
return true
Expand Down
5 changes: 2 additions & 3 deletions cmd/linkweight/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func measure(rootsPattern string, skipped []string, tags string, progress io.Wri
return nil, err
}
var roots []string
for _, m := range strings.Fields(mains) {
for m := range strings.FieldsSeq(mains) {
rel := strings.TrimPrefix(strings.TrimPrefix(m, module), "/")
if !slices.Contains(skipped, "./"+rel) {
roots = append(roots, m)
Expand Down Expand Up @@ -191,8 +191,7 @@ func notEdges(path string) string {
// sh is a command's standard output; what it said on standard error is in the error.
func sh(name string, args ...string) (string, error) {
out, err := exec.Command(name, args...).Output() //nolint:gosec // go, over the caller's own module
var exit *exec.ExitError
if errors.As(err, &exit) {
if exit, ok := errors.AsType[*exec.ExitError](err); ok {
return "", fmt.Errorf("%s %s: %w: %s", name, strings.Join(args, " "), err, strings.TrimSpace(string(exit.Stderr)))
}
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/mutate/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func TestTheExampleTheSiteShows(t *testing.T) {
func abridged(out string) string {
var head, edits, tail []string
verdict := false
for _, line := range strings.Split(out, "\n") {
for line := range strings.SplitSeq(out, "\n") {
switch {
case verdict:
tail = append(tail, line)
Expand Down
19 changes: 8 additions & 11 deletions cmd/proven/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package main

import (
"cmp"
"context"
"encoding/json"
"errors"
Expand All @@ -22,6 +23,7 @@ import (
"net/url"
"os"
"os/exec"
"slices"
"sort"
"strings"

Expand Down Expand Up @@ -95,12 +97,10 @@ func run(ctx context.Context, argv []string) error {
} else if *sha == "HEAD" {
return fmt.Errorf("-sha or GITHUB_SHA names the commit; HEAD: %w", err)
}
gh := proven.GitHub{API: "https://api.github.com", Repo: *repo, Token: os.Getenv("GH_TOKEN")}
if api := os.Getenv("GITHUB_API_URL"); api != "" {
gh.API = api
}
if gh.Token == "" {
gh.Token = os.Getenv("GITHUB_TOKEN")
gh := proven.GitHub{
API: cmp.Or(os.Getenv("GITHUB_API_URL"), "https://api.github.com"),
Repo: *repo,
Token: cmp.Or(os.Getenv("GH_TOKEN"), os.Getenv("GITHUB_TOKEN")),
}
trees := proven.TreesOf(".", gh)
ev := strings.Split(*events, ",")
Expand Down Expand Up @@ -273,11 +273,8 @@ func base(ctx context.Context, gh proven.GitHub, trees proven.Trees, sha, ref, m
}
var passed []proven.Run
for _, r := range runs {
for _, e := range events {
if r.Event == e {
passed = append(passed, r)
break
}
if slices.Contains(events, r.Event) {
passed = append(passed, r)
}
}
history, err := proven.FirstParent(ctx, ".", sha, depth)
Expand Down
9 changes: 4 additions & 5 deletions cmd/proven/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"github.com/stretchr/testify/require"

"github.com/spin-stack/go-tools/internal/gittest"
"github.com/spin-stack/go-tools/internal/proven"
)

// stdout is what f printed to os.Stdout.
Expand Down Expand Up @@ -307,12 +306,12 @@ func TestTestsAreWhatTheAnsweringRunsLogged(t *testing.T) {
{Workflow: "kvm.yml", ID: 30, URL: "u30", Conclusion: "failure", Head: f.main[1]},
},
Tests: []evidenceOf{
{Test: proven.Test{Name: "TestA", Passed: true, Seconds: 1.5}, evidenceJob: fleet},
{Test: proven.Test{Name: "TestA", Passed: true, Seconds: 1.4}, evidenceJob: evidenceJob{Workflow: "kvm.yml", Run: 30, Job: "e2e-verify:1/1"}},
{Test: proven.Test{Name: "TestB", Passed: false, Seconds: 2}, evidenceJob: evidenceJob{Workflow: "kvm.yml", Run: 30, Job: "e2e-verify:1/1"}},
{Name: "TestA", Passed: true, Seconds: 1.5, evidenceJob: fleet},
{Name: "TestA", Passed: true, Seconds: 1.4, Workflow: "kvm.yml", Run: 30, Job: "e2e-verify:1/1"},
{Name: "TestB", Passed: false, Seconds: 2, Workflow: "kvm.yml", Run: 30, Job: "e2e-verify:1/1"},
},
// What job 42's test timed, beside the machine it ran on.
Measures: []evidenceMeasure{{Measure: proven.Measure{Name: "boot_to_running_ms", Value: 12345}, evidenceJob: fleet}},
Measures: []evidenceMeasure{{Name: "boot_to_running_ms", Value: 12345, evidenceJob: fleet}},
}, got)
assert.Contains(t, f.asked, "/repos/o/r/actions/jobs/41/logs", "a job whose log is gone is asked, and says nothing")

Expand Down
5 changes: 2 additions & 3 deletions cmd/refs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,7 @@ func taskLister(root string, task []string) refs.TaskLister {
cmd.Env = append(os.Environ(), "NO_COLOR=1")
out, err := cmd.Output()
if err != nil {
var exit *exec.ExitError
if errors.As(err, &exit) {
if exit, ok := errors.AsType[*exec.ExitError](err); ok {
return nil, fmt.Errorf("refs: %s -d %s --list-all --json --no-status: %w\n%s", strings.Join(task, " "), dir, err, exit.Stderr)
}
return nil, fmt.Errorf("refs: running %s: %w", strings.Join(task, " "), err)
Expand Down Expand Up @@ -152,7 +151,7 @@ func git(root string, args ...string) ([]string, error) {
return nil, fmt.Errorf("refs: git %s: %w", strings.Join(args, " "), err)
}
var lines []string
for _, line := range strings.Split(strings.TrimSpace(string(out)), "\n") {
for line := range strings.SplitSeq(strings.TrimSpace(string(out)), "\n") {
if line != "" {
lines = append(lines, line)
}
Expand Down
9 changes: 3 additions & 6 deletions ghatrace/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package ghatrace

import (
"bufio"
"cmp"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -50,12 +51,8 @@ func Record(s Span) error {
if path == "" {
return nil
}
if s.Runner == "" {
s.Runner = os.Getenv("RUNNER_NAME")
}
if s.Job == "" {
s.Job = os.Getenv("GITHUB_JOB")
}
s.Runner = cmp.Or(s.Runner, os.Getenv("RUNNER_NAME"))
s.Job = cmp.Or(s.Job, os.Getenv("GITHUB_JOB"))
line, err := json.Marshal(s)
if err != nil {
return err
Expand Down
5 changes: 2 additions & 3 deletions internal/exposure/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,14 +212,13 @@ func patches(dir string, each func(commit, file string, kind added, line string)
func lines(dir string, args ...string) ([]string, error) {
raw, err := exec.Command("git", append([]string{"-C", dir}, args...)...).Output()
if err != nil {
var exit *exec.ExitError
if errors.As(err, &exit) {
if exit, ok := errors.AsType[*exec.ExitError](err); ok {
return nil, fmt.Errorf("git %s: %w: %s", strings.Join(args, " "), err, strings.TrimSpace(string(exit.Stderr)))
}
return nil, fmt.Errorf("git %s: %w", strings.Join(args, " "), err)
}
var out []string
for _, l := range strings.Split(string(raw), "\n") {
for l := range strings.SplitSeq(string(raw), "\n") {
if l != "" {
out = append(out, l)
}
Expand Down
25 changes: 7 additions & 18 deletions internal/exposure/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import (
"encoding/json"
"fmt"
"os"
"sort"

"github.com/spin-stack/go-tools/internal/allow"
)

// Secrets is gitleaks' JSON report as findings, one for each secret it found, each of kind
Expand Down Expand Up @@ -37,22 +38,10 @@ func Secrets(report string, onDefault func(commit string) bool) ([]Finding, erro
// allowlist's keys that answer nothing any more, sorted. A key answers a finding by its kind
// and value, or by `<kind> *` for every value of the kind.
func Explain(allowed map[string]bool, found []Finding) (unexplained []Finding, stale []string) {
used := map[string]bool{}
for _, f := range found {
switch k, any := f.Key(), f.Kind+" *"; {
case allowed[k]:
used[k] = true
case allowed[any]:
used[any] = true
default:
unexplained = append(unexplained, f)
}
}
for k := range allowed {
if !used[k] {
stale = append(stale, k)
return allow.Split(allowed, found, func(f Finding) string {
if allowed[f.Key()] {
return f.Key()
}
}
sort.Strings(stale)
return unexplained, stale
return f.Kind + " *"
})
}
16 changes: 16 additions & 0 deletions internal/fetch/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ package fetch

import (
"context"
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -337,3 +339,17 @@ func cause(ctx context.Context, err error) error {
}
return err
}

// FileSum is the SHA-256 of the file at p, in hex: what a pin names, for the file a fetch wrote.
func FileSum(p string) (string, error) {
f, err := os.Open(p) //nolint:gosec // a file the caller fetched, to check against its pin
if err != nil {
return "", err
}
defer func() { _ = f.Close() }() // read-only
h := sha256.New()
if _, err := io.Copy(h, f); err != nil {
return "", err
}
return hex.EncodeToString(h.Sum(nil)), nil
}
13 changes: 4 additions & 9 deletions internal/gencache/gencache.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ import (
"go/parser"
"go/token"
"io"
"maps"
"os"
"os/exec"
"path"
"path/filepath"
"regexp"
"sort"
"slices"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -349,12 +350,7 @@ func (r *run) key(ctx context.Context, s step) (string, string) {
dirs[dep] = q.Dir
}
}
names := make([]string, 0, len(dirs))
for name := range dirs {
names = append(names, name)
}
sort.Strings(names)
for _, name := range names {
for _, name := range slices.Sorted(maps.Keys(dirs)) {
own, err := r.dirHash(dirs[name], tests)
if err != nil {
return "", "a file it reads cannot be read"
Expand Down Expand Up @@ -512,8 +508,7 @@ func abs(dir, file string) string {
}

func stderr(err error) error {
var ee *exec.ExitError
if errors.As(err, &ee) {
if ee, ok := errors.AsType[*exec.ExitError](err); ok {
return fmt.Errorf("%w: %s", err, bytes.TrimSpace(ee.Stderr))
}
return err
Expand Down
5 changes: 2 additions & 3 deletions internal/gencache/gencache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gencache

import (
"bytes"
"maps"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -120,9 +121,7 @@ func newModule(t *testing.T, extra map[string]string) string {
mod = []byte("module example.com/m" + string(mod[bytes.IndexByte(mod, '\n'):]))
files := map[string]string{"go.mod": string(mod), "go.sum": string(sum)}
for _, set := range []map[string]string{fixture, extra} {
for name, body := range set {
files[name] = body
}
maps.Copy(files, set)
}
writeTree(t, dir, files)
return dir
Expand Down
Loading
Loading