mirror of
https://github.com/grdl/git-get.git
synced 2026-02-04 15:04:44 +00:00
Replace two separate binaries with a single one
This commit is contained in:
@@ -4,27 +4,28 @@ import (
|
|||||||
"git-get/pkg"
|
"git-get/pkg"
|
||||||
"git-get/pkg/cfg"
|
"git-get/pkg/cfg"
|
||||||
"git-get/pkg/git"
|
"git-get/pkg/git"
|
||||||
|
"os"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
const example = ` git get grdl/git-get
|
const getExample = ` git get grdl/git-get
|
||||||
git get https://github.com/grdl/git-get.git
|
git get https://github.com/grdl/git-get.git
|
||||||
git get git@github.com:grdl/git-get.git
|
git get git@github.com:grdl/git-get.git
|
||||||
git get -d path/to/dump/file`
|
git get -d path/to/dump/file`
|
||||||
|
|
||||||
var cmd = &cobra.Command{
|
func newGetCommand() *cobra.Command {
|
||||||
Use: "git get <REPO>",
|
cmd := &cobra.Command{
|
||||||
Short: "Clone git repository into an automatically created directory tree based on the repo's URL.",
|
Use: "git get <REPO>",
|
||||||
Example: example,
|
Short: "Clone git repository into an automatically created directory tree based on the repo's URL.",
|
||||||
RunE: run,
|
Example: getExample,
|
||||||
Args: cobra.MaximumNArgs(1), // TODO: add custom validator
|
RunE: runGetCommand,
|
||||||
Version: cfg.Version(),
|
Args: cobra.MaximumNArgs(1), // TODO: add custom validator
|
||||||
SilenceUsage: true, // We don't want to show usage on legit errors (eg, wrong path, repo already existing etc.)
|
Version: cfg.Version(),
|
||||||
}
|
SilenceUsage: true, // We don't want to show usage on legit errors (eg, wrong path, repo already existing etc.)
|
||||||
|
}
|
||||||
|
|
||||||
func init() {
|
|
||||||
cmd.PersistentFlags().StringP(cfg.KeyBranch, "b", "", "Branch (or tag) to checkout after cloning.")
|
cmd.PersistentFlags().StringP(cfg.KeyBranch, "b", "", "Branch (or tag) to checkout after cloning.")
|
||||||
cmd.PersistentFlags().StringP(cfg.KeyDefaultHost, "t", cfg.Defaults[cfg.KeyDefaultHost], "Host to use when <REPO> doesn't have a specified host.")
|
cmd.PersistentFlags().StringP(cfg.KeyDefaultHost, "t", cfg.Defaults[cfg.KeyDefaultHost], "Host to use when <REPO> doesn't have a specified host.")
|
||||||
cmd.PersistentFlags().StringP(cfg.KeyDefaultScheme, "c", cfg.Defaults[cfg.KeyDefaultScheme], "Scheme to use when <REPO> doesn't have a specified scheme.")
|
cmd.PersistentFlags().StringP(cfg.KeyDefaultScheme, "c", cfg.Defaults[cfg.KeyDefaultScheme], "Scheme to use when <REPO> doesn't have a specified scheme.")
|
||||||
@@ -41,10 +42,10 @@ func init() {
|
|||||||
viper.BindPFlag(cfg.KeyReposRoot, cmd.PersistentFlags().Lookup(cfg.KeyReposRoot))
|
viper.BindPFlag(cfg.KeyReposRoot, cmd.PersistentFlags().Lookup(cfg.KeyReposRoot))
|
||||||
viper.BindPFlag(cfg.KeySkipHost, cmd.PersistentFlags().Lookup(cfg.KeySkipHost))
|
viper.BindPFlag(cfg.KeySkipHost, cmd.PersistentFlags().Lookup(cfg.KeySkipHost))
|
||||||
|
|
||||||
cfg.Init(&git.ConfigGlobal{})
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
func run(cmd *cobra.Command, args []string) error {
|
func runGetCommand(cmd *cobra.Command, args []string) error {
|
||||||
var url string
|
var url string
|
||||||
if len(args) > 0 {
|
if len(args) > 0 {
|
||||||
url = args[0]
|
url = args[0]
|
||||||
@@ -64,6 +65,17 @@ func run(cmd *cobra.Command, args []string) error {
|
|||||||
return pkg.Get(config)
|
return pkg.Get(config)
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func runGet(args []string) {
|
||||||
cmd.Execute()
|
// Initialize configuration
|
||||||
|
cfg.Init(&git.ConfigGlobal{})
|
||||||
|
|
||||||
|
// Create and execute the get command
|
||||||
|
cmd := newGetCommand()
|
||||||
|
|
||||||
|
// Set args for cobra to parse
|
||||||
|
cmd.SetArgs(args)
|
||||||
|
|
||||||
|
if err := cmd.Execute(); err != nil {
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -5,22 +5,23 @@ import (
|
|||||||
"git-get/pkg"
|
"git-get/pkg"
|
||||||
"git-get/pkg/cfg"
|
"git-get/pkg/cfg"
|
||||||
"git-get/pkg/git"
|
"git-get/pkg/git"
|
||||||
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
var cmd = &cobra.Command{
|
func newListCommand() *cobra.Command {
|
||||||
Use: "git list",
|
cmd := &cobra.Command{
|
||||||
Short: "List all repositories cloned by 'git get' and their status.",
|
Use: "git list",
|
||||||
RunE: run,
|
Short: "List all repositories cloned by 'git get' and their status.",
|
||||||
Args: cobra.NoArgs,
|
RunE: runListCommand,
|
||||||
Version: cfg.Version(),
|
Args: cobra.NoArgs,
|
||||||
SilenceUsage: true, // We don't want to show usage on legit errors (eg, wrong path, repo already existing etc.)
|
Version: cfg.Version(),
|
||||||
}
|
SilenceUsage: true, // We don't want to show usage on legit errors (eg, wrong path, repo already existing etc.)
|
||||||
|
}
|
||||||
|
|
||||||
func init() {
|
|
||||||
cmd.PersistentFlags().BoolP(cfg.KeyFetch, "f", false, "First fetch from remotes before listing repositories.")
|
cmd.PersistentFlags().BoolP(cfg.KeyFetch, "f", false, "First fetch from remotes before listing repositories.")
|
||||||
cmd.PersistentFlags().StringP(cfg.KeyOutput, "o", cfg.Defaults[cfg.KeyOutput], fmt.Sprintf("Output format. Allowed values: [%s].", strings.Join(cfg.AllowedOut, ", ")))
|
cmd.PersistentFlags().StringP(cfg.KeyOutput, "o", cfg.Defaults[cfg.KeyOutput], fmt.Sprintf("Output format. Allowed values: [%s].", strings.Join(cfg.AllowedOut, ", ")))
|
||||||
cmd.PersistentFlags().StringP(cfg.KeyReposRoot, "r", cfg.Defaults[cfg.KeyReposRoot], "Path to repos root where repositories are cloned.")
|
cmd.PersistentFlags().StringP(cfg.KeyReposRoot, "r", cfg.Defaults[cfg.KeyReposRoot], "Path to repos root where repositories are cloned.")
|
||||||
@@ -31,10 +32,10 @@ func init() {
|
|||||||
viper.BindPFlag(cfg.KeyOutput, cmd.PersistentFlags().Lookup(cfg.KeyOutput))
|
viper.BindPFlag(cfg.KeyOutput, cmd.PersistentFlags().Lookup(cfg.KeyOutput))
|
||||||
viper.BindPFlag(cfg.KeyReposRoot, cmd.PersistentFlags().Lookup(cfg.KeyReposRoot))
|
viper.BindPFlag(cfg.KeyReposRoot, cmd.PersistentFlags().Lookup(cfg.KeyReposRoot))
|
||||||
|
|
||||||
cfg.Init(&git.ConfigGlobal{})
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
func run(cmd *cobra.Command, args []string) error {
|
func runListCommand(cmd *cobra.Command, args []string) error {
|
||||||
cfg.Expand(cfg.KeyReposRoot)
|
cfg.Expand(cfg.KeyReposRoot)
|
||||||
|
|
||||||
config := &pkg.ListCfg{
|
config := &pkg.ListCfg{
|
||||||
@@ -46,6 +47,17 @@ func run(cmd *cobra.Command, args []string) error {
|
|||||||
return pkg.List(config)
|
return pkg.List(config)
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func runList(args []string) {
|
||||||
cmd.Execute()
|
// Initialize configuration
|
||||||
|
cfg.Init(&git.ConfigGlobal{})
|
||||||
|
|
||||||
|
// Create and execute the list command
|
||||||
|
cmd := newListCommand()
|
||||||
|
|
||||||
|
// Set args for cobra to parse
|
||||||
|
cmd.SetArgs(args)
|
||||||
|
|
||||||
|
if err := cmd.Execute(); err != nil {
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
61
cmd/main.go
Normal file
61
cmd/main.go
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
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:]
|
||||||
|
}
|
||||||
|
case "git-list":
|
||||||
|
// Called as: git-list (symlinked binary)
|
||||||
|
command = "list"
|
||||||
|
args = os.Args[1:]
|
||||||
|
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{}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Execute the appropriate command
|
||||||
|
switch command {
|
||||||
|
case "get":
|
||||||
|
runGet(args)
|
||||||
|
case "list":
|
||||||
|
runList(args)
|
||||||
|
default:
|
||||||
|
// Default to get command for unknown commands
|
||||||
|
runGet(os.Args[1:])
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user