6
0
mirror of https://github.com/grdl/git-get.git synced 2026-02-05 17:43:48 +00:00
Files
git-get/pkg/bundle.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

67 lines
1.4 KiB
Go

package pkg
import (
"bufio"
"git-get/pkg/repo"
"os"
"strings"
"github.com/pkg/errors"
)
var (
ErrInvalidNumberOfElements = errors.New("More than two space-separated 2 elements on the line")
)
// ParseBundleFile opens a given gitgetfile and parses its content into a slice of CloneOpts.
func ParseBundleFile(path string) ([]*repo.CloneOpts, error) {
file, err := os.Open(path)
if err != nil {
return nil, errors.Wrapf(err, "Failed opening gitgetfile %s", path)
}
defer file.Close()
scanner := bufio.NewScanner(file)
var opts []*repo.CloneOpts
var line int
for scanner.Scan() {
line++
opt, err := parseLine(scanner.Text())
if err != nil {
return nil, errors.Wrapf(err, "Failed parsing line %d", line)
}
opts = append(opts, opt)
}
return opts, nil
}
// parseLine splits a gitgetfile line into space-separated segments.
// First part is the URL to clone. Second, optional, is the branch (or tag) to checkout after cloning
func parseLine(line string) (*repo.CloneOpts, error) {
parts := strings.Split(line, " ")
if len(parts) > 2 {
return nil, ErrInvalidNumberOfElements
}
url, err := ParseURL(parts[0])
if err != nil {
return nil, err
}
branch := ""
if len(parts) == 2 {
branch = parts[1]
}
return &repo.CloneOpts{
URL: url,
Branch: branch,
// When cloning a bundle we ignore errors about already cloned repos
IgnoreExisting: true,
}, nil
}