From edad314a82d528940e7862a5a61d7da803c10d23 Mon Sep 17 00:00:00 2001 From: Pavel Okhlopkov Date: Wed, 5 Feb 2025 10:21:04 +0300 Subject: [PATCH 1/3] add error and handling Signed-off-by: Pavel Okhlopkov --- pkg/utils/file/file.go | 38 +++++++++++++++++++++++++++---------- pkg/utils/file/file_test.go | 2 +- 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/pkg/utils/file/file.go b/pkg/utils/file/file.go index 54c8fd31..31c053f1 100644 --- a/pkg/utils/file/file.go +++ b/pkg/utils/file/file.go @@ -1,6 +1,7 @@ package utils import ( + "errors" "log/slog" "os" "path/filepath" @@ -21,8 +22,12 @@ func FileExists(path string) (bool, error) { return true, nil } -func IsFileExecutable(f os.FileInfo) bool { - return f.Mode()&0o111 != 0 +func IsFileExecutable(f os.FileInfo) error { + if f.Mode()&0o111 == 0 { + return ErrFileNoExecutablePermissions + } + + return nil } // RecursiveGetExecutablePaths finds recursively all executable files @@ -43,9 +48,15 @@ func RecursiveGetExecutablePaths(dir string) ([]string, error) { return nil } - if !isExecutableHookFile(f) { - log.Warn("File is skipped: no executable permissions, chmod +x is required to run this hook", - slog.String("file", path)) + if err := isExecutableHookFile(f); err != nil { + if errors.Is(err, ErrFileNoExecutablePermissions) { + log.Warn("file is skipped", slog.String("path", path), log.Err(err)) + + return nil + } + + log.Debug("file is skipped", slog.String("path", path), log.Err(err)) + return nil } @@ -80,8 +91,9 @@ func RecursiveCheckLibDirectory(dir string) error { return nil } - if isExecutableHookFile(f) { - log.Warn("File has executable permissions and is located in the ignored 'lib' directory", + + if err := isExecutableHookFile(f); err == nil { + log.Warn("file has executable permissions and is located in the ignored 'lib' directory", slog.String("file", strings.TrimPrefix(path, dir))) } @@ -94,16 +106,22 @@ func RecursiveCheckLibDirectory(dir string) error { return nil } -func isExecutableHookFile(f os.FileInfo) bool { +var ( + ErrFileHasWrongExtension = errors.New("file has wrong extension") + ErrFileIsHidden = errors.New("file is hidden") + ErrFileNoExecutablePermissions = errors.New("no executable permissions, chmod +x is required to run this hook") +) + +func isExecutableHookFile(f os.FileInfo) error { // ignore hidden files if strings.HasPrefix(f.Name(), ".") { - return false + return ErrFileIsHidden } // ignore .yaml, .json, .txt, .md files switch filepath.Ext(f.Name()) { case ".yaml", ".json", ".md", ".txt": - return false + return ErrFileHasWrongExtension } return IsFileExecutable(f) diff --git a/pkg/utils/file/file_test.go b/pkg/utils/file/file_test.go index 97df98ff..34cbd425 100644 --- a/pkg/utils/file/file_test.go +++ b/pkg/utils/file/file_test.go @@ -143,7 +143,7 @@ func TestRecursiveCheckLibDirectory(t *testing.T) { assert.Equal(t, buf.String(), - `{"level":"warn","msg":"File has executable permissions and is located in the ignored 'lib' directory","file":"/lib.py","time":"2006-01-02T15:04:05Z"}`+"\n") + `{"level":"warn","msg":"file has executable permissions and is located in the ignored 'lib' directory","file":"/lib.py","time":"2006-01-02T15:04:05Z"}`+"\n") }) } From f05f82b0222f30c9bb9f950e11d5bbc59c4d3d27 Mon Sep 17 00:00:00 2001 From: Pavel Okhlopkov Date: Wed, 5 Feb 2025 10:51:45 +0300 Subject: [PATCH 2/3] add makefile Signed-off-by: Pavel Okhlopkov --- Makefile | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 Makefile diff --git a/Makefile b/Makefile new file mode 100644 index 00000000..3e1f8be7 --- /dev/null +++ b/Makefile @@ -0,0 +1,22 @@ +GO=$(shell which go) +GIT=$(shell which git) + +.PHONY: go-check +go-check: + $(call error-if-empty,$(GO),go) + +.PHONY: git-check +git-check: + $(call error-if-empty,$(GIT),git) + +.PHONY: go-module-version +go-module-version: go-check git-check + @echo "go get $(shell $(GO) list ./cmd/shell-operator)@$(shell $(GIT) rev-parse HEAD)" + +.PHONY: test +test: go-check + @$(GO) test --race --cover ./... + +define error-if-empty +@if [[ -z $(1) ]]; then echo "$(2) not installed"; false; fi +endef \ No newline at end of file From 4b162d0c7f91e81caf7340cc8d932dbdb6d38588 Mon Sep 17 00:00:00 2001 From: Pavel Okhlopkov Date: Wed, 5 Feb 2025 16:25:20 +0300 Subject: [PATCH 3/3] fix naming Signed-off-by: Pavel Okhlopkov --- pkg/utils/file/file.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pkg/utils/file/file.go b/pkg/utils/file/file.go index 31c053f1..b9aadb78 100644 --- a/pkg/utils/file/file.go +++ b/pkg/utils/file/file.go @@ -22,7 +22,7 @@ func FileExists(path string) (bool, error) { return true, nil } -func IsFileExecutable(f os.FileInfo) error { +func CheckExecutablePermissions(f os.FileInfo) error { if f.Mode()&0o111 == 0 { return ErrFileNoExecutablePermissions } @@ -48,7 +48,7 @@ func RecursiveGetExecutablePaths(dir string) ([]string, error) { return nil } - if err := isExecutableHookFile(f); err != nil { + if err := checkExecutableHookFile(f); err != nil { if errors.Is(err, ErrFileNoExecutablePermissions) { log.Warn("file is skipped", slog.String("path", path), log.Err(err)) @@ -92,7 +92,7 @@ func RecursiveCheckLibDirectory(dir string) error { return nil } - if err := isExecutableHookFile(f); err == nil { + if err := checkExecutableHookFile(f); err == nil { log.Warn("file has executable permissions and is located in the ignored 'lib' directory", slog.String("file", strings.TrimPrefix(path, dir))) } @@ -112,7 +112,7 @@ var ( ErrFileNoExecutablePermissions = errors.New("no executable permissions, chmod +x is required to run this hook") ) -func isExecutableHookFile(f os.FileInfo) error { +func checkExecutableHookFile(f os.FileInfo) error { // ignore hidden files if strings.HasPrefix(f.Name(), ".") { return ErrFileIsHidden @@ -124,5 +124,5 @@ func isExecutableHookFile(f os.FileInfo) error { return ErrFileHasWrongExtension } - return IsFileExecutable(f) + return CheckExecutablePermissions(f) }