Skip to content

Commit

Permalink
docs: remove unnecessary go doc comments
Browse files Browse the repository at this point in the history
  • Loading branch information
iton0 committed Dec 22, 2024
1 parent 6e77bef commit 7a00949
Show file tree
Hide file tree
Showing 16 changed files with 58 additions and 123 deletions.
3 changes: 2 additions & 1 deletion cmd/config/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import (
"github.com/spf13/cobra"
)

// RootCmd is the config command that will be added to the root HkUp command.
// RootCmd is the root config subcommand that will be added to the root HkUp
// command.
var RootCmd = &cobra.Command{
Use: "config",
Short: "HkUp configuration settings",
Expand Down
11 changes: 4 additions & 7 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,8 @@ import (
)

var (
// version holds the centralized version of HkUp.
// It is updated to the latest release version at build time of the binaries.
//
// INFO: look at the .github/workflows/release-please.yml to view how version
// is updated.
// NOTE: Updated to the latest release version at build time of the binaries
// via .github/workflows/release-please.yml
version = "dev"

rootCmd = &cobra.Command{
Expand All @@ -35,8 +32,8 @@ func init() {
rootCmd.AddCommand(listCmd)
}

// Execute serves as a wrapper for the Cobra API's Execute function,
// allowing it to be called from the [github.com/iton0/hkup-cli] package.
// Execute serves as a wrapper for the Cobra API's Execute function, allowing it
// to be called from the [github.com/iton0/hkup-cli] package.
func Execute() {
rootCmd.Execute()
}
3 changes: 2 additions & 1 deletion cmd/template/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import (
"github.com/spf13/cobra"
)

// RootCmd is the template command that will be added to the root HkUp command.
// RootCmd is the root template subcommand that will be added to the root HkUp
// command.
var RootCmd = &cobra.Command{
Use: "template",
Short: "Reusable Git hook",
Expand Down
13 changes: 2 additions & 11 deletions internal/logic/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,14 @@ import (
var LangFlg string

// Add creates a new Git hook with the specified git hook name and optional
// programming language in the designated .hkup directory.
//
// Returns error if any of the steps fail above.
// programming language in the designated .hkup directory. Returns error if any
// of the steps fail above.
func Add(cmd *cobra.Command, args []string) error {
isBare, err := isBareRepo(".")
if err != nil { // Current working directory is not a git repository at all
return err
}

// Tries to create .hkup directory if it does not exist and current working
// directory is not a bare git repository
if !util.DoesDirectoryExist(util.HkupDirName) && !isBare {
if err := util.CreateDirectory(util.HkupDirName); err != nil {
return err
Expand All @@ -35,16 +32,13 @@ func Add(cmd *cobra.Command, args []string) error {
hook := args[0]
filePath := util.GetHookFilePath(hook)

// Does not add if hook already exists in .hkup directory
if util.DoesFileExist(filePath) {
return fmt.Errorf("%s hook already exists", hook)
}

var sheBangLine string

// Uses the specified language from lang flag; else default to sh
if LangFlg != "" {
// Makes sure lang is supported
if isValid := git.CheckLangSupported(LangFlg); !isValid {
return fmt.Errorf("language not supported: %s", LangFlg)
}
Expand All @@ -53,19 +47,16 @@ func Add(cmd *cobra.Command, args []string) error {
sheBangLine = "#!/bin/sh"
}

// Creates the git hook file
file, err := util.CreateFile(filePath)
if err != nil {
return err
}
defer file.Close()

// Writes the language shebang line to the created file from above
_, err = file.WriteString(sheBangLine)
if err != nil {
return err
}

// Either changes the create file's permissions successful or returns error
return util.MakeExecutable(filePath)
}
7 changes: 3 additions & 4 deletions internal/logic/config/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ import (
"github.com/spf13/cobra"
)

// Get prints out the value of a specified configuration setting.
//
// Returns error if issue with getting the value.
// Get prints out the value of a specified configuration setting. Returns error
// if issue with getting the value.
func Get(cmd *cobra.Command, args []string) error {
out, err := util.GetINIValue(args[0])
if err != nil {
return err
}

cmd.Println(out) // This may be empty if the value is not set but key is valid
cmd.Println(out)
return nil
}
7 changes: 2 additions & 5 deletions internal/logic/config/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@ import (
"github.com/spf13/cobra"
)

// Set updates a configuration setting with a new value.
//
// Returns error if issue with settings the configuration setting.
// Set updates a configuration setting with a new value. Returns error if issue
// with settings the configuration setting.
func Set(cmd *cobra.Command, args []string) error {
// Either setting configuration setting is successful and returns nil or
// returns error
return util.SetINIValue(args[0], args[1])
}
10 changes: 5 additions & 5 deletions internal/logic/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ import (
// It constructs the URL for the documentation based on the hook name and attempts
// to open it using the appropriate command for the operating system.
//
// Returns:
// - error if the hook key is invalid, if the platform is unsupported, or if
// there is an issue starting the command.
// Returns error if:
// - hook key is invalid
// - platform is unsupported
// - issue starting the command
func Doc(cmd *cobra.Command, args []string) error {
// Full url path for the specified git hook
url := "https://git-scm.com/docs/githooks#" + git.GetHookUrl(args[0])
Expand All @@ -38,6 +39,5 @@ func Doc(cmd *cobra.Command, args []string) error {
return err
}

// Must be called after successfully starting terminal command above
return termCmd.Wait() // Returns error if command fails
return termCmd.Wait()
}
4 changes: 0 additions & 4 deletions internal/logic/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ func Init(cmd *cobra.Command, args []string) error {
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.
// Otherwise, use the default hooks directory (util.HkupDirName).
if GitDirFlg != "" && WorkTreeFlg != "" {
hkupDirPath = filepath.Join(WorkTreeFlg, util.HkupDirName)
gitCmd = []string{"--git-dir=" + GitDirFlg, "--work-tree=" + WorkTreeFlg, "config", "--local", "core.hooksPath", hkupDirPath}
Expand All @@ -45,8 +43,6 @@ func Init(cmd *cobra.Command, args []string) error {
gitCmd = []string{"config", "--local", "core.hooksPath", hkupDirPath}
}

// Does not override the hooksPath variable if already set and force flag is
// not used
if !ForceFlg {
out, _ := exec.Command("git", gitCmd[:len(gitCmd)-1]...).CombinedOutput()
if len(out) != 0 {
Expand Down
8 changes: 4 additions & 4 deletions internal/logic/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
func List(cmd *cobra.Command, args []string) error {
out := []string{}

if len(args) > 0 { // Gets appropriate output based on argument provided
if len(args) > 0 {
switch args[0] {
case "template":
out = getHookTemplates()
Expand All @@ -31,7 +31,7 @@ func List(cmd *cobra.Command, args []string) error {
case "lang":
out = util.ConvertMapKeysToSlice(git.SupportedLangs())
}
} else { // No args; gets hooks in current working directory
} else {
out = getCwdHooks()
if out == nil {
return fmt.Errorf("could not read .hkup directory")
Expand All @@ -53,8 +53,8 @@ func formatOutput(out []string) string {
return fout
}

// getHookTemplates returns all user-defined templates.
// If no templates are found, returns a empty string slice.
// getHookTemplates returns a slice of all user-defined template file names.
// If no templates are found, returns a empty slice.
func getHookTemplates() []string {
out := []string{}

Expand Down
3 changes: 1 addition & 2 deletions internal/logic/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import (

// Remove deletes a specified Git hook from the .hkup directory.
// It takes a single argument, which is the name of the hook to be removed.
//
// Returns error if issue deleting the file
// Returns error if issue deleting the file.
func Remove(cmd *cobra.Command, args []string) error {
return os.Remove(util.GetHookFilePath(args[0]))
}
37 changes: 15 additions & 22 deletions internal/logic/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,18 @@ import (
)

// Root wraps git-related clone commands for easier initialization of HkUp.
//
// Returns error if issue with cloning repository or initializing HkUp.
func Root(cmd *cobra.Command, args []string) error {
// Tries to run git command in the terminal
if err := util.RunCommandInTerminal(args[0], args[1:]...); err != nil {
return err
}

possibleRepoUrl := args[len(args)-2]
possibleCustomDir := args[len(args)-1]

// Loop through the args to see if "--" is used.
// Gets the two previous args and sets them to the repoUrl and
// possibleCustomDir variables, respectively.
// INFO: Since gh command can use the syntax of:
// gh repo clone <url> [<customdir>] -- <gitflags>
// Need to parse for "--" and update possible repo url and custom directory
for i, v := range args {
if v == "--" {
possibleRepoUrl = args[i-2]
Expand All @@ -32,18 +30,16 @@ func Root(cmd *cobra.Command, args []string) error {
}
}

// Tries to cd into the created directory
if err := cdLogic(possibleRepoUrl, possibleCustomDir); err != nil {
return err
}

// Tries to initialize HkUp
return Init(cmd, nil)
}

// cdLogic implements the HkUp wrapper logic around cloning for both 'git' and
// 'gh' command.
// Returns error if issue with changing directory
// Returns error if issue with changing directory.
func cdLogic(possibleRepoUrl, possibleCustomDir string) error {
// No custom directory name provided when using git command
usedDefaultGit := strings.HasSuffix(possibleCustomDir, ".git")
Expand All @@ -54,33 +50,30 @@ func cdLogic(possibleRepoUrl, possibleCustomDir string) error {
// Starting index of the remote repo name
start := strings.LastIndex(possibleCustomDir, "/") + 1

// Checks if user did not provide a custom directory name
if usedDefaultGit { // git command
// If the repo is bare then just take the remote repo name
createdDir := possibleCustomDir // Holds the name of the cloned directory

if usedDefaultGit {
if isBare, _ := isBareRepo(possibleCustomDir[start:]); isBare {
possibleCustomDir = possibleCustomDir[start:]
} else { // Repo is not bare ie regular clone
createdDir = possibleCustomDir[start:]
} else {
end := strings.LastIndex(possibleCustomDir, ".git")
possibleCustomDir = possibleCustomDir[start:end]
createdDir = possibleCustomDir[start:end]
}
} else if usedDefaultGh { // gh command
// If the repo is bare then just take the remote repo name
} else if usedDefaultGh {
if isBare, _ := isBareRepo(possibleCustomDir[start:] + ".git"); isBare {
possibleCustomDir = possibleCustomDir[start:] + ".git"
} else { // Repo is not bare ie regular clone
possibleCustomDir = possibleCustomDir[start:]
createdDir = possibleCustomDir[start:] + ".git"
} else {
createdDir = possibleCustomDir[start:]
}
}

// Either successful or returns error if issue with changing directory
return os.Chdir(possibleCustomDir)
return os.Chdir(createdDir)
}

// isBareRepo reports if given directory (dir) is a bare git repository.
// Additionally, returns error if the given directory is not a git repository
// at all.
func isBareRepo(dir string) (bool, error) {
// Checks if current working directory is git bare repo
out, err := exec.Command("git", "-C", dir, "rev-parse", "--is-bare-repository").Output()
if err != nil {
return false, err
Expand Down
1 change: 0 additions & 1 deletion internal/logic/template/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ func Copy(cmd *cobra.Command, args []string) error {
configPath := util.GetConfigDirPath()
var templateName string

// Only starts the process of copying if config path and .hkup directory exist
switch {
case !util.DoesDirectoryExist(configPath):
return fmt.Errorf("%s directory does not exist", configPath)
Expand Down
Loading

0 comments on commit 7a00949

Please sign in to comment.