Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[shell-operator] fix/add error and handling #712

Merged
merged 3 commits into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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
40 changes: 29 additions & 11 deletions pkg/utils/file/file.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package utils

import (
"errors"
"log/slog"
"os"
"path/filepath"
Expand All @@ -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 CheckExecutablePermissions(f os.FileInfo) error {
if f.Mode()&0o111 == 0 {
return ErrFileNoExecutablePermissions
}

return nil
}

// RecursiveGetExecutablePaths finds recursively all executable files
Expand All @@ -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 := checkExecutableHookFile(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
}

Expand Down Expand Up @@ -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 := 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)))
}

Expand All @@ -94,17 +106,23 @@ 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 checkExecutableHookFile(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)
return CheckExecutablePermissions(f)
}
2 changes: 1 addition & 1 deletion pkg/utils/file/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
})
}

Expand Down
Loading