6
0
mirror of https://github.com/grdl/git-get.git synced 2026-02-08 07:34:18 +00:00

Simplify linters config, fix cyclop issues in main.go

This commit is contained in:
Grzegorz Dlugoszewski
2025-08-24 14:29:56 +02:00
parent ccdca12f96
commit b31de718d4
3 changed files with 84 additions and 183 deletions

View File

@@ -6,56 +6,54 @@ import (
"strings"
)
// This program behaves as a git subcommand (see https://git.github.io/htmldocs/howto/new-command.html)
// When added to PATH, git recognizes it as its subcommand and it can be invoked as "git get..." or "git list..."
// It can also be invoked as a regular binary with subcommands: "git-get get..." or "git-get list"
// The following flow detects the invokation method and runs the appropriate command.
func main() {
// This program behaves as a git subcommand (see https://git.github.io/htmldocs/howto/new-command.html)
// When added to PATH, git recognizes it as its subcommand and it can be invoked as "git get..." or "git list..."
// It can also be invoked as a regular binary with subcommands: "git-get get..." or "git-get list"
// The following flow detects the invokation method and runs the appropriate command.
command, args := determineCommand()
executeCommand(command, args)
}
programName := filepath.Base(os.Args[0])
// Remove common executable extensions
programName = strings.TrimSuffix(programName, ".exe")
// Determine which command to run based on program name or first argument
var command string
var args []string
func determineCommand() (string, []string) {
programName := strings.TrimSuffix(filepath.Base(os.Args[0]), ".exe")
switch programName {
case "git-get":
// Check if first argument is a subcommand
if len(os.Args) > 1 && (os.Args[1] == "get" || os.Args[1] == "list") {
// Called as: git-get get <repo> or git-get list
command = os.Args[1]
args = os.Args[2:]
} else {
// Called as: git-get <repo> (default to get command)
command = "get"
args = os.Args[1:]
}
return handleGitGetInvocation()
case "git-list":
// Called as: git-list (symlinked binary)
command = "list"
args = os.Args[1:]
return handleGitListInvocation()
default:
// Fallback: use first argument as command
if len(os.Args) > 1 {
command = os.Args[1]
args = os.Args[2:]
} else {
command = "get"
args = []string{}
}
return handleDefaultInvocation()
}
}
// Execute the appropriate command
func handleGitGetInvocation() (string, []string) {
if len(os.Args) > 1 && (os.Args[1] == "get" || os.Args[1] == "list") {
return os.Args[1], os.Args[2:]
}
return "get", os.Args[1:]
}
func handleGitListInvocation() (string, []string) {
return "list", os.Args[1:]
}
func handleDefaultInvocation() (string, []string) {
if len(os.Args) > 1 {
return os.Args[1], os.Args[2:]
}
return "get", []string{}
}
func executeCommand(command string, args []string) {
switch command {
case "get":
runGet(args)
case "list":
runList(args)
default:
// Default to get command for unknown commands
runGet(os.Args[1:])
}
}