Skip to content

Commit

Permalink
feat: list and rename lib
Browse files Browse the repository at this point in the history
  • Loading branch information
Kostiantyn Kostikov authored and kostikovk committed Dec 1, 2024
1 parent 7088be8 commit 2fe248d
Show file tree
Hide file tree
Showing 15 changed files with 108 additions and 57 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ out/
.env
*.local

# Do not ignore GoHooks specific files (include .gohooks/)
# Do not ignore Hooky specific files (include .hooky/)
File renamed without changes.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Define the name of the binary
BINARY_NAME := gohooks
BINARY_NAME := hooky

# Define the default target
.PHONY: all
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# GoHooks CLI [WIP]
# Hooky CLI [WIP]

GoHooks CLI is a command-line tool for managing Git hooks in Go projects. It helps developers easily set up, configure, and run Git hooks, such as pre-commit hooks, in a Go-centric environment.
Hooky CLI is a command-line tool for managing Git hooks in Go projects. It helps developers easily set up, configure, and run Git hooks, such as pre-commit hooks, in a Go-centric environment.

## Features
- **Manage Git Hooks**: Create, configure, and manage Git hooks like pre-commit, commit-msg, and more.
Expand Down
6 changes: 3 additions & 3 deletions cmd/init.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package cmd

import (
"github.com/kostikovk/gohooks/lib"
"github.com/kostikovk/hooky/lib"
"github.com/spf13/cobra"
)

var initCmd = &cobra.Command{
Use: "init",
Short: "Init GoHooks CLI",
Long: `Init GoHooks CLI...`,
Short: "Init Hooky CLI",
Long: `Init Hooky CLI...`,
Run: lib.RunInit,
}

Expand Down
14 changes: 3 additions & 11 deletions cmd/list.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,18 @@
package cmd

import (
"fmt"

"github.com/kostikovk/gohooks/helpers"
"github.com/kostikovk/hooky/lib"
"github.com/spf13/cobra"
)

var listCmd = &cobra.Command{
Use: "list",
Short: "List all available hooks",
Long: `List all available hooks.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("List all available hooks:")

// todo: need to implement this function
for i, hook := range helpers.GitHooks {
fmt.Printf("%d. %s\n", i+1, hook)
}
},
Run: lib.RunList,
}

func init() {
rootCmd.AddCommand(listCmd)
listCmd.Flags().Bool("installed", false, "Show only installed hooks")
}
6 changes: 3 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
)

var rootCmd = &cobra.Command{
Use: "gohooks",
Short: "GoHooks CLI",
Long: `GoHooks CLI helps you to work with git hooks easily.`,
Use: "hooky",
Short: "Hooky CLI",
Long: `Hooky CLI helps you to work with git hooks easily.`,
Run: func(cmd *cobra.Command, args []string) {
err := cmd.Help()
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions cmd/uninstall.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package cmd

import (
"github.com/kostikovk/gohooks/lib"
"github.com/kostikovk/hooky/lib"
"github.com/spf13/cobra"
)

var uninstallCmd = &cobra.Command{
Use: "uninstall",
Short: "Uninstall GoHooks CLI",
Long: `Uninstall GoHooks CLI...`,
Short: "Uninstall Hooky CLI",
Long: `Uninstall Hooky CLI...`,
Run: lib.RunUninstall,
}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/kostikovk/gohooks
module github.com/kostikovk/hooky

go 1.22.4

Expand Down
6 changes: 3 additions & 3 deletions helpers/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ func PromptToInitGit() error {
return Prompt("This is not a Git repository. Would you like to initialize it?")
}

// PromptToCopyGitHooksToGoHooks prompts the user to copy Git hooks to GoHooks repository.
func PromptToCopyGitHooksToGoHooks() error {
return Prompt("Would you like to copy Git hooks to GoHooks repository?")
// 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.
Expand Down
48 changes: 31 additions & 17 deletions helpers/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,33 @@ import (
"path/filepath"
)

var AbsoluteGoHooksPath = getAbsolutePath(".gohooks")
var AbsoluteGoHooksGitHooksPath = getAbsolutePath(".gohooks/hooks")
var AbsoluteHookyPath = getAbsolutePath(".hooky")
var AbsoluteHookyGitHooksPath = getAbsolutePath(".hooky/git-hooks")

// IsGoHooksRepository checks if the current directory is a GoHooks repository.
func IsGoHooksRepository() bool {
return dirExists(AbsoluteGoHooksPath)
// IsHookyRepository checks if the current directory is a Hooky repository.
func IsHookyRepository() bool {
return dirExists(AbsoluteHookyPath)
}

// CreateGoHooksGitDirectory creates a .gohooks/git/hooks folder.
func CreateGoHooksGitDirectory() error {
return os.MkdirAll(AbsoluteGoHooksGitHooksPath, 0750)
// CreateHookyGitDirectory creates a .hooky/ folder.
func CreateHookyGitDirectory() error {
return os.MkdirAll(AbsoluteHookyGitHooksPath, 0750)
}

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

// CreateGitHook creates a GoHooks Git hook.
// CreateGitHook creates a Hooky Git hook.
func CreateGitHook(hook string, cmd string) error {
// check if hook is valid Git hook.
if !GitHookExists(hook) {
return fmt.Errorf("invalid Git hook: %s", hook)
}

// check if GoHooks Git directory exists.
files, err := os.ReadDir(AbsoluteGoHooksGitHooksPath)
files, err := os.ReadDir(AbsoluteHookyGitHooksPath)
if err != nil {
return fmt.Errorf("failed to read directory: %w", err)
}
Expand All @@ -43,7 +43,7 @@ func CreateGitHook(hook string, cmd string) error {
}

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

// InstallHooks installs all GoHooks Git hooks.
func InstallHooks() error {
if !IsGoHooksRepository() {
if !IsHookyRepository() {
return fmt.Errorf("GoHooks repository not found")
}

hooks, err := os.ReadDir(AbsoluteGoHooksGitHooksPath)
hooks, err := os.ReadDir(AbsoluteHookyGitHooksPath)
if err != nil {
return fmt.Errorf("failed to read directory: %w", err)
}
Expand All @@ -89,7 +89,7 @@ func InstallHooks() error {
}

err = os.Link(
filepath.Join(AbsoluteGoHooksGitHooksPath, hook.Name()),
filepath.Join(AbsoluteHookyGitHooksPath, hook.Name()),
filepath.Join(AbsoluteGitHooksPath, hook.Name()),
)
if err != nil {
Expand All @@ -105,3 +105,17 @@ func InstallHooks() error {

return nil
}

func ListOfInstalledGitHooks() ([]string, error) {
files, err := os.ReadDir(AbsoluteHookyGitHooksPath)
if err != nil {
return nil, fmt.Errorf("failed to read directory: %w", err)
}

var hooks []string
for _, file := range files {
hooks = append(hooks, file.Name())
}

return hooks, nil
}
16 changes: 8 additions & 8 deletions lib/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package lib
import (
"os"

"github.com/kostikovk/gohooks/helpers"
"github.com/kostikovk/hooky/helpers"
"github.com/spf13/cobra"
)

Expand All @@ -21,8 +21,8 @@ func RunInit(cmd *cobra.Command, args []string) {
os.Exit(1)
}

// Initialize GoHooks repository
err = initGoHooks()
// Initialize Hooky repository
err = isHookyRepository()
if err != nil {
cmd.Println("Error initializing GoHooks repository.")

Expand Down Expand Up @@ -56,14 +56,14 @@ func initGit(cmd *cobra.Command) error {
}

// Initialize GoHooks repository.
func initGoHooks() error {
// Check if GoHooks repository already exists
if helpers.IsGoHooksRepository() {
func isHookyRepository() error {
// Check if Hooky repository already exists
if helpers.IsHookyRepository() {
return nil
}

// Create GoHooks repository
err := helpers.CreateGoHooksGitDirectory()
// Create Hooky repository
err := helpers.CreateHookyGitDirectory()
if err != nil {
return err
}
Expand Down
45 changes: 45 additions & 0 deletions lib/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package lib

import (
"github.com/kostikovk/hooky/helpers"
"github.com/spf13/cobra"
)

// Whant to implement:
// 1. Show all available hooks
// 2. Show all hooks that are installed
// 3. Show all hooks that are not installed
// 4. Ask a user about installing a hook

func RunList(cmd *cobra.Command, args []string) {
installed, _ := cmd.Flags().GetBool("installed")

if installed {
showListOfInstalledHooks(cmd)
} else {
showListOfAvailableHooks(cmd)
}
}

func showListOfAvailableHooks(cmd *cobra.Command) {
cmd.Println("Git Hooks:")

for i, hook := range helpers.GitHooks {
cmd.Printf("%d. %s\n", i, hook)
}
}

func showListOfInstalledHooks(cmd *cobra.Command) {
cmd.Println("Installed Git Hooks:")

var installedHooks []string
var err error
installedHooks, err = helpers.ListOfInstalledGitHooks()
if err != nil {
cmd.PrintErr("Error listing installed hooks.")
}

for i, hook := range installedHooks {
cmd.Printf("%d. %s\n", i, hook)
}
}
6 changes: 3 additions & 3 deletions lib/uninstall.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package lib

import (
"github.com/kostikovk/gohooks/helpers"
"github.com/kostikovk/hooky/helpers"
"github.com/spf13/cobra"
)

Expand All @@ -15,11 +15,11 @@ func RunUninstall(cmd *cobra.Command, args []string) {
}

func gohooksUninstallHandler(cmd *cobra.Command) error {
if !helpers.IsGoHooksRepository() {
if !helpers.IsHookyRepository() {
return nil
}

err := helpers.DeleteGoHooksDirectory()
err := helpers.DeleteHookyDirectory()
if err != nil {
cmd.Println("Error deleting GoHooks repository.")

Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package main

import (
"github.com/kostikovk/gohooks/cmd"
"github.com/kostikovk/hooky/cmd"
)

func main() {
Expand Down

0 comments on commit 2fe248d

Please sign in to comment.