From 6ed2c6e93111348bff84c32044a8a858b5d6cc90 Mon Sep 17 00:00:00 2001 From: Amardeep Chawla Date: Thu, 16 Mar 2023 11:01:23 -0700 Subject: [PATCH 1/7] Add wrapper for execute command --- go.mod | 2 +- go.sum | 4 +- src/core/environment.go | 22 ++++++- src/core/environment_test.go | 62 +++++++++++++++---- .../nginx/agent/v2/src/core/environment.go | 22 ++++++- .../evilmartians/lefthook/CHANGELOG.md | 5 ++ .../github.com/evilmartians/lefthook/Makefile | 8 +++ .../internal/config/available_hooks.go | 4 ++ .../lefthook/runner/prepare_command.go | 34 +++++++--- .../internal/lefthook/runner/runner.go | 22 +++++-- .../lefthook/internal/version/version.go | 2 +- vendor/modules.txt | 2 +- 12 files changed, 156 insertions(+), 33 deletions(-) diff --git a/go.mod b/go.mod index b4c601acb..661c1b1c4 100644 --- a/go.mod +++ b/go.mod @@ -39,7 +39,7 @@ require ( require ( github.com/bufbuild/buf v1.15.1 - github.com/evilmartians/lefthook v1.3.5 + github.com/evilmartians/lefthook v1.3.6 github.com/go-resty/resty/v2 v2.7.0 github.com/go-swagger/go-swagger v0.30.4 github.com/goreleaser/nfpm/v2 v2.26.0 diff --git a/go.sum b/go.sum index 1778b4093..4c43d7010 100644 --- a/go.sum +++ b/go.sum @@ -147,8 +147,8 @@ github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.m github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= github.com/envoyproxy/protoc-gen-validate v0.3.0-java h1:bV5JGEB1ouEzZa0hgVDFFiClrUEuGWRaAc/3mxR2QK0= github.com/envoyproxy/protoc-gen-validate v0.3.0-java/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= -github.com/evilmartians/lefthook v1.3.5 h1:K+wkFYtf1+oNe/gguNEGly1jXlUmfYIMYaZ7A43QZjQ= -github.com/evilmartians/lefthook v1.3.5/go.mod h1:X4o0AX2Jh9SVFyxyDMoJb6p8LmzzqGGfNkrcg1FiDt8= +github.com/evilmartians/lefthook v1.3.6 h1:xeSQszyJ4eJ/LPbg3zyH7jg6sK01sS0tJEnXT9OOi50= +github.com/evilmartians/lefthook v1.3.6/go.mod h1:X4o0AX2Jh9SVFyxyDMoJb6p8LmzzqGGfNkrcg1FiDt8= github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= github.com/fatih/color v1.14.1 h1:qfhVLaG5s+nCROl1zJsZRxFeYrHLqWroPOQ8BWiNb4w= github.com/fatih/color v1.14.1/go.mod h1:2oHN61fhTpgcxD3TSWCgKDiH1+x4OiDVVGH8WlgGZGg= diff --git a/src/core/environment.go b/src/core/environment.go index 7a6f08fe1..8a13a03b2 100644 --- a/src/core/environment.go +++ b/src/core/environment.go @@ -96,6 +96,8 @@ var ( _ Environment = &EnvironmentType{} ) +var currentShellCommander = execShellCommander + func (env *EnvironmentType) NewHostInfo(agentVersion string, tags *[]string, configDirs string, clearCache bool) *proto.HostInfo { // temp cache measure if env.host == nil || clearCache { @@ -654,15 +656,31 @@ func processorCache(item cpu.InfoStat) map[string]string { return cache } +type IExecShellCommander interface { + Output() ([]byte, error) +} + +type execShellCommand struct { + *exec.Cmd +} + +func execShellCommander(name string, arg ...string) IExecShellCommander { + execCmd := exec.Command(name, arg...) + return execShellCommand{Cmd: execCmd} +} + func getProcessorCacheInfo(cpuInfo cpuid.CPUInfo) map[string]string { cache := getDefaultProcessorCacheInfo(cpuInfo) + return getCacheInfo(cache) +} - out, err := exec.Command("lscpu").Output() +func getCacheInfo(cache map[string]string) map[string]string { + cmd := currentShellCommander("lscpu") + out, err := cmd.Output() if err != nil { log.Warnf("Install lscpu on host to get processor info: %v", err) return cache } - return parseLscpu(string(out), cache) } diff --git a/src/core/environment_test.go b/src/core/environment_test.go index 913de5fa0..618bfe035 100644 --- a/src/core/environment_test.go +++ b/src/core/environment_test.go @@ -8,6 +8,7 @@ package core import ( + "errors" "io/ioutil" "os" "strings" @@ -469,17 +470,55 @@ func TestProcessors(t *testing.T) { assert.Equal(t, "arm64", processorInfo[0].GetArchitecture()) } -func TestParseLscpu(t *testing.T) { +type mockShellCommand struct { + OutputFunc func() ([]byte, error) +} + +func (msc mockShellCommand) Output() ([]byte, error) { + return msc.OutputFunc() +} + +type testExecShellCommander func(name string, arg ...string) IExecShellCommander + +func mockExecShellCommander(output string, err error) testExecShellCommander { + return func(name string, arg ...string) IExecShellCommander { + outputFunc := func() ([]byte, error) { + return []byte(output), err + } + return mockShellCommand{ + OutputFunc: outputFunc, + } + } +} + +func TestGetCacheInfo(t *testing.T) { + tempShellCommander := currentShellCommander + defer func() { currentShellCommander = tempShellCommander }() tests := []struct { - name string - defaultCacheInfo map[string]string - cpuInfoCache proto.CpuInfo - lscpuContent string - expect map[string]string + name string + mockExecShellCommander testExecShellCommander + defaultCacheInfo map[string]string + expect map[string]string }{ { - name: "os-release present", - lscpuContent: lscpuInfo1, + name: "lscpu error", + mockExecShellCommander: mockExecShellCommander("", errors.New("Error executing lscpu")), + defaultCacheInfo: map[string]string{ + "L1d": "64 KiB", + "L1i": "96 KiB", + "L2": "2 MiB", + "L3": "1 MiB", + }, + expect: map[string]string{ + "L1d": "64 KiB", + "L1i": "96 KiB", + "L2": "2 MiB", + "L3": "1 MiB", + }, + }, + { + name: "default cache info absent", + mockExecShellCommander: mockExecShellCommander(lscpuInfo1, nil), defaultCacheInfo: map[string]string{ "L1d": "-1", "L1i": "-1", @@ -494,8 +533,8 @@ func TestParseLscpu(t *testing.T) { }, }, { - name: "os-release present with quote", - lscpuContent: lscpuInfo2, + name: "os-release present with quote", + mockExecShellCommander: mockExecShellCommander(lscpuInfo2, nil), defaultCacheInfo: map[string]string{ "L1d": "-1", "L1i": "-1", @@ -512,7 +551,8 @@ func TestParseLscpu(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - actual := parseLscpu(tt.lscpuContent, tt.defaultCacheInfo) + currentShellCommander = tt.mockExecShellCommander + actual := getCacheInfo(tt.defaultCacheInfo) assert.Equal(t, tt.expect, actual) }) } diff --git a/test/performance/vendor/github.com/nginx/agent/v2/src/core/environment.go b/test/performance/vendor/github.com/nginx/agent/v2/src/core/environment.go index 7a6f08fe1..8a13a03b2 100644 --- a/test/performance/vendor/github.com/nginx/agent/v2/src/core/environment.go +++ b/test/performance/vendor/github.com/nginx/agent/v2/src/core/environment.go @@ -96,6 +96,8 @@ var ( _ Environment = &EnvironmentType{} ) +var currentShellCommander = execShellCommander + func (env *EnvironmentType) NewHostInfo(agentVersion string, tags *[]string, configDirs string, clearCache bool) *proto.HostInfo { // temp cache measure if env.host == nil || clearCache { @@ -654,15 +656,31 @@ func processorCache(item cpu.InfoStat) map[string]string { return cache } +type IExecShellCommander interface { + Output() ([]byte, error) +} + +type execShellCommand struct { + *exec.Cmd +} + +func execShellCommander(name string, arg ...string) IExecShellCommander { + execCmd := exec.Command(name, arg...) + return execShellCommand{Cmd: execCmd} +} + func getProcessorCacheInfo(cpuInfo cpuid.CPUInfo) map[string]string { cache := getDefaultProcessorCacheInfo(cpuInfo) + return getCacheInfo(cache) +} - out, err := exec.Command("lscpu").Output() +func getCacheInfo(cache map[string]string) map[string]string { + cmd := currentShellCommander("lscpu") + out, err := cmd.Output() if err != nil { log.Warnf("Install lscpu on host to get processor info: %v", err) return cache } - return parseLscpu(string(out), cache) } diff --git a/vendor/github.com/evilmartians/lefthook/CHANGELOG.md b/vendor/github.com/evilmartians/lefthook/CHANGELOG.md index fcda36318..268dae4c9 100644 --- a/vendor/github.com/evilmartians/lefthook/CHANGELOG.md +++ b/vendor/github.com/evilmartians/lefthook/CHANGELOG.md @@ -2,6 +2,11 @@ ## master (unreleased) +## 1.3.6 (2024-03-16) + +- fix: stage fixed when root specified ([#449](https://github.com/evilmartians/lefthook/pull/449)) by @mrexox +- feat: implitic skip on missing files for pre-commit and pre-push hooks ([#448](https://github.com/evilmartians/lefthook/pull/448)) by @mrexox + ## 1.3.5 (2024-03-15) - feat: add stage_fixed option ([#445](https://github.com/evilmartians/lefthook/pull/445)) by @mrexox diff --git a/vendor/github.com/evilmartians/lefthook/Makefile b/vendor/github.com/evilmartians/lefthook/Makefile index 0fa5a5920..e246c64e2 100644 --- a/vendor/github.com/evilmartians/lefthook/Makefile +++ b/vendor/github.com/evilmartians/lefthook/Makefile @@ -15,3 +15,11 @@ bin/golangci-lint: lint: bin/golangci-lint $$(go env GOPATH)/bin/golangci-lint run + +.ONESHELL: +version: + @read -p "New version: " version + sed -i "s/const version = .*/const version = \"$$version\"/" internal/version/version.go + sed -i "s/VERSION := .*/VERSION := $$version/" packaging/Makefile + make -C packaging clean set-version + git add internal/version/version.go packaging/* diff --git a/vendor/github.com/evilmartians/lefthook/internal/config/available_hooks.go b/vendor/github.com/evilmartians/lefthook/internal/config/available_hooks.go index 6633948fd..cdd46157a 100644 --- a/vendor/github.com/evilmartians/lefthook/internal/config/available_hooks.go +++ b/vendor/github.com/evilmartians/lefthook/internal/config/available_hooks.go @@ -43,6 +43,10 @@ func HookUsesStagedFiles(hook string) bool { return hook == "pre-commit" } +func HookUsesPushFiles(hook string) bool { + return hook == "pre-push" +} + func HookAvailable(hook string) bool { for _, name := range AvailableHooks { if name == hook { diff --git a/vendor/github.com/evilmartians/lefthook/internal/lefthook/runner/prepare_command.go b/vendor/github.com/evilmartians/lefthook/internal/lefthook/runner/prepare_command.go index e33ce459f..e4bcec376 100644 --- a/vendor/github.com/evilmartians/lefthook/internal/lefthook/runner/prepare_command.go +++ b/vendor/github.com/evilmartians/lefthook/internal/lefthook/runner/prepare_command.go @@ -34,19 +34,19 @@ func (r *Runner) prepareCommand(name string, command *config.Command) (*commandA return nil, errors.New("invalid conig") } - args, err := r.buildCommandArgs(command) + args, err, skipReason := r.buildCommandArgs(command) if err != nil { log.Error(err) return nil, errors.New("error") } - if args == nil || len(args.all) == 0 { - return nil, errors.New("no files for inspection") + if skipReason != nil { + return nil, skipReason } return args, nil } -func (r *Runner) buildCommandArgs(command *config.Command) (*commandArgs, error) { +func (r *Runner) buildCommandArgs(command *config.Command) (*commandArgs, error, error) { filesCommand := r.Hook.Files if command.Files != "" { filesCommand = command.Files @@ -72,21 +72,39 @@ func (r *Runner) buildCommandArgs(command *config.Command) (*commandArgs, error) filesCommand != "" && filesType == config.SubFiles { files, err := filesFn() if err != nil { - return nil, fmt.Errorf("error replacing %s: %w", filesType, err) + return nil, fmt.Errorf("error replacing %s: %w", filesType, err), nil } if len(files) == 0 { - return nil, nil + return nil, nil, errors.New("no files for inspection") } filesPrepared := prepareFiles(command, files) if len(filesPrepared) == 0 { - return nil, nil + return nil, nil, errors.New("no files for inspection") } filteredFiles = append(filteredFiles, filesPrepared...) runString = replaceQuoted(runString, filesType, filesPrepared) } } + if len(filteredFiles) == 0 && config.HookUsesStagedFiles(r.HookName) { + files, err := r.Repo.StagedFiles() + if err == nil { + if len(prepareFiles(command, files)) == 0 { + return nil, nil, errors.New("no matching staged files") + } + } + } + + if len(filteredFiles) == 0 && config.HookUsesPushFiles(r.HookName) { + files, err := r.Repo.PushFiles() + if err == nil { + if len(prepareFiles(command, files)) == 0 { + return nil, nil, errors.New("no matching push files") + } + } + } + runString = strings.ReplaceAll(runString, "{0}", strings.Join(r.GitArgs, " ")) for i, gitArg := range r.GitArgs { runString = strings.ReplaceAll(runString, fmt.Sprintf("{%d}", i+1), gitArg) @@ -97,7 +115,7 @@ func (r *Runner) buildCommandArgs(command *config.Command) (*commandArgs, error) return &commandArgs{ files: filteredFiles, all: strings.Split(runString, " "), - }, nil + }, nil, nil } func prepareFiles(command *config.Command, files []string) []string { diff --git a/vendor/github.com/evilmartians/lefthook/internal/lefthook/runner/runner.go b/vendor/github.com/evilmartians/lefthook/internal/lefthook/runner/runner.go index fc915b517..35fc4840e 100644 --- a/vendor/github.com/evilmartians/lefthook/internal/lefthook/runner/runner.go +++ b/vendor/github.com/evilmartians/lefthook/internal/lefthook/runner/runner.go @@ -203,6 +203,10 @@ func (r *Runner) preHook() { } func (r *Runner) postHook() { + if !config.HookUsesStagedFiles(r.HookName) { + return + } + if err := r.Repo.RestoreUnstaged(); err != nil { log.Warnf("Couldn't restore hidden unstaged files: %s\n", err) return @@ -295,9 +299,7 @@ func (r *Runner) runScript(script *config.Script, path string, file os.FileInfo) return } - if err := r.Repo.AddFiles(files); err != nil { - log.Warn("Couldn't stage fixed files:", err) - } + r.addStagedFiles(files) } } @@ -378,9 +380,19 @@ func (r *Runner) runCommand(name string, command *config.Command) { files = prepareFiles(command, stagedFiles) } - if err := r.Repo.AddFiles(files); err != nil { - log.Warn("Couldn't stage fixed files:", err) + if len(command.Root) > 0 { + for i, file := range files { + files[i] = filepath.Join(command.Root, file) + } } + + r.addStagedFiles(files) + } +} + +func (r *Runner) addStagedFiles(files []string) { + if err := r.Repo.AddFiles(files); err != nil { + log.Warn("Couldn't stage fixed files:", err) } } diff --git a/vendor/github.com/evilmartians/lefthook/internal/version/version.go b/vendor/github.com/evilmartians/lefthook/internal/version/version.go index 8d9f490fc..84bcf09db 100644 --- a/vendor/github.com/evilmartians/lefthook/internal/version/version.go +++ b/vendor/github.com/evilmartians/lefthook/internal/version/version.go @@ -6,7 +6,7 @@ import ( "strconv" ) -const version = "1.3.5" +const version = "1.3.6" var ( // Is set via -X github.com/evilmartians/lefthook/internal/version.commit={commit}. diff --git a/vendor/modules.txt b/vendor/modules.txt index 4d0a77f02..0b7632e95 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -333,7 +333,7 @@ github.com/emirpasic/gods/utils # github.com/envoyproxy/protoc-gen-validate v0.3.0-java ## explicit github.com/envoyproxy/protoc-gen-validate/validate -# github.com/evilmartians/lefthook v1.3.5 +# github.com/evilmartians/lefthook v1.3.6 ## explicit; go 1.19 github.com/evilmartians/lefthook github.com/evilmartians/lefthook/cmd From 48c5cb153ca93e685f713977f31448160e12824e Mon Sep 17 00:00:00 2001 From: Amardeep Chawla Date: Wed, 22 Mar 2023 09:06:13 -0700 Subject: [PATCH 2/7] add comments for reviews --- src/core/environment.go | 15 ++++---- src/core/environment_test.go | 66 ++++++++++++++++++++---------------- 2 files changed, 44 insertions(+), 37 deletions(-) diff --git a/src/core/environment.go b/src/core/environment.go index 8a13a03b2..d3f09eef6 100644 --- a/src/core/environment.go +++ b/src/core/environment.go @@ -96,7 +96,6 @@ var ( _ Environment = &EnvironmentType{} ) -var currentShellCommander = execShellCommander func (env *EnvironmentType) NewHostInfo(agentVersion string, tags *[]string, configDirs string, clearCache bool) *proto.HostInfo { // temp cache measure @@ -656,27 +655,27 @@ func processorCache(item cpu.InfoStat) map[string]string { return cache } -type IExecShellCommander interface { - Output() ([]byte, error) +type ShellCommander interface { + Exec(cmd string, arg ...string) ([]byte, error) } type execShellCommand struct { - *exec.Cmd } -func execShellCommander(name string, arg ...string) IExecShellCommander { +func (e execShellCommand) Exec(cmd string, arg ...string) ([]byte, error) { execCmd := exec.Command(name, arg...) - return execShellCommand{Cmd: execCmd} + return execCmd.Output() } +var shell = execShellCommand + func getProcessorCacheInfo(cpuInfo cpuid.CPUInfo) map[string]string { cache := getDefaultProcessorCacheInfo(cpuInfo) return getCacheInfo(cache) } func getCacheInfo(cache map[string]string) map[string]string { - cmd := currentShellCommander("lscpu") - out, err := cmd.Output() + out, err := shell.Exec("lscpu") if err != nil { log.Warnf("Install lscpu on host to get processor info: %v", err) return cache diff --git a/src/core/environment_test.go b/src/core/environment_test.go index 618bfe035..2c1ddf4aa 100644 --- a/src/core/environment_test.go +++ b/src/core/environment_test.go @@ -9,6 +9,7 @@ package core import ( "errors" + "fmt" "io/ioutil" "os" "strings" @@ -470,39 +471,38 @@ func TestProcessors(t *testing.T) { assert.Equal(t, "arm64", processorInfo[0].GetArchitecture()) } -type mockShellCommand struct { - OutputFunc func() ([]byte, error) +type fakeShell struct { + output map[string]string + errors map[string]error } -func (msc mockShellCommand) Output() ([]byte, error) { - return msc.OutputFunc() -} - -type testExecShellCommander func(name string, arg ...string) IExecShellCommander - -func mockExecShellCommander(output string, err error) testExecShellCommander { - return func(name string, arg ...string) IExecShellCommander { - outputFunc := func() ([]byte, error) { - return []byte(output), err - } - return mockShellCommand{ - OutputFunc: outputFunc, - } +func (f *fakeShell) Exec(cmd string, arg ...string) ([]byte, error) { + key := strings.Join(append([]string{cmd}, arg...), " ") + if err, ok := f.errors[key]; ok { + return nil, err } + if out, ok := f.output[key]; ok { + return []byte(out), nil + } + return nil, fmt.Errorf("unexpected command %s", key) } func TestGetCacheInfo(t *testing.T) { - tempShellCommander := currentShellCommander - defer func() { currentShellCommander = tempShellCommander }() + tempShellCommander := shell + defer func() { shell = tempShellCommander }() tests := []struct { - name string - mockExecShellCommander testExecShellCommander - defaultCacheInfo map[string]string - expect map[string]string + name string + shell ShellCommander + defaultCacheInfo map[string]string + expect map[string]string }{ { - name: "lscpu error", - mockExecShellCommander: mockExecShellCommander("", errors.New("Error executing lscpu")), + name: "lscpu error", + shell: &fakeShell{ + errors: map[string]error{ + "lscpu": errors.New("nope"), + }, + }, defaultCacheInfo: map[string]string{ "L1d": "64 KiB", "L1i": "96 KiB", @@ -517,8 +517,12 @@ func TestGetCacheInfo(t *testing.T) { }, }, { - name: "default cache info absent", - mockExecShellCommander: mockExecShellCommander(lscpuInfo1, nil), + name: "default cache info absent", + shell: &fakeShell{ + output: map[string]string{ + "lscpu": lscpuInfo1, + }, + }, defaultCacheInfo: map[string]string{ "L1d": "-1", "L1i": "-1", @@ -533,8 +537,12 @@ func TestGetCacheInfo(t *testing.T) { }, }, { - name: "os-release present with quote", - mockExecShellCommander: mockExecShellCommander(lscpuInfo2, nil), + name: "os-release present with quote", + shell: &fakeShell{ + output: map[string]string{ + "lscpu": lscpuInfo2, + }, + }, defaultCacheInfo: map[string]string{ "L1d": "-1", "L1i": "-1", @@ -551,7 +559,7 @@ func TestGetCacheInfo(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - currentShellCommander = tt.mockExecShellCommander + shell = tt.shell actual := getCacheInfo(tt.defaultCacheInfo) assert.Equal(t, tt.expect, actual) }) From 9b2d7a9385112d77d475d119f0320a33b5396b37 Mon Sep 17 00:00:00 2001 From: Amardeep Chawla Date: Thu, 23 Mar 2023 10:02:17 -0700 Subject: [PATCH 3/7] Make deps --- src/core/environment.go | 3 +-- .../nginx/agent/v2/src/core/environment.go | 16 +++++++--------- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/src/core/environment.go b/src/core/environment.go index d3f09eef6..bdc07c05d 100644 --- a/src/core/environment.go +++ b/src/core/environment.go @@ -96,7 +96,6 @@ var ( _ Environment = &EnvironmentType{} ) - func (env *EnvironmentType) NewHostInfo(agentVersion string, tags *[]string, configDirs string, clearCache bool) *proto.HostInfo { // temp cache measure if env.host == nil || clearCache { @@ -667,7 +666,7 @@ func (e execShellCommand) Exec(cmd string, arg ...string) ([]byte, error) { return execCmd.Output() } -var shell = execShellCommand +var shell ShellCommander = execShellCommand{} func getProcessorCacheInfo(cpuInfo cpuid.CPUInfo) map[string]string { cache := getDefaultProcessorCacheInfo(cpuInfo) diff --git a/test/performance/vendor/github.com/nginx/agent/v2/src/core/environment.go b/test/performance/vendor/github.com/nginx/agent/v2/src/core/environment.go index 8a13a03b2..bdc07c05d 100644 --- a/test/performance/vendor/github.com/nginx/agent/v2/src/core/environment.go +++ b/test/performance/vendor/github.com/nginx/agent/v2/src/core/environment.go @@ -96,8 +96,6 @@ var ( _ Environment = &EnvironmentType{} ) -var currentShellCommander = execShellCommander - func (env *EnvironmentType) NewHostInfo(agentVersion string, tags *[]string, configDirs string, clearCache bool) *proto.HostInfo { // temp cache measure if env.host == nil || clearCache { @@ -656,27 +654,27 @@ func processorCache(item cpu.InfoStat) map[string]string { return cache } -type IExecShellCommander interface { - Output() ([]byte, error) +type ShellCommander interface { + Exec(cmd string, arg ...string) ([]byte, error) } type execShellCommand struct { - *exec.Cmd } -func execShellCommander(name string, arg ...string) IExecShellCommander { +func (e execShellCommand) Exec(cmd string, arg ...string) ([]byte, error) { execCmd := exec.Command(name, arg...) - return execShellCommand{Cmd: execCmd} + return execCmd.Output() } +var shell ShellCommander = execShellCommand{} + func getProcessorCacheInfo(cpuInfo cpuid.CPUInfo) map[string]string { cache := getDefaultProcessorCacheInfo(cpuInfo) return getCacheInfo(cache) } func getCacheInfo(cache map[string]string) map[string]string { - cmd := currentShellCommander("lscpu") - out, err := cmd.Output() + out, err := shell.Exec("lscpu") if err != nil { log.Warnf("Install lscpu on host to get processor info: %v", err) return cache From d7cf01b45b8c5d130db6e5d20d898cff94724349 Mon Sep 17 00:00:00 2001 From: Amardeep Chawla Date: Thu, 23 Mar 2023 10:10:12 -0700 Subject: [PATCH 4/7] Test --- src/core/environment.go | 4 ++-- src/core/environment_test.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/core/environment.go b/src/core/environment.go index bdc07c05d..fce19893c 100644 --- a/src/core/environment.go +++ b/src/core/environment.go @@ -654,7 +654,7 @@ func processorCache(item cpu.InfoStat) map[string]string { return cache } -type ShellCommander interface { +type Shell interface { Exec(cmd string, arg ...string) ([]byte, error) } @@ -666,7 +666,7 @@ func (e execShellCommand) Exec(cmd string, arg ...string) ([]byte, error) { return execCmd.Output() } -var shell ShellCommander = execShellCommand{} +var shell Shell = execShellCommand{} func getProcessorCacheInfo(cpuInfo cpuid.CPUInfo) map[string]string { cache := getDefaultProcessorCacheInfo(cpuInfo) diff --git a/src/core/environment_test.go b/src/core/environment_test.go index 2c1ddf4aa..ce55abb43 100644 --- a/src/core/environment_test.go +++ b/src/core/environment_test.go @@ -492,7 +492,7 @@ func TestGetCacheInfo(t *testing.T) { defer func() { shell = tempShellCommander }() tests := []struct { name string - shell ShellCommander + shell Shell defaultCacheInfo map[string]string expect map[string]string }{ From 833b872c4958a994d2f7ae93f9a977ff4a1f5a05 Mon Sep 17 00:00:00 2001 From: Amardeep Chawla Date: Thu, 23 Mar 2023 10:17:15 -0700 Subject: [PATCH 5/7] make deps after main merge --- go.sum | 2 -- vendor/github.com/evilmartians/lefthook/CHANGELOG.md | 4 ++++ .../evilmartians/lefthook/internal/templates/hook.tmpl | 2 +- .../evilmartians/lefthook/internal/version/version.go | 2 +- vendor/modules.txt | 2 +- 5 files changed, 7 insertions(+), 5 deletions(-) diff --git a/go.sum b/go.sum index 328be5de8..84cc9f997 100644 --- a/go.sum +++ b/go.sum @@ -149,8 +149,6 @@ github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.m github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= github.com/envoyproxy/protoc-gen-validate v0.3.0-java h1:bV5JGEB1ouEzZa0hgVDFFiClrUEuGWRaAc/3mxR2QK0= github.com/envoyproxy/protoc-gen-validate v0.3.0-java/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= -github.com/evilmartians/lefthook v1.3.7 h1:h2lvNl5OSlnAZdV3bdA283itH6pHKaTnzk837dQqrCk= -github.com/evilmartians/lefthook v1.3.7/go.mod h1:FTw9v/+5Tg5AlrLPxS2kJeqHhQNqhUA+6/WdGeaf41s= github.com/evilmartians/lefthook v1.3.8 h1:5SOqN6vd7S7FYvr9MDNM2g2lq/kTE3uTWY7Qt63uuj8= github.com/evilmartians/lefthook v1.3.8/go.mod h1:FTw9v/+5Tg5AlrLPxS2kJeqHhQNqhUA+6/WdGeaf41s= github.com/fatih/color v1.14.1 h1:qfhVLaG5s+nCROl1zJsZRxFeYrHLqWroPOQ8BWiNb4w= diff --git a/vendor/github.com/evilmartians/lefthook/CHANGELOG.md b/vendor/github.com/evilmartians/lefthook/CHANGELOG.md index 376619641..e4442d0e0 100644 --- a/vendor/github.com/evilmartians/lefthook/CHANGELOG.md +++ b/vendor/github.com/evilmartians/lefthook/CHANGELOG.md @@ -2,6 +2,10 @@ ## master (unreleased) +## 1.3.8 (2023-03-23) + +- fix: make hook template compatible with shells without source command ([#458](https://github.com/evilmartians/lefthook/pull/458)) by @mdesantis + ## 1.3.7 (2023-03-20) - fix: allow globs in skip option ([#457](https://github.com/evilmartians/lefthook/pull/457)) by @mrexox diff --git a/vendor/github.com/evilmartians/lefthook/internal/templates/hook.tmpl b/vendor/github.com/evilmartians/lefthook/internal/templates/hook.tmpl index 75cf2793a..b8af547f3 100644 --- a/vendor/github.com/evilmartians/lefthook/internal/templates/hook.tmpl +++ b/vendor/github.com/evilmartians/lefthook/internal/templates/hook.tmpl @@ -7,7 +7,7 @@ fi {{- if .Rc}} {{/* Load rc file and export all ENV vars defined in it */}} set -a -[ -f {{.Rc}} ] && source {{.Rc}} +[ -f {{.Rc}} ] && . {{.Rc}} {{- end}} call_lefthook() diff --git a/vendor/github.com/evilmartians/lefthook/internal/version/version.go b/vendor/github.com/evilmartians/lefthook/internal/version/version.go index 6c487693a..b24205493 100644 --- a/vendor/github.com/evilmartians/lefthook/internal/version/version.go +++ b/vendor/github.com/evilmartians/lefthook/internal/version/version.go @@ -6,7 +6,7 @@ import ( "strconv" ) -const version = "1.3.7" +const version = "1.3.8" var ( // Is set via -X github.com/evilmartians/lefthook/internal/version.commit={commit}. diff --git a/vendor/modules.txt b/vendor/modules.txt index a3cd8b8dc..48b8e5895 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -336,7 +336,7 @@ github.com/emirpasic/gods/utils # github.com/envoyproxy/protoc-gen-validate v0.3.0-java ## explicit github.com/envoyproxy/protoc-gen-validate/validate -# github.com/evilmartians/lefthook v1.3.7 +# github.com/evilmartians/lefthook v1.3.8 ## explicit; go 1.19 github.com/evilmartians/lefthook github.com/evilmartians/lefthook/cmd From ce0612893ef11cab3e3b5e0f7318dd83ed8a6ed0 Mon Sep 17 00:00:00 2001 From: Amardeep Chawla Date: Thu, 23 Mar 2023 10:19:13 -0700 Subject: [PATCH 6/7] Fix dependencies --- sdk/proto/events/event.proto | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/sdk/proto/events/event.proto b/sdk/proto/events/event.proto index 3357643af..61324fc70 100644 --- a/sdk/proto/events/event.proto +++ b/sdk/proto/events/event.proto @@ -34,17 +34,17 @@ message Event { ActivityEvent ActivityEvent = 2 [(gogoproto.jsontag) = "activity_event"]; // Activity event /** - * While generating the SecurityViolationEvent, the Metadata for a SecurityViolationEvent - * would look as shown below: - * - Module = Agent - * - UUID = A UUID generated by the Agent for the EventReport - * - CorrelationID = The UUID will be used as the Correlation ID to track the EventReport - * - Timestamp = The timestamp when NGINX Agent received the violation event - * - EventLevel = All the SecurityViolationEvent would be generated at an ERROR_EVENT_LEVEL ("ERROR") level - * In future, the levels might be dynamically chosen based on Request Outcome of SecurityViolationEvent - * - Type = NGINX_EVENT_TYPE ("Nginx") - * - Category = APP_PROTECT_CATEGORY ("AppProtect") - */ + * While generating the SecurityViolationEvent, the Metadata for a SecurityViolationEvent + * would look as shown below: + * - Module = Agent + * - UUID = A UUID generated by the Agent for the EventReport + * - CorrelationID = The UUID will be used as the Correlation ID to track the EventReport + * - Timestamp = The timestamp when NGINX Agent received the violation event + * - EventLevel = All the SecurityViolationEvent would be generated at an ERROR_EVENT_LEVEL ("ERROR") level + * In future, the levels might be dynamically chosen based on Request Outcome of SecurityViolationEvent + * - Type = NGINX_EVENT_TYPE ("Nginx") + * - Category = APP_PROTECT_CATEGORY ("AppProtect") + */ SecurityViolationEvent SecurityViolationEvent = 3 [(gogoproto.jsontag) = "security_violation_event"]; // Security violation event } From 0e93bb339effbf8a400b6abbdc56feffdab9c662 Mon Sep 17 00:00:00 2001 From: Amardeep Chawla Date: Thu, 23 Mar 2023 10:19:48 -0700 Subject: [PATCH 7/7] fix more deps --- .../agent/sdk/v2/proto/events/event.proto | 22 +++++++++---------- .../agent/sdk/v2/proto/events/event.proto | 22 +++++++++---------- .../agent/sdk/v2/proto/events/event.proto | 22 +++++++++---------- 3 files changed, 33 insertions(+), 33 deletions(-) diff --git a/test/integration/vendor/github.com/nginx/agent/sdk/v2/proto/events/event.proto b/test/integration/vendor/github.com/nginx/agent/sdk/v2/proto/events/event.proto index 3357643af..61324fc70 100644 --- a/test/integration/vendor/github.com/nginx/agent/sdk/v2/proto/events/event.proto +++ b/test/integration/vendor/github.com/nginx/agent/sdk/v2/proto/events/event.proto @@ -34,17 +34,17 @@ message Event { ActivityEvent ActivityEvent = 2 [(gogoproto.jsontag) = "activity_event"]; // Activity event /** - * While generating the SecurityViolationEvent, the Metadata for a SecurityViolationEvent - * would look as shown below: - * - Module = Agent - * - UUID = A UUID generated by the Agent for the EventReport - * - CorrelationID = The UUID will be used as the Correlation ID to track the EventReport - * - Timestamp = The timestamp when NGINX Agent received the violation event - * - EventLevel = All the SecurityViolationEvent would be generated at an ERROR_EVENT_LEVEL ("ERROR") level - * In future, the levels might be dynamically chosen based on Request Outcome of SecurityViolationEvent - * - Type = NGINX_EVENT_TYPE ("Nginx") - * - Category = APP_PROTECT_CATEGORY ("AppProtect") - */ + * While generating the SecurityViolationEvent, the Metadata for a SecurityViolationEvent + * would look as shown below: + * - Module = Agent + * - UUID = A UUID generated by the Agent for the EventReport + * - CorrelationID = The UUID will be used as the Correlation ID to track the EventReport + * - Timestamp = The timestamp when NGINX Agent received the violation event + * - EventLevel = All the SecurityViolationEvent would be generated at an ERROR_EVENT_LEVEL ("ERROR") level + * In future, the levels might be dynamically chosen based on Request Outcome of SecurityViolationEvent + * - Type = NGINX_EVENT_TYPE ("Nginx") + * - Category = APP_PROTECT_CATEGORY ("AppProtect") + */ SecurityViolationEvent SecurityViolationEvent = 3 [(gogoproto.jsontag) = "security_violation_event"]; // Security violation event } diff --git a/test/performance/vendor/github.com/nginx/agent/sdk/v2/proto/events/event.proto b/test/performance/vendor/github.com/nginx/agent/sdk/v2/proto/events/event.proto index 3357643af..61324fc70 100644 --- a/test/performance/vendor/github.com/nginx/agent/sdk/v2/proto/events/event.proto +++ b/test/performance/vendor/github.com/nginx/agent/sdk/v2/proto/events/event.proto @@ -34,17 +34,17 @@ message Event { ActivityEvent ActivityEvent = 2 [(gogoproto.jsontag) = "activity_event"]; // Activity event /** - * While generating the SecurityViolationEvent, the Metadata for a SecurityViolationEvent - * would look as shown below: - * - Module = Agent - * - UUID = A UUID generated by the Agent for the EventReport - * - CorrelationID = The UUID will be used as the Correlation ID to track the EventReport - * - Timestamp = The timestamp when NGINX Agent received the violation event - * - EventLevel = All the SecurityViolationEvent would be generated at an ERROR_EVENT_LEVEL ("ERROR") level - * In future, the levels might be dynamically chosen based on Request Outcome of SecurityViolationEvent - * - Type = NGINX_EVENT_TYPE ("Nginx") - * - Category = APP_PROTECT_CATEGORY ("AppProtect") - */ + * While generating the SecurityViolationEvent, the Metadata for a SecurityViolationEvent + * would look as shown below: + * - Module = Agent + * - UUID = A UUID generated by the Agent for the EventReport + * - CorrelationID = The UUID will be used as the Correlation ID to track the EventReport + * - Timestamp = The timestamp when NGINX Agent received the violation event + * - EventLevel = All the SecurityViolationEvent would be generated at an ERROR_EVENT_LEVEL ("ERROR") level + * In future, the levels might be dynamically chosen based on Request Outcome of SecurityViolationEvent + * - Type = NGINX_EVENT_TYPE ("Nginx") + * - Category = APP_PROTECT_CATEGORY ("AppProtect") + */ SecurityViolationEvent SecurityViolationEvent = 3 [(gogoproto.jsontag) = "security_violation_event"]; // Security violation event } diff --git a/vendor/github.com/nginx/agent/sdk/v2/proto/events/event.proto b/vendor/github.com/nginx/agent/sdk/v2/proto/events/event.proto index 3357643af..61324fc70 100644 --- a/vendor/github.com/nginx/agent/sdk/v2/proto/events/event.proto +++ b/vendor/github.com/nginx/agent/sdk/v2/proto/events/event.proto @@ -34,17 +34,17 @@ message Event { ActivityEvent ActivityEvent = 2 [(gogoproto.jsontag) = "activity_event"]; // Activity event /** - * While generating the SecurityViolationEvent, the Metadata for a SecurityViolationEvent - * would look as shown below: - * - Module = Agent - * - UUID = A UUID generated by the Agent for the EventReport - * - CorrelationID = The UUID will be used as the Correlation ID to track the EventReport - * - Timestamp = The timestamp when NGINX Agent received the violation event - * - EventLevel = All the SecurityViolationEvent would be generated at an ERROR_EVENT_LEVEL ("ERROR") level - * In future, the levels might be dynamically chosen based on Request Outcome of SecurityViolationEvent - * - Type = NGINX_EVENT_TYPE ("Nginx") - * - Category = APP_PROTECT_CATEGORY ("AppProtect") - */ + * While generating the SecurityViolationEvent, the Metadata for a SecurityViolationEvent + * would look as shown below: + * - Module = Agent + * - UUID = A UUID generated by the Agent for the EventReport + * - CorrelationID = The UUID will be used as the Correlation ID to track the EventReport + * - Timestamp = The timestamp when NGINX Agent received the violation event + * - EventLevel = All the SecurityViolationEvent would be generated at an ERROR_EVENT_LEVEL ("ERROR") level + * In future, the levels might be dynamically chosen based on Request Outcome of SecurityViolationEvent + * - Type = NGINX_EVENT_TYPE ("Nginx") + * - Category = APP_PROTECT_CATEGORY ("AppProtect") + */ SecurityViolationEvent SecurityViolationEvent = 3 [(gogoproto.jsontag) = "security_violation_event"]; // Security violation event }