6
0
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:
Grzegorz Dlugoszewski
2020-06-18 15:44:10 +02:00
parent 8511cd6c97
commit 5a588f22d1
9 changed files with 151 additions and 118 deletions

58
pkg/get.go Normal file
View 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
}