From 2fe248d3f8a68f228b81080333168de4c045ff7c Mon Sep 17 00:00:00 2001 From: Kostiantyn Kostikov Date: Sun, 1 Dec 2024 23:01:07 +0100 Subject: [PATCH] feat: list and rename lib --- .gitignore | 2 +- .../hooks => .hooky/git-hooks}/pre-commit | 0 Makefile | 2 +- README.md | 4 +- cmd/init.go | 6 +-- cmd/list.go | 14 ++---- cmd/root.go | 6 +-- cmd/uninstall.go | 6 +-- go.mod | 2 +- helpers/git.go | 6 +-- helpers/hooks.go | 48 ++++++++++++------- lib/init.go | 16 +++---- lib/list.go | 45 +++++++++++++++++ lib/uninstall.go | 6 +-- main.go | 2 +- 15 files changed, 108 insertions(+), 57 deletions(-) rename {.gohooks/hooks => .hooky/git-hooks}/pre-commit (100%) create mode 100644 lib/list.go diff --git a/.gitignore b/.gitignore index d8ebca4..4b23d61 100644 --- a/.gitignore +++ b/.gitignore @@ -30,4 +30,4 @@ out/ .env *.local -# Do not ignore GoHooks specific files (include .gohooks/) +# Do not ignore Hooky specific files (include .hooky/) diff --git a/.gohooks/hooks/pre-commit b/.hooky/git-hooks/pre-commit similarity index 100% rename from .gohooks/hooks/pre-commit rename to .hooky/git-hooks/pre-commit diff --git a/Makefile b/Makefile index 7db7ab7..a7cc96c 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ # Define the name of the binary -BINARY_NAME := gohooks +BINARY_NAME := hooky # Define the default target .PHONY: all diff --git a/README.md b/README.md index 5d08163..398015d 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/cmd/init.go b/cmd/init.go index 7465cc4..52e4e6d 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -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, } diff --git a/cmd/list.go b/cmd/list.go index 19f45ce..f162057 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -1,9 +1,7 @@ package cmd import ( - "fmt" - - "github.com/kostikovk/gohooks/helpers" + "github.com/kostikovk/hooky/lib" "github.com/spf13/cobra" ) @@ -11,16 +9,10 @@ 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") } diff --git a/cmd/root.go b/cmd/root.go index 4ecbfb1..0fdf9db 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -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 { diff --git a/cmd/uninstall.go b/cmd/uninstall.go index cc4b003..e368984 100644 --- a/cmd/uninstall.go +++ b/cmd/uninstall.go @@ -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, } diff --git a/go.mod b/go.mod index 735eac4..a5b7ac4 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/kostikovk/gohooks +module github.com/kostikovk/hooky go 1.22.4 diff --git a/helpers/git.go b/helpers/git.go index 0d8a9ca..f91ab41 100644 --- a/helpers/git.go +++ b/helpers/git.go @@ -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. diff --git a/helpers/hooks.go b/helpers/hooks.go index 5576123..a105e57 100644 --- a/helpers/hooks.go +++ b/helpers/hooks.go @@ -6,25 +6,25 @@ 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) { @@ -32,7 +32,7 @@ func CreateGitHook(hook string, cmd string) error { } // 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) } @@ -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) } @@ -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) } @@ -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 { @@ -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 +} diff --git a/lib/init.go b/lib/init.go index 2e082e7..343cd52 100644 --- a/lib/init.go +++ b/lib/init.go @@ -3,7 +3,7 @@ package lib import ( "os" - "github.com/kostikovk/gohooks/helpers" + "github.com/kostikovk/hooky/helpers" "github.com/spf13/cobra" ) @@ -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.") @@ -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 } diff --git a/lib/list.go b/lib/list.go new file mode 100644 index 0000000..6741ff6 --- /dev/null +++ b/lib/list.go @@ -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) + } +} diff --git a/lib/uninstall.go b/lib/uninstall.go index c96d92c..2f001b0 100644 --- a/lib/uninstall.go +++ b/lib/uninstall.go @@ -1,7 +1,7 @@ package lib import ( - "github.com/kostikovk/gohooks/helpers" + "github.com/kostikovk/hooky/helpers" "github.com/spf13/cobra" ) @@ -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.") diff --git a/main.go b/main.go index 46e4e4b..b361b8f 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,7 @@ package main import ( - "github.com/kostikovk/gohooks/cmd" + "github.com/kostikovk/hooky/cmd" ) func main() {