From f2cdd16438dd51f50df06fb837ad1dcdfc0e6238 Mon Sep 17 00:00:00 2001 From: iton0 Date: Mon, 16 Dec 2024 14:10:25 -0500 Subject: [PATCH] feat: improve list subcommand - added no arg option which outputs git hooks in the current working directory via `hkup list` - added 'template' arg to list all user-defined templates - updated help page based on the changes above --- cmd/list.go | 17 +++++++-- cmd/list_test.go | 5 --- internal/logic/init.go | 2 +- internal/logic/list.go | 84 +++++++++++++++++++++++++++++++++++------- 4 files changed, 85 insertions(+), 23 deletions(-) diff --git a/cmd/list.go b/cmd/list.go index 9119631..5482920 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -6,10 +6,19 @@ import ( ) var listCmd = &cobra.Command{ - Use: "list {hook|lang}", - Short: "List git hooks information", - ValidArgs: []string{"hook", "lang"}, - Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs), + Use: "list [hook|lang|template]", + Short: "List git hooks information", + Long: `List git hooks information for the specified category. + +If no arguments are provided, this command will display the hooks used +in the current working directory. + +Valid arguments: +- hook: Displays supported git hooks. +- lang: Displays supported languages used for hooks. +- template: Displays user-defined templates.`, + ValidArgs: []string{"hook", "lang", "template"}, + Args: cobra.MatchAll(cobra.MaximumNArgs(1), cobra.OnlyValidArgs), RunE: logic.List, } diff --git a/cmd/list_test.go b/cmd/list_test.go index aadeef8..378d2aa 100644 --- a/cmd/list_test.go +++ b/cmd/list_test.go @@ -29,11 +29,6 @@ func TestListCmd(t *testing.T) { want string err error }{ - { - args: []string{"list"}, - want: "", - err: fmt.Errorf("accepts 1 arg(s), received 0"), - }, { args: []string{"list", "test"}, want: "", diff --git a/internal/logic/init.go b/internal/logic/init.go index 08ddc7e..e7be048 100644 --- a/internal/logic/init.go +++ b/internal/logic/init.go @@ -42,7 +42,7 @@ func Init(cmd *cobra.Command, args []string) error { return fmt.Errorf("must run \"hkup init\" inside a worktree") } - var gitCmd []string // Holds everything after the root git command + gitCmd := []string{} // Holds everything after the root git command var hkupDirPath string // Holds the path the .hkup directory // If both flags are set, configure core.hooksPath with their values. diff --git a/internal/logic/list.go b/internal/logic/list.go index e633ef4..79d264b 100644 --- a/internal/logic/list.go +++ b/internal/logic/list.go @@ -1,30 +1,88 @@ package logic import ( + "fmt" + "os" + "github.com/iton0/hkup-cli/internal/git" "github.com/iton0/hkup-cli/internal/util" "github.com/spf13/cobra" ) -// List displays a list of available Git hooks or supported languages based on -// the provided argument. +// List displays a list of one of the below based on provided arguement: +// - Supported git hooks +// - Supported languages +// - User-defined git hook templates +// - Git hook(s) used in the current working directory // -// Returns error if the argument is invalid. +// Returns error if issue with checking directories. func List(cmd *cobra.Command, args []string) error { - arg := args[0] out := []string{} - // NOTE: Default case is handled by cobra framework - switch { - case arg == "hook": - out = util.ConvertMapKeysToSlice(git.Hooks()) - case arg == "lang": - out = util.ConvertMapKeysToSlice(git.SupportedLangs()) + if len(args) > 0 { // Gets appropriate output based on argument provided + switch args[0] { + case "template": + out = getHookTemplates() + if out == nil { + return fmt.Errorf("could not read template directory") + } + case "hook": + out = util.ConvertMapKeysToSlice(git.Hooks()) + case "lang": + out = util.ConvertMapKeysToSlice(git.SupportedLangs()) + } + } else { // No args; gets hooks in current working directory + out = getCwdHooks() + if out == nil { + return fmt.Errorf("could not read .hkup directory") + } } - for _, key := range out { - cmd.Printf(" %s\n", key) + cmd.Print(formatOutput(out)) + return nil +} + +// formatOutput formats the output string slice as a string that is returned +func formatOutput(out []string) string { + var fout string + + for _, val := range out { + fout += " " + val + "\n" } - return nil + return fout +} + +// getHookTemplates returns all user-defined templates. +// If no templates are found, returns a empty string slice. +func getHookTemplates() []string { + out := []string{} + + files, err := os.ReadDir(util.GetTemplateDirPath()) + if err != nil { + return nil + } + + for _, file := range files { + out = append(out, file.Name()) + } + + return out +} + +// getCwdHooks returns the hooks in the current working directory. +// If no hooks are found, returns a empty string slice. +func getCwdHooks() []string { + out := []string{} + + files, err := os.ReadDir(util.HkupDirName) + if err != nil { + return nil + } + + for _, file := range files { + out = append(out, file.Name()) + } + + return out }