mirror of
https://github.com/grdl/git-get.git
synced 2026-02-04 14:31:49 +00:00
Refactor implementations of get and list commands
- Simplify the main functions by moving actual implementation to "pkg" package - Remove the "path" package and move files into the root "pkg" package - Replace viper with explicit Cfg structs
This commit is contained in:
58
pkg/get.go
Normal file
58
pkg/get.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package pkg
|
||||
|
||||
import (
|
||||
"git-get/pkg/repo"
|
||||
"path"
|
||||
)
|
||||
|
||||
// GetCfg provides configuration for the Get command.
|
||||
type GetCfg struct {
|
||||
Branch string
|
||||
Dump string
|
||||
Root string
|
||||
URL string
|
||||
}
|
||||
|
||||
// Get executes the "git get" command.
|
||||
func Get(c *GetCfg) error {
|
||||
if c.Dump != "" {
|
||||
return cloneDumpFile(c)
|
||||
}
|
||||
|
||||
if c.URL != "" {
|
||||
return cloneSingleRepo(c)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func cloneSingleRepo(c *GetCfg) error {
|
||||
url, err := ParseURL(c.URL)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
cloneOpts := &repo.CloneOpts{
|
||||
URL: url,
|
||||
Path: path.Join(c.Root, URLToPath(url)),
|
||||
Branch: c.Branch,
|
||||
}
|
||||
|
||||
_, err = repo.Clone(cloneOpts)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func cloneDumpFile(c *GetCfg) error {
|
||||
opts, err := ParseBundleFile(c.Dump)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, opt := range opts {
|
||||
path := path.Join(c.Root, URLToPath(opt.URL))
|
||||
opt.Path = path
|
||||
_, _ = repo.Clone(opt)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user