diff --git a/lib/package.go b/lib/package.go index 6fcb7e2..7ed8713 100644 --- a/lib/package.go +++ b/lib/package.go @@ -3,13 +3,13 @@ package lib import ( "archive/tar" "bufio" + "bytes" "compress/gzip" "fmt" "io" "net/url" "os" "path" - "strings" "git.matterlinux.xyz/matter/tracker/log" "github.com/bigkevmcd/go-configparser" @@ -87,12 +87,20 @@ func (p *Package) Load(r io.Reader) error { var ( err error size int64 - depends string = "" section string = "DEFAULT" + buffer []byte ) + if buffer, err = io.ReadAll(r); err != nil { + return err + } + + if p.Depends, err = GetMultiple("depends", bytes.NewReader(buffer)); err != nil { + return err + } + parser := configparser.New() - if err = parser.ParseReader(r); err != nil { + if err = parser.ParseReader(bytes.NewReader(buffer)); err != nil { return err } @@ -123,13 +131,5 @@ func (p *Package) Load(r io.Reader) error { return err } - depends, _ = parser.Get(section, "depends") - - if depends == "" { - p.Depends = []string{} - } else { - p.Depends = strings.Split(depends, ",") - } - return nil } diff --git a/lib/util.go b/lib/util.go index 45c6fd3..e9a0609 100644 --- a/lib/util.go +++ b/lib/util.go @@ -2,6 +2,7 @@ package lib import ( "archive/tar" + "bufio" "compress/gzip" "fmt" "io" @@ -11,6 +12,33 @@ import ( "github.com/gofiber/fiber/v2" ) +// configparser can't get multiple keys, so heres a function to manually extract them +func GetMultiple(k string, r io.Reader) ([]string, error) { + var ( + res []string + value string + scanner *bufio.Scanner + ) + + scanner = bufio.NewScanner(r) + + for scanner.Scan() { + line := scanner.Text() + value = strings.TrimPrefix(line, fmt.Sprintf("%s = ", k)) + value = strings.TrimPrefix(value, fmt.Sprintf("%s =", k)) + value = strings.TrimPrefix(value, fmt.Sprintf("%s= ", k)) + value = strings.TrimPrefix(value, fmt.Sprintf("%s=", k)) + + if line == value { + continue + } + + res = append(res, value) + } + + return res, nil +} + func GetFiles(r io.Reader) ([]string, error) { var ( gzip_reader io.Reader