From c3771591214e08ddc1042fdd00ed56e404fd51f6 Mon Sep 17 00:00:00 2001 From: Earl Warren Date: Mon, 28 Jul 2025 12:26:41 +0000 Subject: [PATCH] chore: use the same .golangci.yml as the runner & gofumpt over gofmt (#206) To prepare for a smooth merge in the runner codebase. - run with --fix for gofumpt and golangci - manual edits for - disabling useless package naming warning - rename variables that had underscore in their name - remove trailing else at the end of a few functions Reviewed-on: https://code.forgejo.org/forgejo/act/pulls/206 Reviewed-by: Michael Kriese Co-authored-by: Earl Warren Co-committed-by: Earl Warren --- act/artifactcache/handler_test.go | 38 ++++++++++++++------------ act/artifactcache/mac.go | 4 +-- act/artifactcache/storage.go | 4 +-- act/cacheproxy/handler.go | 10 +++---- act/common/draw.go | 2 ++ act/common/executor.go | 2 +- act/common/file.go | 6 ++-- act/common/git/git.go | 11 +++----- act/common/git/git_test.go | 2 +- act/container/container_types.go | 4 +-- act/container/docker_build.go | 5 ++-- act/container/docker_cli.go | 2 +- act/container/docker_cli_test.go | 12 +++----- act/container/docker_images.go | 4 +-- act/container/docker_run.go | 12 ++++---- act/container/docker_run_test.go | 2 +- act/container/host_environment_test.go | 18 ++++++------ act/exprparser/functions.go | 2 +- act/exprparser/interpreter.go | 6 ++-- act/filecollector/file_collector.go | 3 +- act/lookpath/env.go | 3 +- act/lookpath/lp_plan9.go | 2 +- act/lookpath/lp_unix.go | 2 +- act/model/github_context.go | 14 ++++++---- act/model/workflow.go | 2 +- act/runner/action.go | 16 +++++------ act/runner/action_test.go | 2 +- act/runner/command.go | 15 +++++++--- act/runner/container_mock_test.go | 4 +-- act/runner/logger.go | 10 ++++--- act/runner/reusable_workflow.go | 6 ++-- act/runner/run_context.go | 26 +++++++++--------- act/runner/step.go | 2 +- act/runner/step_action_local_test.go | 2 +- act/runner/step_docker.go | 6 ++-- act/runner/step_run.go | 8 +++--- act/schema/schema.go | 9 ++---- 37 files changed, 138 insertions(+), 140 deletions(-) diff --git a/act/artifactcache/handler_test.go b/act/artifactcache/handler_test.go index 183a462f..b8ff6769 100644 --- a/act/artifactcache/handler_test.go +++ b/act/artifactcache/handler_test.go @@ -18,35 +18,39 @@ import ( "go.etcd.io/bbolt" ) -const cache_repo = "testuser/repo" -const cache_runnum = "1" -const cache_timestamp = "0" -const cache_mac = "c13854dd1ac599d1d61680cd93c26b77ba0ee10f374a3408bcaea82f38ca1865" +const ( + cacheRepo = "testuser/repo" + cacheRunnum = "1" + cacheTimestamp = "0" + cacheMac = "c13854dd1ac599d1d61680cd93c26b77ba0ee10f374a3408bcaea82f38ca1865" +) -var handlerExternalUrl string +var handlerExternalURL string type AuthHeaderTransport struct { T http.RoundTripper } func (t *AuthHeaderTransport) RoundTrip(req *http.Request) (*http.Response, error) { - req.Header.Set("Forgejo-Cache-Repo", cache_repo) - req.Header.Set("Forgejo-Cache-RunNumber", cache_runnum) - req.Header.Set("Forgejo-Cache-Timestamp", cache_timestamp) - req.Header.Set("Forgejo-Cache-MAC", cache_mac) - req.Header.Set("Forgejo-Cache-Host", handlerExternalUrl) + req.Header.Set("Forgejo-Cache-Repo", cacheRepo) + req.Header.Set("Forgejo-Cache-RunNumber", cacheRunnum) + req.Header.Set("Forgejo-Cache-Timestamp", cacheTimestamp) + req.Header.Set("Forgejo-Cache-MAC", cacheMac) + req.Header.Set("Forgejo-Cache-Host", handlerExternalURL) return t.T.RoundTrip(req) } -var httpClientTransport = AuthHeaderTransport{http.DefaultTransport} -var httpClient = http.Client{Transport: &httpClientTransport} +var ( + httpClientTransport = AuthHeaderTransport{http.DefaultTransport} + httpClient = http.Client{Transport: &httpClientTransport} +) func TestHandler(t *testing.T) { dir := filepath.Join(t.TempDir(), "artifactcache") handler, err := StartHandler(dir, "", 0, "secret", nil) require.NoError(t, err) - handlerExternalUrl = handler.ExternalURL() + handlerExternalURL = handler.ExternalURL() base := fmt.Sprintf("%s%s", handler.ExternalURL(), urlBase) defer func() { @@ -386,11 +390,11 @@ func TestHandler(t *testing.T) { // Perform the same request with incorrect MAC data req, err := http.NewRequest("GET", fmt.Sprintf("%s/cache?keys=%s&version=%s", base, key, version), nil) require.NoError(t, err) - req.Header.Set("Forgejo-Cache-Repo", cache_repo) - req.Header.Set("Forgejo-Cache-RunNumber", cache_runnum) - req.Header.Set("Forgejo-Cache-Timestamp", cache_timestamp) + req.Header.Set("Forgejo-Cache-Repo", cacheRepo) + req.Header.Set("Forgejo-Cache-RunNumber", cacheRunnum) + req.Header.Set("Forgejo-Cache-Timestamp", cacheTimestamp) req.Header.Set("Forgejo-Cache-MAC", "33f0e850ba0bdfd2f3e66ff79c1f8004b8226114e3b2e65c229222bb59df0f9d") // ! This is not the correct MAC - req.Header.Set("Forgejo-Cache-Host", handlerExternalUrl) + req.Header.Set("Forgejo-Cache-Host", handlerExternalURL) resp, err = http.Get(fmt.Sprintf("%s/cache?keys=%s&version=%s", base, key, version)) require.NoError(t, err) require.Equal(t, 403, resp.StatusCode) diff --git a/act/artifactcache/mac.go b/act/artifactcache/mac.go index 16f46c26..3e237c4e 100644 --- a/act/artifactcache/mac.go +++ b/act/artifactcache/mac.go @@ -14,9 +14,7 @@ import ( "github.com/nektos/act/pkg/cacheproxy" ) -var ( - ErrValidation = errors.New("validation error") -) +var ErrValidation = errors.New("validation error") func (h *Handler) validateMac(rundata cacheproxy.RunData) (string, error) { // TODO: allow configurable max age diff --git a/act/artifactcache/storage.go b/act/artifactcache/storage.go index 0cadfa6f..c066520c 100644 --- a/act/artifactcache/storage.go +++ b/act/artifactcache/storage.go @@ -31,7 +31,7 @@ func (s *Storage) Exist(id uint64) (bool, error) { return true, nil } -func (s *Storage) Write(id uint64, offset uint64, reader io.Reader) error { +func (s *Storage) Write(id, offset uint64, reader io.Reader) error { name := s.tempName(id, offset) if err := os.MkdirAll(filepath.Dir(name), 0o755); err != nil { return err @@ -110,7 +110,7 @@ func (s *Storage) tempDir(id uint64) string { return filepath.Join(s.rootDir, "tmp", fmt.Sprint(id)) } -func (s *Storage) tempName(id uint64, offset uint64) string { +func (s *Storage) tempName(id, offset uint64) string { return filepath.Join(s.tempDir(id), fmt.Sprintf("%016x", offset)) } diff --git a/act/cacheproxy/handler.go b/act/cacheproxy/handler.go index 61b09ebe..3bba0ff5 100644 --- a/act/cacheproxy/handler.go +++ b/act/cacheproxy/handler.go @@ -29,9 +29,7 @@ const ( urlBase = "/_apis/artifactcache" ) -var ( - urlRegex = regexp.MustCompile(`/(\w+)(/_apis/artifactcache/.+)`) -) +var urlRegex = regexp.MustCompile(`/(\w+)(/_apis/artifactcache/.+)`) type Handler struct { router *httprouter.Router @@ -55,7 +53,7 @@ type RunData struct { RepositoryMAC string } -func (h *Handler) CreateRunData(fullName string, runNumber string, timestamp string) RunData { +func (h *Handler) CreateRunData(fullName, runNumber, timestamp string) RunData { mac := computeMac(h.cacheSecret, fullName, runNumber, timestamp) return RunData{ RepositoryFullName: fullName, @@ -65,7 +63,7 @@ func (h *Handler) CreateRunData(fullName string, runNumber string, timestamp str } } -func StartHandler(targetHost string, outboundIP string, port uint16, cacheSecret string, logger logrus.FieldLogger) (*Handler, error) { +func StartHandler(targetHost, outboundIP string, port uint16, cacheSecret string, logger logrus.FieldLogger) (*Handler, error) { h := &Handler{} if logger == nil { @@ -142,7 +140,7 @@ func (h *Handler) newReverseProxy(targetHost string) (*httputil.ReverseProxy, er h.logger.Warn(fmt.Sprintf("Tried starting a cache proxy with id %s, which does not exist.", id)) return } - var runData = data.(RunData) + runData := data.(RunData) uri := matches[2] r.SetURL(targetURL) diff --git a/act/common/draw.go b/act/common/draw.go index b5b21fe9..30bd3dd5 100644 --- a/act/common/draw.go +++ b/act/common/draw.go @@ -72,6 +72,7 @@ func (p *Pen) drawTopBars(buf io.Writer, labels ...string) { } fmt.Fprintf(buf, "\n") } + func (p *Pen) drawBottomBars(buf io.Writer, labels ...string) { style := styleDefs[p.style] for _, label := range labels { @@ -83,6 +84,7 @@ func (p *Pen) drawBottomBars(buf io.Writer, labels ...string) { } fmt.Fprintf(buf, "\n") } + func (p *Pen) drawLabels(buf io.Writer, labels ...string) { style := styleDefs[p.style] for _, label := range labels { diff --git a/act/common/executor.go b/act/common/executor.go index 24173565..3033ca09 100644 --- a/act/common/executor.go +++ b/act/common/executor.go @@ -68,7 +68,7 @@ func NewPipelineExecutor(executors ...Executor) Executor { } // NewConditionalExecutor creates a new executor based on conditions -func NewConditionalExecutor(conditional Conditional, trueExecutor Executor, falseExecutor Executor) Executor { +func NewConditionalExecutor(conditional Conditional, trueExecutor, falseExecutor Executor) Executor { return func(ctx context.Context) error { if conditional(ctx) { if trueExecutor != nil { diff --git a/act/common/file.go b/act/common/file.go index 09c21029..243c07e4 100644 --- a/act/common/file.go +++ b/act/common/file.go @@ -7,7 +7,7 @@ import ( ) // CopyFile copy file -func CopyFile(source string, dest string) (err error) { +func CopyFile(source, dest string) (err error) { sourcefile, err := os.Open(source) if err != nil { return err @@ -30,11 +30,11 @@ func CopyFile(source string, dest string) (err error) { } } - return + return err } // CopyDir recursive copy of directory -func CopyDir(source string, dest string) (err error) { +func CopyDir(source, dest string) (err error) { // get properties of source dir sourceinfo, err := os.Stat(source) if err != nil { diff --git a/act/common/git/git.go b/act/common/git/git.go index 87939be3..ce1b2f66 100644 --- a/act/common/git/git.go +++ b/act/common/git/git.go @@ -52,7 +52,7 @@ func (e *Error) Commit() string { } // FindGitRevision get the current git revision -func FindGitRevision(ctx context.Context, file string) (shortSha string, sha string, err error) { +func FindGitRevision(ctx context.Context, file string) (shortSha, sha string, err error) { logger := common.Logger(ctx) gitDir, err := git.PlainOpenWithOptions( @@ -62,7 +62,6 @@ func FindGitRevision(ctx context.Context, file string) (shortSha string, sha str EnableDotGitCommonDir: true, }, ) - if err != nil { logger.WithError(err).Error("path", file, "not located inside a git repository") return "", "", err @@ -96,8 +95,8 @@ func FindGitRef(ctx context.Context, file string) (string, error) { logger.Debugf("HEAD points to '%s'", ref) // Prefer the git library to iterate over the references and find a matching tag or branch. - var refTag = "" - var refBranch = "" + refTag := "" + refBranch := "" repo, err := git.PlainOpenWithOptions( file, &git.PlainOpenOptions{ @@ -105,7 +104,6 @@ func FindGitRef(ctx context.Context, file string) (string, error) { EnableDotGitCommonDir: true, }, ) - if err != nil { return "", err } @@ -144,7 +142,6 @@ func FindGitRef(ctx context.Context, file string) (string, error) { return nil }) - if err != nil { return "", err } @@ -198,7 +195,7 @@ func findGitRemoteURL(_ context.Context, file, remoteName string) (string, error return remote.Config().URLs[0], nil } -func findGitSlug(url string, githubInstance string) (string, string, error) { +func findGitSlug(url, githubInstance string) (string, string, error) { if matches := codeCommitHTTPRegex.FindStringSubmatch(url); matches != nil { return "CodeCommit", matches[2], nil } else if matches := codeCommitSSHRegex.FindStringSubmatch(url); matches != nil { diff --git a/act/common/git/git_test.go b/act/common/git/git_test.go index 7a08a97d..51fed0d6 100644 --- a/act/common/git/git_test.go +++ b/act/common/git/git_test.go @@ -19,7 +19,7 @@ import ( func TestFindGitSlug(t *testing.T) { assert := assert.New(t) - var slugTests = []struct { + slugTests := []struct { url string // input provider string // expected result slug string // expected result diff --git a/act/container/container_types.go b/act/container/container_types.go index 810d7914..ed27c29a 100644 --- a/act/container/container_types.go +++ b/act/container/container_types.go @@ -49,11 +49,11 @@ type FileEntry struct { // Container for managing docker run containers type Container interface { - Create(capAdd []string, capDrop []string) common.Executor + Create(capAdd, capDrop []string) common.Executor ConnectToNetwork(name string) common.Executor Copy(destPath string, files ...*FileEntry) common.Executor CopyTarStream(ctx context.Context, destPath string, tarStream io.Reader) error - CopyDir(destPath string, srcPath string, useGitIgnore bool) common.Executor + CopyDir(destPath, srcPath string, useGitIgnore bool) common.Executor GetContainerArchive(ctx context.Context, srcPath string) (io.ReadCloser, error) Pull(forcePull bool) common.Executor Start(attach bool) common.Executor diff --git a/act/container/docker_build.go b/act/container/docker_build.go index 1d06cc35..52cdab80 100644 --- a/act/container/docker_build.go +++ b/act/container/docker_build.go @@ -68,7 +68,8 @@ func NewDockerBuildExecutor(input NewDockerBuildExecutorInput) common.Executor { return nil } } -func createBuildContext(ctx context.Context, contextDir string, relDockerfile string) (io.ReadCloser, error) { + +func createBuildContext(ctx context.Context, contextDir, relDockerfile string) (io.ReadCloser, error) { common.Logger(ctx).Debugf("Creating archive for build context dir '%s' with relative dockerfile '%s'", contextDir, relDockerfile) // And canonicalize dockerfile name to a platform-independent one @@ -95,7 +96,7 @@ func createBuildContext(ctx context.Context, contextDir string, relDockerfile st // removed. The daemon will remove them for us, if needed, after it // parses the Dockerfile. Ignore errors here, as they will have been // caught by validateContextDirectory above. - var includes = []string{"."} + includes := []string{"."} keepThem1, _ := patternmatcher.Matches(".dockerignore", excludes) keepThem2, _ := patternmatcher.Matches(relDockerfile, excludes) if keepThem1 || keepThem2 { diff --git a/act/container/docker_cli.go b/act/container/docker_cli.go index 7ea5f3aa..1a1ffe6b 100644 --- a/act/container/docker_cli.go +++ b/act/container/docker_cli.go @@ -7,7 +7,7 @@ // See DOCKER_LICENSE for the full license text. // -//nolint:unparam,errcheck,depguard,unused +//nolint:unparam,errcheck,unused package container import ( diff --git a/act/container/docker_cli_test.go b/act/container/docker_cli_test.go index 3eed50ae..7d41ed15 100644 --- a/act/container/docker_cli_test.go +++ b/act/container/docker_cli_test.go @@ -6,7 +6,7 @@ // See DOCKER_LICENSE for the full license text. // -//nolint:unparam,whitespace,depguard,dupl,gocritic +//nolint:unparam,gocritic package container import ( @@ -192,7 +192,6 @@ func TestParseRunWithInvalidArgs(t *testing.T) { } func TestParseWithVolumes(t *testing.T) { - // A single volume arr, tryit := setupPlatformVolume([]string{`/tmp`}, []string{`c:\tmp`}) if config, hostConfig := mustParse(t, tryit); hostConfig.Binds != nil { @@ -260,14 +259,13 @@ func TestParseWithVolumes(t *testing.T) { t.Fatalf("Error parsing %s. Should have a single bind mount and no volumes", arr[0]) } } - } // setupPlatformVolume takes two arrays of volume specs - a Unix style // spec and a Windows style spec. Depending on the platform being unit tested, // it returns one of them, along with a volume string that would be passed // on the docker CLI (e.g. -v /bar -v /foo). -func setupPlatformVolume(u []string, w []string) ([]string, string) { +func setupPlatformVolume(u, w []string) ([]string, string) { var a []string if runtime.GOOS == "windows" { a = w @@ -301,9 +299,9 @@ func TestParseWithMacAddress(t *testing.T) { t.Fatalf("Expected an error with %v mac-address, got %v", invalidMacAddress, err) } config, _ := mustParse(t, validMacAddress) - if config.MacAddress != "92:d0:c6:0a:29:33" { //nolint:staticcheck // ignore SA1019: field is deprecated, but still used on API < v1.44. + if config.MacAddress != "92:d0:c6:0a:29:33" { t.Fatalf("Expected the config to have '92:d0:c6:0a:29:33' as container-wide MacAddress, got '%v'", - config.MacAddress) //nolint:staticcheck // ignore SA1019: field is deprecated, but still used on API < v1.44. + config.MacAddress) } } @@ -459,7 +457,6 @@ func TestParseDevice(t *testing.T) { t.Fatalf("Expected %v, got %v", deviceMapping, hostconfig.Devices) } } - } func TestParseNetworkConfig(t *testing.T) { @@ -964,7 +961,6 @@ func TestConvertToStandardNotation(t *testing.T) { for key, ports := range valid { convertedPorts, err := convertToStandardNotation(ports) - if err != nil { assert.NilError(t, err) } diff --git a/act/container/docker_images.go b/act/container/docker_images.go index 4bc24c1d..484cc834 100644 --- a/act/container/docker_images.go +++ b/act/container/docker_images.go @@ -12,7 +12,7 @@ import ( // ImageExistsLocally returns a boolean indicating if an image with the // requested name, tag and architecture exists in the local docker image store -func ImageExistsLocally(ctx context.Context, imageName string, platform string) (bool, error) { +func ImageExistsLocally(ctx context.Context, imageName, platform string) (bool, error) { cli, err := GetDockerClient(ctx) if err != nil { return false, err @@ -35,7 +35,7 @@ func ImageExistsLocally(ctx context.Context, imageName string, platform string) // RemoveImage removes image from local store, the function is used to run different // container image architectures -func RemoveImage(ctx context.Context, imageName string, force bool, pruneChildren bool) (bool, error) { +func RemoveImage(ctx context.Context, imageName string, force, pruneChildren bool) (bool, error) { cli, err := GetDockerClient(ctx) if err != nil { return false, err diff --git a/act/container/docker_run.go b/act/container/docker_run.go index e5739ed5..8c55af5e 100644 --- a/act/container/docker_run.go +++ b/act/container/docker_run.go @@ -86,7 +86,7 @@ func supportsContainerImagePlatform(ctx context.Context, cli client.APIClient) b return constraint.Check(sv) } -func (cr *containerReference) Create(capAdd []string, capDrop []string) common.Executor { +func (cr *containerReference) Create(capAdd, capDrop []string) common.Executor { return common. NewInfoExecutor("%sdocker create image=%s platform=%s entrypoint=%+q cmd=%+q network=%+q", logPrefix, cr.input.Image, cr.input.Platform, cr.input.Entrypoint, cr.input.Cmd, cr.input.NetworkMode). Then( @@ -143,7 +143,7 @@ func (cr *containerReference) Copy(destPath string, files ...*FileEntry) common. ).IfNot(common.Dryrun) } -func (cr *containerReference) CopyDir(destPath string, srcPath string, useGitIgnore bool) common.Executor { +func (cr *containerReference) CopyDir(destPath, srcPath string, useGitIgnore bool) common.Executor { return common.NewPipelineExecutor( common.NewInfoExecutor("%sdocker cp src=%s dst=%s", logPrefix, srcPath, destPath), cr.copyDir(destPath, srcPath, useGitIgnore), @@ -191,7 +191,7 @@ func (cr *containerReference) Remove() common.Executor { ).IfNot(common.Dryrun) } -func (cr *containerReference) ReplaceLogWriter(stdout io.Writer, stderr io.Writer) (io.Writer, io.Writer) { +func (cr *containerReference) ReplaceLogWriter(stdout, stderr io.Writer) (io.Writer, io.Writer) { out := cr.input.Stdout err := cr.input.Stderr @@ -472,7 +472,7 @@ func parseOptions(ctx context.Context, options string) (*containerConfig, error) return containerConfig, nil } -func (cr *containerReference) create(capAdd []string, capDrop []string) common.Executor { +func (cr *containerReference) create(capAdd, capDrop []string) common.Executor { return func(ctx context.Context) error { if cr.id != "" { return nil @@ -726,7 +726,7 @@ func (cr *containerReference) tryReadGID() common.Executor { return cr.tryReadID("-g", func(id int) { cr.GID = id }) } -func (cr *containerReference) waitForCommand(ctx context.Context, isTerminal bool, resp types.HijackedResponse, _ container.ExecCreateResponse, _ string, _ string) error { +func (cr *containerReference) waitForCommand(ctx context.Context, isTerminal bool, resp types.HijackedResponse, _ container.ExecCreateResponse, _, _ string) error { logger := common.Logger(ctx) cmdResponse := make(chan error) @@ -797,7 +797,7 @@ func (cr *containerReference) CopyTarStream(ctx context.Context, destPath string return nil } -func (cr *containerReference) copyDir(dstPath string, srcPath string, useGitIgnore bool) common.Executor { +func (cr *containerReference) copyDir(dstPath, srcPath string, useGitIgnore bool) common.Executor { return func(ctx context.Context) error { logger := common.Logger(ctx) tarFile, err := os.CreateTemp("", "act") diff --git a/act/container/docker_run_test.go b/act/container/docker_run_test.go index 81e7a013..4d58acc2 100644 --- a/act/container/docker_run_test.go +++ b/act/container/docker_run_test.go @@ -85,7 +85,7 @@ func (m *mockDockerClient) ContainerExecInspect(ctx context.Context, execID stri return args.Get(0).(container.ExecInspect), args.Error(1) } -func (m *mockDockerClient) CopyToContainer(ctx context.Context, id string, path string, content io.Reader, options container.CopyToContainerOptions) error { +func (m *mockDockerClient) CopyToContainer(ctx context.Context, id, path string, content io.Reader, options container.CopyToContainerOptions) error { args := m.Called(ctx, id, path, content, options) return args.Error(0) } diff --git a/act/container/host_environment_test.go b/act/container/host_environment_test.go index 2614a2f8..85456288 100644 --- a/act/container/host_environment_test.go +++ b/act/container/host_environment_test.go @@ -28,10 +28,10 @@ func TestCopyDir(t *testing.T) { StdOut: os.Stdout, Workdir: path.Join("testdata", "scratch"), } - _ = os.MkdirAll(e.Path, 0700) - _ = os.MkdirAll(e.TmpDir, 0700) - _ = os.MkdirAll(e.ToolCache, 0700) - _ = os.MkdirAll(e.ActPath, 0700) + _ = os.MkdirAll(e.Path, 0o700) + _ = os.MkdirAll(e.TmpDir, 0o700) + _ = os.MkdirAll(e.ToolCache, 0o700) + _ = os.MkdirAll(e.ActPath, 0o700) err = e.CopyDir(e.Workdir, e.Path, true)(ctx) assert.NoError(t, err) } @@ -49,12 +49,12 @@ func TestGetContainerArchive(t *testing.T) { StdOut: os.Stdout, Workdir: path.Join("testdata", "scratch"), } - _ = os.MkdirAll(e.Path, 0700) - _ = os.MkdirAll(e.TmpDir, 0700) - _ = os.MkdirAll(e.ToolCache, 0700) - _ = os.MkdirAll(e.ActPath, 0700) + _ = os.MkdirAll(e.Path, 0o700) + _ = os.MkdirAll(e.TmpDir, 0o700) + _ = os.MkdirAll(e.ToolCache, 0o700) + _ = os.MkdirAll(e.ActPath, 0o700) expectedContent := []byte("sdde/7sh") - err = os.WriteFile(filepath.Join(e.Path, "action.yml"), expectedContent, 0600) + err = os.WriteFile(filepath.Join(e.Path, "action.yml"), expectedContent, 0o600) assert.NoError(t, err) archive, err := e.GetContainerArchive(ctx, e.Path) assert.NoError(t, err) diff --git a/act/exprparser/functions.go b/act/exprparser/functions.go index 83b2a080..74455476 100644 --- a/act/exprparser/functions.go +++ b/act/exprparser/functions.go @@ -137,7 +137,7 @@ func (impl *interperterImpl) format(str reflect.Value, replaceValue ...reflect.V return output, nil } -func (impl *interperterImpl) join(array reflect.Value, sep reflect.Value) (string, error) { +func (impl *interperterImpl) join(array, sep reflect.Value) (string, error) { separator := impl.coerceToString(sep).String() switch array.Kind() { case reflect.Slice: diff --git a/act/exprparser/interpreter.go b/act/exprparser/interpreter.go index 576ee45e..d040c41f 100644 --- a/act/exprparser/interpreter.go +++ b/act/exprparser/interpreter.go @@ -350,7 +350,7 @@ func (impl *interperterImpl) evaluateCompare(compareNode *actionlint.CompareOpNo return impl.compareValues(leftValue, rightValue, compareNode.Kind) } -func (impl *interperterImpl) compareValues(leftValue reflect.Value, rightValue reflect.Value, kind actionlint.CompareOpNodeKind) (interface{}, error) { +func (impl *interperterImpl) compareValues(leftValue, rightValue reflect.Value, kind actionlint.CompareOpNodeKind) (interface{}, error) { if leftValue.Kind() != rightValue.Kind() { if !impl.isNumber(leftValue) { leftValue = impl.coerceToNumber(leftValue) @@ -462,7 +462,7 @@ func (impl *interperterImpl) coerceToString(value reflect.Value) reflect.Value { return value } -func (impl *interperterImpl) compareString(left string, right string, kind actionlint.CompareOpNodeKind) (bool, error) { +func (impl *interperterImpl) compareString(left, right string, kind actionlint.CompareOpNodeKind) (bool, error) { switch kind { case actionlint.CompareOpNodeKindLess: return left < right, nil @@ -481,7 +481,7 @@ func (impl *interperterImpl) compareString(left string, right string, kind actio } } -func (impl *interperterImpl) compareNumber(left float64, right float64, kind actionlint.CompareOpNodeKind) (bool, error) { +func (impl *interperterImpl) compareNumber(left, right float64, kind actionlint.CompareOpNodeKind) (bool, error) { switch kind { case actionlint.CompareOpNodeKindLess: return left < right, nil diff --git a/act/filecollector/file_collector.go b/act/filecollector/file_collector.go index f40d4f45..535c3568 100644 --- a/act/filecollector/file_collector.go +++ b/act/filecollector/file_collector.go @@ -97,8 +97,7 @@ type Fs interface { Readlink(path string) (string, error) } -type DefaultFs struct { -} +type DefaultFs struct{} func (*DefaultFs) Walk(root string, fn filepath.WalkFunc) error { return filepath.Walk(root, fn) diff --git a/act/lookpath/env.go b/act/lookpath/env.go index dc376e7b..b7e61f8c 100644 --- a/act/lookpath/env.go +++ b/act/lookpath/env.go @@ -6,8 +6,7 @@ type Env interface { Getenv(name string) string } -type defaultEnv struct { -} +type defaultEnv struct{} func (*defaultEnv) Getenv(name string) string { return os.Getenv(name) diff --git a/act/lookpath/lp_plan9.go b/act/lookpath/lp_plan9.go index a201b715..18db2d88 100644 --- a/act/lookpath/lp_plan9.go +++ b/act/lookpath/lp_plan9.go @@ -20,7 +20,7 @@ func findExecutable(file string) error { if err != nil { return err } - if m := d.Mode(); !m.IsDir() && m&0111 != 0 { + if m := d.Mode(); !m.IsDir() && m&0o111 != 0 { return nil } return fs.ErrPermission diff --git a/act/lookpath/lp_unix.go b/act/lookpath/lp_unix.go index 233e21f9..f8bc60c5 100644 --- a/act/lookpath/lp_unix.go +++ b/act/lookpath/lp_unix.go @@ -22,7 +22,7 @@ func findExecutable(file string) error { if err != nil { return err } - if m := d.Mode(); !m.IsDir() && m&0111 != 0 { + if m := d.Mode(); !m.IsDir() && m&0o111 != 0 { return nil } return fs.ErrPermission diff --git a/act/model/github_context.go b/act/model/github_context.go index 71221a59..4dbcd15c 100644 --- a/act/model/github_context.go +++ b/act/model/github_context.go @@ -62,9 +62,9 @@ func nestedMapLookup(m map[string]interface{}, ks ...string) (rval interface{}) return rval } else if m, ok = rval.(map[string]interface{}); !ok { return nil - } else { // 1+ more keys - return nestedMapLookup(m, ks[1:]...) } + // 1+ more keys + return nestedMapLookup(m, ks[1:]...) } func withDefaultBranch(ctx context.Context, b string, event map[string]interface{}) map[string]interface{} { @@ -90,10 +90,12 @@ func withDefaultBranch(ctx context.Context, b string, event map[string]interface return event } -var findGitRef = git.FindGitRef -var findGitRevision = git.FindGitRevision +var ( + findGitRef = git.FindGitRef + findGitRevision = git.FindGitRevision +) -func (ghc *GithubContext) SetRef(ctx context.Context, defaultBranch string, repoPath string) { +func (ghc *GithubContext) SetRef(ctx context.Context, defaultBranch, repoPath string) { logger := common.Logger(ctx) // https://docs.github.com/en/actions/learn-github-actions/events-that-trigger-workflows @@ -164,7 +166,7 @@ func (ghc *GithubContext) SetSha(ctx context.Context, repoPath string) { } } -func (ghc *GithubContext) SetRepositoryAndOwner(ctx context.Context, githubInstance string, remoteName string, repoPath string) { +func (ghc *GithubContext) SetRepositoryAndOwner(ctx context.Context, githubInstance, remoteName, repoPath string) { if ghc.Repository == "" { repo, err := git.FindGithubRepo(ctx, repoPath, githubInstance, remoteName) if err != nil { diff --git a/act/model/workflow.go b/act/model/workflow.go index bca11dd0..a6f46e15 100644 --- a/act/model/workflow.go +++ b/act/model/workflow.go @@ -642,7 +642,7 @@ func (s *Step) GetEnv() map[string]string { // ShellCommand returns the command for the shell func (s *Step) ShellCommand() string { - shellCommand := "" + var shellCommand string // Reference: https://github.com/actions/runner/blob/8109c962f09d9acc473d92c595ff43afceddb347/src/Runner.Worker/Handlers/ScriptHandlerHelpers.cs#L9-L17 switch s.Shell { diff --git a/act/runner/action.go b/act/runner/action.go index 9ddbf5b4..2d42ce27 100644 --- a/act/runner/action.go +++ b/act/runner/action.go @@ -29,7 +29,7 @@ type actionStep interface { getCompositeSteps() *compositeSteps } -type readAction func(ctx context.Context, step *model.Step, actionDir string, actionPath string, readFile actionYamlReader, writeFile fileWriter) (*model.Action, error) +type readAction func(ctx context.Context, step *model.Step, actionDir, actionPath string, readFile actionYamlReader, writeFile fileWriter) (*model.Action, error) type actionYamlReader func(filename string) (io.Reader, io.Closer, error) @@ -40,7 +40,7 @@ type runAction func(step actionStep, actionDir string, remoteAction *remoteActio //go:embed res/trampoline.js var trampoline embed.FS -func readActionImpl(ctx context.Context, step *model.Step, actionDir string, actionPath string, readFile actionYamlReader, writeFile fileWriter) (*model.Action, error) { +func readActionImpl(ctx context.Context, step *model.Step, actionDir, actionPath string, readFile actionYamlReader, writeFile fileWriter) (*model.Action, error) { logger := common.Logger(ctx) allErrors := []error{} addError := func(fileName string, err error) { @@ -116,7 +116,7 @@ func readActionImpl(ctx context.Context, step *model.Step, actionDir string, act return action, err } -func maybeCopyToActionDir(ctx context.Context, step actionStep, actionDir string, actionPath string, containerActionDir string) error { +func maybeCopyToActionDir(ctx context.Context, step actionStep, actionDir, actionPath, containerActionDir string) error { logger := common.Logger(ctx) rc := step.getRunContext() stepModel := step.getStepModel() @@ -274,7 +274,7 @@ func removeGitIgnore(ctx context.Context, directory string) error { } // TODO: break out parts of function to reduce complexicity -func execAsDocker(ctx context.Context, step actionStep, actionName string, basedir string, localAction bool) error { +func execAsDocker(ctx context.Context, step actionStep, actionName, basedir string, localAction bool) error { logger := common.Logger(ctx) rc := step.getRunContext() action := step.getActionModel() @@ -402,7 +402,7 @@ func evalDockerArgs(ctx context.Context, step step, action *model.Action, cmd *[ } } -func newStepContainer(ctx context.Context, step step, image string, cmd []string, entrypoint []string) container.Container { +func newStepContainer(ctx context.Context, step step, image string, cmd, entrypoint []string) container.Container { rc := step.getRunContext() stepModel := step.getStepModel() rawLogger := common.Logger(ctx).WithField("raw_output", true) @@ -557,7 +557,7 @@ func runPreStep(step actionStep) common.Executor { actionPath = "" } - actionLocation := "" + var actionLocation string if actionPath != "" { actionLocation = path.Join(actionDir, actionPath) } else { @@ -608,7 +608,7 @@ func runPreStep(step actionStep) common.Executor { actionPath = "" } - actionLocation := "" + var actionLocation string if actionPath != "" { actionLocation = path.Join(actionDir, actionPath) } else { @@ -695,7 +695,7 @@ func runPostStep(step actionStep) common.Executor { actionPath = "" } - actionLocation := "" + var actionLocation string if actionPath != "" { actionLocation = path.Join(actionDir, actionPath) } else { diff --git a/act/runner/action_test.go b/act/runner/action_test.go index 36ee14f7..1145974e 100644 --- a/act/runner/action_test.go +++ b/act/runner/action_test.go @@ -122,7 +122,7 @@ runs: writeFile := func(filename string, data []byte, perm fs.FileMode) error { assert.Equal(t, "actionDir/actionPath/trampoline.js", filename) - assert.Equal(t, fs.FileMode(0400), perm) + assert.Equal(t, fs.FileMode(0o400), perm) return nil } diff --git a/act/runner/command.go b/act/runner/command.go index 9b59a974..9cca6b3a 100644 --- a/act/runner/command.go +++ b/act/runner/command.go @@ -8,8 +8,10 @@ import ( "github.com/nektos/act/pkg/common" ) -var commandPatternGA *regexp.Regexp -var commandPatternADO *regexp.Regexp +var ( + commandPatternGA *regexp.Regexp + commandPatternADO *regexp.Regexp +) func init() { commandPatternGA = regexp.MustCompile("^::([^ ]+)( (.+))?::([^\r\n]*)[\r\n]+$") @@ -28,7 +30,7 @@ func tryParseRawActionCommand(line string) (command string, kvPairs map[string]s arg = m[4] ok = true } - return + return command, kvPairs, arg, ok } func (rc *RunContext) commandHandler(ctx context.Context) common.LineHandler { @@ -101,6 +103,7 @@ func (rc *RunContext) setEnv(ctx context.Context, kvPairs map[string]string, arg mergeIntoMap(rc.Env, newenv) mergeIntoMap(rc.GlobalEnv, newenv) } + func (rc *RunContext) setOutput(ctx context.Context, kvPairs map[string]string, arg string) { logger := common.Logger(ctx) stepID := rc.CurrentStep @@ -119,6 +122,7 @@ func (rc *RunContext) setOutput(ctx context.Context, kvPairs map[string]string, logger.Infof(" \U00002699 ::set-output:: %s=%s", outputName, arg) result.Outputs[outputName] = arg } + func (rc *RunContext) addPath(ctx context.Context, arg string) { common.Logger(ctx).Infof(" \U00002699 ::add-path:: %s", arg) extraPath := []string{arg} @@ -130,7 +134,7 @@ func (rc *RunContext) addPath(ctx context.Context, arg string) { rc.ExtraPath = extraPath } -func parseKeyValuePairs(kvPairs string, separator string) map[string]string { +func parseKeyValuePairs(kvPairs, separator string) map[string]string { rtn := make(map[string]string) kvPairList := strings.Split(kvPairs, separator) for _, kvPair := range kvPairList { @@ -141,6 +145,7 @@ func parseKeyValuePairs(kvPairs string, separator string) map[string]string { } return rtn } + func unescapeCommandData(arg string) string { escapeMap := map[string]string{ "%25": "%", @@ -152,6 +157,7 @@ func unescapeCommandData(arg string) string { } return arg } + func unescapeCommandProperty(arg string) string { escapeMap := map[string]string{ "%25": "%", @@ -165,6 +171,7 @@ func unescapeCommandProperty(arg string) string { } return arg } + func unescapeKvPairs(kvPairs map[string]string) map[string]string { for k, v := range kvPairs { kvPairs[k] = unescapeCommandProperty(v) diff --git a/act/runner/container_mock_test.go b/act/runner/container_mock_test.go index 04d6261b..30e449dd 100644 --- a/act/runner/container_mock_test.go +++ b/act/runner/container_mock_test.go @@ -15,7 +15,7 @@ type containerMock struct { container.LinuxContainerEnvironmentExtensions } -func (cm *containerMock) Create(capAdd []string, capDrop []string) common.Executor { +func (cm *containerMock) Create(capAdd, capDrop []string) common.Executor { args := cm.Called(capAdd, capDrop) return args.Get(0).(func(context.Context) error) } @@ -55,7 +55,7 @@ func (cm *containerMock) Copy(destPath string, files ...*container.FileEntry) co return args.Get(0).(func(context.Context) error) } -func (cm *containerMock) CopyDir(destPath string, srcPath string, useGitIgnore bool) common.Executor { +func (cm *containerMock) CopyDir(destPath, srcPath string, useGitIgnore bool) common.Executor { args := cm.Called(destPath, srcPath, useGitIgnore) return args.Get(0).(func(context.Context) error) } diff --git a/act/runner/logger.go b/act/runner/logger.go index ef93a02b..297c8224 100644 --- a/act/runner/logger.go +++ b/act/runner/logger.go @@ -26,9 +26,11 @@ const ( gray = 37 ) -var colors []int -var nextColor int -var mux sync.Mutex +var ( + colors []int + nextColor int + mux sync.Mutex +) func init() { nextColor = 0 @@ -70,7 +72,7 @@ func WithJobLoggerFactory(ctx context.Context, factory JobLoggerFactory) context } // WithJobLogger attaches a new logger to context that is aware of steps -func WithJobLogger(ctx context.Context, jobID string, jobName string, config *Config, masks *[]string, matrix map[string]interface{}) context.Context { +func WithJobLogger(ctx context.Context, jobID, jobName string, config *Config, masks *[]string, matrix map[string]interface{}) context.Context { ctx = WithMasks(ctx, masks) var logger *logrus.Logger diff --git a/act/runner/reusable_workflow.go b/act/runner/reusable_workflow.go index 312d4150..914a10c7 100644 --- a/act/runner/reusable_workflow.go +++ b/act/runner/reusable_workflow.go @@ -113,9 +113,7 @@ func newActionCacheReusableWorkflowExecutor(rc *RunContext, filename string, rem } } -var ( - executorLock sync.Mutex -) +var executorLock sync.Mutex func newMutexExecutor(executor common.Executor) common.Executor { return func(ctx context.Context) error { @@ -150,7 +148,7 @@ func cloneIfRequired(rc *RunContext, remoteReusableWorkflow remoteReusableWorkfl ) } -func newReusableWorkflowExecutor(rc *RunContext, directory string, workflow string) common.Executor { +func newReusableWorkflowExecutor(rc *RunContext, directory, workflow string) common.Executor { return func(ctx context.Context) error { planner, err := model.NewWorkflowPlanner(path.Join(directory, workflow), true, false) if err != nil { diff --git a/act/runner/run_context.go b/act/runner/run_context.go index 2f0cce32..492fb4de 100644 --- a/act/runner/run_context.go +++ b/act/runner/run_context.go @@ -263,7 +263,7 @@ func (rc *RunContext) stopHostEnvironment(ctx context.Context) error { return common.NewPipelineExecutor( rc.JobContainer.Copy(rc.JobContainer.GetActPath()+"/", &container.FileEntry{ Name: "workflow/stop-lxc.sh", - Mode: 0755, + Mode: 0o755, Body: stopScript.String(), }), rc.JobContainer.Exec([]string{rc.JobContainer.GetActPath() + "/workflow/stop-lxc.sh"}, map[string]string{}, "root", "/tmp"), @@ -360,17 +360,17 @@ func (rc *RunContext) startHostEnvironment() common.Executor { executors = append(executors, rc.JobContainer.Copy(rc.JobContainer.GetActPath()+"/", &container.FileEntry{ Name: "workflow/lxc-helpers-lib.sh", - Mode: 0755, + Mode: 0o755, Body: lxcHelpersLib, }), rc.JobContainer.Copy(rc.JobContainer.GetActPath()+"/", &container.FileEntry{ Name: "workflow/lxc-helpers.sh", - Mode: 0755, + Mode: 0o755, Body: lxcHelpers, }), rc.JobContainer.Copy(rc.JobContainer.GetActPath()+"/", &container.FileEntry{ Name: "workflow/start-lxc.sh", - Mode: 0755, + Mode: 0o755, Body: startScript.String(), }), rc.JobContainer.Exec([]string{rc.JobContainer.GetActPath() + "/workflow/start-lxc.sh"}, map[string]string{}, "root", "/tmp"), @@ -819,7 +819,7 @@ func (rc *RunContext) IsLXCHostEnv(ctx context.Context) bool { func (rc *RunContext) GetLXCInfo(ctx context.Context) (isLXC bool, template, release, config string) { platform := rc.runsOnImage(ctx) if !strings.HasPrefix(platform, lxcPrefix) { - return + return isLXC, template, release, config } isLXC = true s := strings.SplitN(strings.TrimPrefix(platform, lxcPrefix), ":", 3) @@ -830,7 +830,7 @@ func (rc *RunContext) GetLXCInfo(ctx context.Context) (isLXC bool, template, rel if len(s) > 2 { config = s[2] } - return + return isLXC, template, release, config } func (rc *RunContext) IsHostEnv(ctx context.Context) bool { @@ -1247,9 +1247,9 @@ func nestedMapLookup(m map[string]interface{}, ks ...string) (rval interface{}) return rval } else if m, ok = rval.(map[string]interface{}); !ok { return nil - } else { // 1+ more keys - return nestedMapLookup(m, ks[1:]...) } + // 1+ more keys + return nestedMapLookup(m, ks[1:]...) } func (rc *RunContext) withGithubEnv(ctx context.Context, github *model.GithubContext, env map[string]string) map[string]string { @@ -1365,25 +1365,25 @@ func (rc *RunContext) handleCredentials(ctx context.Context) (string, string, er func (rc *RunContext) handleServiceCredentials(ctx context.Context, creds map[string]string) (username, password string, err error) { if creds == nil { - return + return username, password, err } if len(creds) != 2 { err = fmt.Errorf("invalid property count for key 'credentials:'") - return + return username, password, err } ee := rc.NewExpressionEvaluator(ctx) if username = ee.Interpolate(ctx, creds["username"]); username == "" { err = fmt.Errorf("failed to interpolate credentials.username") - return + return username, password, err } if password = ee.Interpolate(ctx, creds["password"]); password == "" { err = fmt.Errorf("failed to interpolate credentials.password") - return + return username, password, err } - return + return username, password, err } // GetServiceBindsAndMounts returns the binds and mounts for the service container, resolving paths as appopriate diff --git a/act/runner/step.go b/act/runner/step.go index 411bbe14..da230c37 100644 --- a/act/runner/step.go +++ b/act/runner/step.go @@ -139,7 +139,7 @@ func runStepExecutor(step step, stage stepStage, executor common.Executor) commo Mode: 0o666, }, &container.FileEntry{ Name: envFileCommand, - Mode: 0666, + Mode: 0o666, }, &container.FileEntry{ Name: summaryFileCommand, Mode: 0o666, diff --git a/act/runner/step_action_local_test.go b/act/runner/step_action_local_test.go index c4b63459..796b96a4 100644 --- a/act/runner/step_action_local_test.go +++ b/act/runner/step_action_local_test.go @@ -24,7 +24,7 @@ func (salm *stepActionLocalMocks) runAction(step actionStep, actionDir string, r return args.Get(0).(func(context.Context) error) } -func (salm *stepActionLocalMocks) readAction(_ context.Context, step *model.Step, actionDir string, actionPath string, readFile actionYamlReader, writeFile fileWriter) (*model.Action, error) { +func (salm *stepActionLocalMocks) readAction(_ context.Context, step *model.Step, actionDir, actionPath string, readFile actionYamlReader, writeFile fileWriter) (*model.Action, error) { args := salm.Called(step, actionDir, actionPath, readFile, writeFile) return args.Get(0).(*model.Action), args.Error(1) } diff --git a/act/runner/step_docker.go b/act/runner/step_docker.go index bbf46e01..fb0c52a3 100644 --- a/act/runner/step_docker.go +++ b/act/runner/step_docker.go @@ -85,11 +85,9 @@ func (sd *stepDocker) runUsesContainer() common.Executor { } } -var ( - ContainerNewContainer = container.NewContainer -) +var ContainerNewContainer = container.NewContainer -func (sd *stepDocker) newStepContainer(ctx context.Context, image string, cmd []string, entrypoint []string) container.Container { +func (sd *stepDocker) newStepContainer(ctx context.Context, image string, cmd, entrypoint []string) container.Container { rc := sd.RunContext step := sd.Step diff --git a/act/runner/step_run.go b/act/runner/step_run.go index e4d32e59..7089b9e0 100644 --- a/act/runner/step_run.go +++ b/act/runner/step_run.go @@ -195,16 +195,16 @@ func (sr *stepRun) setupShell(ctx context.Context) { step.Shell = shellWithFallback[1] } } else { - shell_fallback := ` + shellFallback := ` if command -v bash >/dev/null; then echo -n bash else echo -n sh fi ` - stdout, _, err := rc.sh(ctx, shell_fallback) + stdout, _, err := rc.sh(ctx, shellFallback) if err != nil { - common.Logger(ctx).Error("fail to run %q: %v", shell_fallback, err) + common.Logger(ctx).Error("fail to run %q: %v", shellFallback, err) return } step.Shell = stdout @@ -215,7 +215,7 @@ fi func (sr *stepRun) setupWorkingDirectory(ctx context.Context) { rc := sr.RunContext step := sr.Step - workingdirectory := "" + var workingdirectory string if step.WorkingDirectory == "" { workingdirectory = rc.Run.Job().Defaults.Run.WorkingDirectory diff --git a/act/schema/schema.go b/act/schema/schema.go index a523e721..71f474db 100644 --- a/act/schema/schema.go +++ b/act/schema/schema.go @@ -89,14 +89,11 @@ type StringDefinition struct { IsExpression bool `json:"is-expression"` } -type NumberDefinition struct { -} +type NumberDefinition struct{} -type BooleanDefinition struct { -} +type BooleanDefinition struct{} -type NullDefinition struct { -} +type NullDefinition struct{} func GetWorkflowSchema() *Schema { sh := &Schema{}