| ... | @@ -0,0 +1,49 @@ |
| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | 	"io/ioutil" |
| 5 | 	"os" |
| 6 | 	"strings" |
| 7 | |
| 8 | 	"github.com/nektro/go-util/util" |
| 9 | 	"gopkg.in/yaml.v2" |
| 10 | ) |
| 11 | |
| 12 | // ModFile is |
| 13 | type ModFile struct { |
| 14 | 	Name string `yaml:"name"` |
| 15 | 	Main string `yaml:"main"` |
| 16 | 	Deps []Dep `yaml:"dependencies"` |
| 17 | } |
| 18 | |
| 19 | // this is the string enum because Go doesnt have those |
| 20 | var ( |
| 21 | 	DepTypeGit = "git" |
| 22 | 	AllDepTypes = []string{ |
| 23 | 		DepTypeGit, |
| 24 | 	} |
| 25 | ) |
| 26 | |
| 27 | // Dep is |
| 28 | type Dep struct { |
| 29 | 	Type string `yaml:"type"` |
| 30 | 	Path string `yaml:"path"` |
| 31 | } |
| 32 | |
| 33 | func (s *Dep) cleanPath() string { |
| 34 | 	p := s.Path |
| 35 | 	p = strings.TrimPrefix(p, "https://") |
| 36 | 	p = strings.TrimPrefix(p, "http://") |
| 37 | 	p = strings.TrimSuffix(p, ".git") |
| 38 | 	p = strings.Join(strings.Fields(strings.Join(strings.Split(p, "/"), " ")), "/") |
| 39 | 	return p |
| 40 | } |
| 41 | |
| 42 | func readModFile(fpath string) *ModFile { |
| 43 | 	f, err := os.Open(fpath) |
| 44 | 	util.DieOnError(err) |
| 45 | 	bys, _ := ioutil.ReadAll(f) |
| 46 | 	m := new(ModFile) |
| 47 | 	yaml.Unmarshal(bys, &m) |
| 48 | 	return m |
| 49 | } |