fix: read multiple depends key from DATA file

This commit is contained in:
ngn 2024-08-26 02:33:38 +03:00
parent 9c61c49b96
commit 6810f9b2ec
2 changed files with 39 additions and 11 deletions

View File

@ -3,13 +3,13 @@ package lib
import ( import (
"archive/tar" "archive/tar"
"bufio" "bufio"
"bytes"
"compress/gzip" "compress/gzip"
"fmt" "fmt"
"io" "io"
"net/url" "net/url"
"os" "os"
"path" "path"
"strings"
"git.matterlinux.xyz/matter/tracker/log" "git.matterlinux.xyz/matter/tracker/log"
"github.com/bigkevmcd/go-configparser" "github.com/bigkevmcd/go-configparser"
@ -87,12 +87,20 @@ func (p *Package) Load(r io.Reader) error {
var ( var (
err error err error
size int64 size int64
depends string = ""
section string = "DEFAULT" 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() parser := configparser.New()
if err = parser.ParseReader(r); err != nil { if err = parser.ParseReader(bytes.NewReader(buffer)); err != nil {
return err return err
} }
@ -123,13 +131,5 @@ func (p *Package) Load(r io.Reader) error {
return err return err
} }
depends, _ = parser.Get(section, "depends")
if depends == "" {
p.Depends = []string{}
} else {
p.Depends = strings.Split(depends, ",")
}
return nil return nil
} }

View File

@ -2,6 +2,7 @@ package lib
import ( import (
"archive/tar" "archive/tar"
"bufio"
"compress/gzip" "compress/gzip"
"fmt" "fmt"
"io" "io"
@ -11,6 +12,33 @@ import (
"github.com/gofiber/fiber/v2" "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) { func GetFiles(r io.Reader) ([]string, error) {
var ( var (
gzip_reader io.Reader gzip_reader io.Reader