87 lines
1.5 KiB
Go
87 lines
1.5 KiB
Go
|
package lib
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"io"
|
||
|
"net/url"
|
||
|
"strings"
|
||
|
|
||
|
"github.com/bigkevmcd/go-configparser"
|
||
|
)
|
||
|
|
||
|
type Package struct {
|
||
|
Name string `json:"name"`
|
||
|
Pool *Pool `json:"-"`
|
||
|
Version string `json:"version"`
|
||
|
Depends []string `json:"depends"`
|
||
|
Size string `json:"size"`
|
||
|
Desc string `json:"desc"`
|
||
|
}
|
||
|
|
||
|
func (p *Package) URL() string {
|
||
|
if nil == p.Pool {
|
||
|
return ""
|
||
|
}
|
||
|
url, _ := url.JoinPath(p.Pool.Source, "src/branch/"+p.Pool.Branch+"/src", p.Name)
|
||
|
return url
|
||
|
}
|
||
|
|
||
|
func (p *Package) DependsToStr() string {
|
||
|
var depends string = ""
|
||
|
for _, d := range p.Depends {
|
||
|
depends += fmt.Sprintf("%s ", d)
|
||
|
}
|
||
|
return depends
|
||
|
}
|
||
|
|
||
|
func (p *Package) Load(r io.Reader) error {
|
||
|
var (
|
||
|
err error
|
||
|
size int64
|
||
|
depends string = ""
|
||
|
section string = "DEFAULT"
|
||
|
)
|
||
|
|
||
|
parser := configparser.New()
|
||
|
if err = parser.ParseReader(r); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
for _, s := range parser.Sections() {
|
||
|
if s == "DEFAULT" {
|
||
|
continue
|
||
|
}
|
||
|
section = s
|
||
|
break
|
||
|
}
|
||
|
|
||
|
if section == "DEFAULT" {
|
||
|
return fmt.Errorf("DATA does not contain any sections")
|
||
|
}
|
||
|
|
||
|
p.Name = section
|
||
|
|
||
|
if p.Version, err = parser.Get(section, "version"); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
if size, err = parser.GetInt64(section, "size"); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
p.Size = SizeFromBytes(size)
|
||
|
|
||
|
if p.Desc, err = parser.Get(section, "desc"); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
depends, _ = parser.Get(section, "depends")
|
||
|
|
||
|
if depends == "" {
|
||
|
p.Depends = []string{}
|
||
|
} else {
|
||
|
p.Depends = strings.Split(depends, ",")
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|