Signed-off-by: Matheus Sampaio Queiroga <srherobrine20@gmail.com>
144 lines
3.6 KiB
Go
144 lines
3.6 KiB
Go
package apt
|
|
|
|
import (
|
|
"fmt"
|
|
"net/url"
|
|
"strings"
|
|
|
|
"sirherobrine23.com.br/sirherobrine23/go-dpkg/deb822"
|
|
)
|
|
|
|
type AptSource struct {
|
|
Enabled bool `json:"enabled"`
|
|
Types []string `json:"source_type"`
|
|
URIs []*url.URL `json:"uris"`
|
|
Suites []string `json:"suites"`
|
|
Components []string `json:"components"`
|
|
SignedBy string `json:"signed_by"`
|
|
Arch string `json:"arch"`
|
|
Extra deb822.Deb822 `json:"extra"`
|
|
}
|
|
|
|
func ParseSourcelist(body string) (sources []AptSource, err error) {
|
|
newBody, sourceType := "", "deb822"
|
|
for line := range strings.SplitSeq(body, "\n") {
|
|
line = strings.TrimSpace(line)
|
|
if strings.HasPrefix(line, "#") {
|
|
continue
|
|
}
|
|
newBody += line + "\n"
|
|
if sourceType == "deb822" && (strings.HasPrefix(line, "deb") || strings.HasPrefix(line, "deb-src")) {
|
|
sourceType = "apt"
|
|
}
|
|
}
|
|
|
|
switch sourceType {
|
|
case "deb822":
|
|
return parseDeb822(newBody)
|
|
default:
|
|
return parseApt(newBody)
|
|
}
|
|
}
|
|
|
|
func parseApt(body string) (sources []AptSource, err error) {
|
|
for line := range strings.SplitSeq(body, "\n") {
|
|
if line == "" {
|
|
continue
|
|
}
|
|
|
|
config := ""
|
|
if i := strings.Index(line, "["); i > 0 {
|
|
j := strings.Index(line, "]")
|
|
config = strings.TrimSpace(line[i+1 : j])
|
|
line = strings.TrimSpace(line[:i]) + " " + strings.TrimSpace(line[j+1:])
|
|
}
|
|
|
|
fields := strings.Fields(line)
|
|
if len(fields) < 3 || !(fields[0] == "deb" || fields[0] == "deb-src") {
|
|
continue
|
|
}
|
|
source := AptSource{Enabled: true, Types: []string{fields[0]}, Extra: deb822.Deb822{}}
|
|
fields = fields[1:]
|
|
|
|
if config != "" {
|
|
config := strings.Fields(strings.Trim(config, "[]"))
|
|
for _, c := range config {
|
|
values := strings.SplitN(c, "=", 2)
|
|
switch values[0] {
|
|
case "signed-by":
|
|
source.SignedBy = values[1]
|
|
case "arch":
|
|
source.Arch = values[1]
|
|
default:
|
|
source.Extra[values[0]] = deb822.SingleLine(values[1])
|
|
}
|
|
}
|
|
}
|
|
|
|
uriRepository, err := url.Parse(fields[0])
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
source.URIs = append(source.URIs, uriRepository)
|
|
source.Suites = append(source.Suites, fields[1])
|
|
source.Components = fields[2:]
|
|
sources = append(sources, source)
|
|
}
|
|
return
|
|
}
|
|
|
|
func parseDeb822(body string) (sources []AptSource, err error) {
|
|
for sourceBody := range strings.SplitSeq(body, "\n\n") {
|
|
keys, err := deb822.Parse([]byte(sourceBody))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
source := AptSource{Enabled: true, Extra: deb822.Deb822{}}
|
|
if enabled, ok := keys["Enabled"]; ok && enabled.String() == "no" {
|
|
source.Enabled = false
|
|
}
|
|
|
|
if _, ok := keys["Types"]; !ok {
|
|
return nil, fmt.Errorf("missing Types key")
|
|
}
|
|
source.Types = strings.Fields(keys["Types"].String())
|
|
|
|
if _, ok := keys["URIs"]; !ok {
|
|
return nil, fmt.Errorf("missing URIs key")
|
|
}
|
|
for _, uri := range strings.Fields(keys["URIs"].String()) {
|
|
uriRepository, err := url.Parse(uri)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
source.URIs = append(source.URIs, uriRepository)
|
|
}
|
|
|
|
if _, ok := keys["Suites"]; !ok {
|
|
return nil, fmt.Errorf("missing Suites key")
|
|
}
|
|
source.Suites = strings.Fields(keys["Suites"].String())
|
|
|
|
if _, ok := keys["Components"]; !ok {
|
|
return nil, fmt.Errorf("missing Components key")
|
|
}
|
|
source.Components = strings.Fields(keys["Components"].String())
|
|
|
|
if signedBy, ok := keys["Signed-By"]; ok {
|
|
source.SignedBy = signedBy.String()
|
|
}
|
|
|
|
delete(keys, "Enabled")
|
|
delete(keys, "Types")
|
|
delete(keys, "URIs")
|
|
delete(keys, "Suites")
|
|
delete(keys, "Components")
|
|
delete(keys, "Signed-By")
|
|
source.Extra = keys
|
|
|
|
sources = append(sources, source)
|
|
}
|
|
return
|
|
}
|