6
0
mirror of https://github.com/grdl/git-get.git synced 2026-02-04 20:54:41 +00:00
Files
git-get/pkg/get.go
Grzegorz Dlugoszewski 5a588f22d1 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
2020-06-18 15:44:10 +02:00

59 lines
884 B
Go

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
}