Skip to content

Commit

Permalink
feat: update Makefile to use specific Go version and improve command …
Browse files Browse the repository at this point in the history
…execution
  • Loading branch information
kostikovk committed Mar 3, 2025
1 parent 90f8c6e commit 773bddb
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 39 deletions.
14 changes: 8 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
# Define the name of the binary
BINARY_NAME := hooky

GO := go1.24.0

# Define the default target
.PHONY: all
all: build

# Build the application
.PHONY: build
build:
CGO_ENABLED=0 go build -ldflags="-s -w" -o bin/$(BINARY_NAME)
CGO_ENABLED=0 $(GO) build -ldflags="-s -w" -o bin/$(BINARY_NAME)

# Run tests
.PHONY: test
test:
go test -v ./...
$(GO) test -v ./...

# Clean build artifacts
.PHONY: clean
Expand All @@ -29,8 +31,8 @@ run: build
# Format code
.PHONY: fmt
fmt:
go fmt ./...
go mod tidy
$(GO) fmt ./...
$(GO) mod tidy

# Lint code
.PHONY: lint
Expand All @@ -54,12 +56,12 @@ static-analysis: lint vet
# Vet code
.PHONY: vet
vet:
go vet ./...
$(GO) vet ./...

# Tidy go.mod and go.sum
.PHONY: tidy
tidy:
go mod tidy
$(GO) mod tidy

# Pre commit hook
# todo: need to add security later and fix the issues
Expand Down
6 changes: 0 additions & 6 deletions helpers/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,16 @@ var GitHooks = []string{
var AbsoluteGitPath = getAbsolutePath(".git")
var AbsoluteGitHooksPath = getAbsolutePath(".git/hooks")

// GitHookExists checks if a Git hook exists in the GitHooks slice.
func GitHookExists(hook string) bool {
return Contains(GitHooks, func(h string) bool {
return h == hook
})
}

// IsGitRepository checks if the current directory is a Git repository.
func IsGitRepository() bool {
return dirExists(AbsoluteGitPath)
}

// InitGit initializes a Git repository.
func InitGit() error {
cmd := exec.Command("git", "init")
err := cmd.Run()
Expand All @@ -55,17 +52,14 @@ func InitGit() error {
return nil
}

// PromptToInitGit prompts the user to initialize a Git repository.
func PromptToInitGit() error {
return Prompt("This is not a Git repository. Would you like to initialize it?")
}

// PromptToCopyGitHooksToHooky prompts the user to copy Git hooks to Hooky repository.
func PromptToCopyGitHooksToHooky() error {
return Prompt("Would you like to copy Git hooks to Hooky repository?")
}

// DeleteGitHooksDirectory .git/hooks folder with all its contents.
func DeleteGitHooksDirectory() error {
return os.RemoveAll(AbsoluteGitHooksPath)
}
13 changes: 1 addition & 12 deletions helpers/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
var AbsoluteHookyPath = getAbsolutePath(".hooky")
var AbsoluteHookyGitHooksPath = getAbsolutePath(".hooky/git-hooks")

// IsHookyRepository checks if the current directory is a Hooky repository.
func IsHookyRepository() bool {
return dirExists(AbsoluteHookyPath)
}
Expand All @@ -18,19 +17,15 @@ func HasGitHooksDirectory() bool {
return dirExists(AbsoluteHookyGitHooksPath)
}

// CreateHookyGitDirectory creates a .hooky/ folder.
func CreateHookyGitDirectory() error {
return os.MkdirAll(AbsoluteHookyGitHooksPath, 0750)
}

// DeleteHookyDirectory .hooky directory
func DeleteHookyDirectory() error {
return os.RemoveAll(AbsoluteHookyPath)
}

// CreateGitHook creates a Hooky Git hook.
func CreateGitHook(hook string, cmd string) error {
// check if Hooky repository exists.
if !IsHookyRepository() {
fmt.Println("Hooky repository not found")
fmt.Println("Please, do 'hooky init' to create a Hooky repository")
Expand All @@ -45,23 +40,19 @@ func CreateGitHook(hook string, cmd string) error {
return fmt.Errorf("git hooks directory not found in Hooky repository '.hooky/git-hooks'")
}

// check if hook is valid Git hook.
if !GitHookExists(hook) {
return fmt.Errorf("invalid Git hook: %s", hook)
}

// check if Hooky Git directory exists.
files, err := os.ReadDir(AbsoluteHookyGitHooksPath)
if err != nil {
return fmt.Errorf("failed to read directory: %w", err)
}

// check if hook already exists.
if ContainsFile(files, hook) {
return fmt.Errorf("hook already exists: %s", hook)
}

// create hook file.
file, err := os.Create(filepath.Join(AbsoluteHookyGitHooksPath, hook))
if err != nil {
return fmt.Errorf("failed to create file: %w", err)
Expand All @@ -81,7 +72,6 @@ func CreateGitHook(hook string, cmd string) error {
return nil
}

// InstallHooks installs all GoHooks Git hooks.
func InstallHooks() error {
if !IsHookyRepository() {
return fmt.Errorf("GoHooks repository not found")
Expand All @@ -107,15 +97,14 @@ func InstallHooks() error {
continue
}

err = os.Link(
err = os.Symlink(
filepath.Join(AbsoluteHookyGitHooksPath, hook.Name()),
filepath.Join(AbsoluteGitHooksPath, hook.Name()),
)
if err != nil {
return fmt.Errorf("failed to link file: %w", err)
}

// make hook executable.
err = os.Chmod(filepath.Join(AbsoluteGitHooksPath, hook.Name()), 0750)
if err != nil {
return fmt.Errorf("failed to change file permissions: %w", err)
Expand Down
5 changes: 0 additions & 5 deletions helpers/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"os"
)

// dirExists checks if a directory exists and is a directory.
func dirExists(path string) bool {
info, err := os.Stat(path)
if os.IsNotExist(err) {
Expand All @@ -15,7 +14,6 @@ func dirExists(path string) bool {
return info.IsDir()
}

// getAbsolutePath returns the absolute path of a file.
func getAbsolutePath(path string) string {
wd, err := os.Getwd()
if err != nil {
Expand All @@ -25,7 +23,6 @@ func getAbsolutePath(path string) string {
return wd + "/" + path
}

// Contains checks if a slice contains an element based on the provided comparison function.
func Contains[T any](arr []T, compare func(T) bool) bool {
for _, a := range arr {
if compare(a) {
Expand All @@ -35,14 +32,12 @@ func Contains[T any](arr []T, compare func(T) bool) bool {
return false
}

// ContainsFile checks if a slice of FileInfo contains a file with the given name.
func ContainsFile(arr []os.DirEntry, fileName string) bool {
return Contains(arr, func(f os.DirEntry) bool {
return f.Name() == fileName
})
}

// Prompt asks the user for input y/n and return error or nil.
func Prompt(prompt string) error {
fmt.Println(prompt)
fmt.Print("Y/n: ")
Expand Down
10 changes: 1 addition & 9 deletions lib/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,18 @@ import (
"github.com/spf13/cobra"
)

// RunInit initializes GoHooks.
func RunInit(cmd *cobra.Command, args []string) {
cmd.Println("Initializing GoHooks...")

var err error

// Initialize or ask to initialize Git repository
err = initGit(cmd)
if err != nil {
cmd.Println("Error initializing Git repository.")

os.Exit(1)
}

// Initialize Hooky repository
err = initHooky()
if err != nil {
cmd.Println("Error initializing GoHooks repository.")
Expand All @@ -39,7 +36,6 @@ func RunInit(cmd *cobra.Command, args []string) {
cmd.Println("Hooky initialized 🎉")
}

// Initialize or ask to initialize Git repository.
func initGit(cmd *cobra.Command) error {
if helpers.IsGitRepository() {
return nil
Expand All @@ -55,21 +51,17 @@ func initGit(cmd *cobra.Command) error {
return nil
}

// Initialize Hooky repository.
func initHooky() error {
// Check if Hooky repository already exists
if helpers.IsHookyRepository() {
return nil
}

// Create Hooky repository
err := helpers.CreateHookyGitDirectory()
if err != nil {
return err
}

// Create pre-commit hook
err = helpers.CreateGitHook("pre-commit", "# go test ./...")
err = helpers.CreateGitHook("pre-commit", "echo 'Hey 👋, Hooky!'")
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion lib/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func RunUninstall(cmd *cobra.Command, args []string) {
cmd.Println("Error uninstalling GoHooks.")
}

cmd.Println("Hooky uninstalled :(")
cmd.Println("Hooky uninstalled 🥺")
}

func hookyUninstallHandler(cmd *cobra.Command) error {
Expand Down

0 comments on commit 773bddb

Please sign in to comment.