mirror of
https://github.com/grdl/git-get.git
synced 2026-02-04 15:39:46 +00:00
87 lines
1.5 KiB
Go
87 lines
1.5 KiB
Go
package pkg
|
|
|
|
import (
|
|
"fmt"
|
|
"git-get/pkg/git"
|
|
"path/filepath"
|
|
)
|
|
|
|
// GetCfg provides configuration for the Get command.
|
|
type GetCfg struct {
|
|
Branch string
|
|
DefHost string
|
|
DefScheme string
|
|
Dump string
|
|
Root string
|
|
SkipHost bool
|
|
URL string
|
|
}
|
|
|
|
// Get executes the "git get" command.
|
|
func Get(conf *GetCfg) error {
|
|
if conf.URL == "" && conf.Dump == "" {
|
|
return fmt.Errorf("missing <REPO> argument or --dump flag")
|
|
}
|
|
|
|
if conf.URL != "" {
|
|
return cloneSingleRepo(conf)
|
|
}
|
|
|
|
if conf.Dump != "" {
|
|
return cloneDumpFile(conf)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func cloneSingleRepo(conf *GetCfg) error {
|
|
url, err := ParseURL(conf.URL, conf.DefHost, conf.DefScheme)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
opts := &git.CloneOpts{
|
|
URL: url,
|
|
Path: filepath.Join(conf.Root, URLToPath(*url, conf.SkipHost)),
|
|
Branch: conf.Branch,
|
|
}
|
|
|
|
_, err = git.Clone(opts)
|
|
|
|
return err
|
|
}
|
|
|
|
func cloneDumpFile(conf *GetCfg) error {
|
|
parsedLines, err := parseDumpFile(conf.Dump)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
for _, line := range parsedLines {
|
|
url, err := ParseURL(line.rawurl, conf.DefHost, conf.DefScheme)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
opts := &git.CloneOpts{
|
|
URL: url,
|
|
Path: filepath.Join(conf.Root, URLToPath(*url, conf.SkipHost)),
|
|
Branch: line.branch,
|
|
}
|
|
|
|
// If target path already exists, skip cloning this repo
|
|
if exists, _ := git.Exists(opts.Path); exists {
|
|
continue
|
|
}
|
|
|
|
fmt.Printf("Cloning %s...\n", opts.URL.String())
|
|
|
|
_, err = git.Clone(opts)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|