-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- 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
- Loading branch information
Showing
4 changed files
with
85 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |