Add support for Zypper

This commit is contained in:
Elara 2025-04-12 19:49:18 +02:00
parent 55fd94c46b
commit 53c39b6f3d
4 changed files with 99 additions and 28 deletions

20
internal/index/common.go Normal file
View File

@ -0,0 +1,20 @@
package index
import "strings"
type repomd struct {
Locations []location `xml:"data>location"`
}
type location struct {
Href string `xml:"href,attr"`
}
func (r repomd) getFilelists() string {
for _, loc := range r.Locations {
if strings.Contains(loc.Href, "filelists.xml") {
return loc.Href
}
}
return ""
}

View File

@ -27,7 +27,6 @@ import (
"context"
"encoding/xml"
"errors"
"fmt"
"io"
"net/http"
"net/url"
@ -37,23 +36,6 @@ import (
"go.elara.ws/distrohop/internal/tags"
)
type repomd struct {
Locations []location `xml:"data>location"`
}
type location struct {
Href string `xml:"href,attr"`
}
func (r repomd) getGzipFile() string {
for _, loc := range r.Locations {
if strings.HasSuffix(loc.Href, "filelists.xml.gz") {
return loc.Href
}
}
return ""
}
type DNF struct{}
func (DNF) Name() string {
@ -65,10 +47,9 @@ func (DNF) IndexURL(baseURL, version, repo, arch string) ([]string, error) {
if err != nil {
return nil, err
}
repomdPath := fmt.Sprintf("/pub/fedora/linux/releases/%s/%s/%s/os/repodata/repomd.xml", version, repo, arch)
u.Path = repomdPath
res, err := http.Get(u.String())
repomdURL := u.JoinPath("linux/releases", version, repo, arch, "os/repodata/repomd.xml")
res, err := http.Get(repomdURL.String())
if err != nil {
return nil, err
}
@ -80,13 +61,13 @@ func (DNF) IndexURL(baseURL, version, repo, arch string) ([]string, error) {
return nil, err
}
gzipFile := data.getGzipFile()
if gzipFile == "" {
return nil, errors.New("no gzip file found in repomd.xml")
filelists := data.getFilelists()
if filelists == "" {
return nil, errors.New("no filelists found in repomd.xml")
}
u.Path = fmt.Sprintf("/pub/fedora/linux/releases/%s/%s/%s/os/%s", version, repo, arch, gzipFile)
return []string{u.String()}, nil
filelistsURL := u.JoinPath("linux/releases", version, repo, arch, "os", filelists)
return []string{filelistsURL.String()}, nil
}
func (DNF) ReadPkgData(r io.Reader, out chan Record) {

View File

@ -47,6 +47,7 @@ var importers = []Importer{
APT{},
DNF{},
Pacman{},
Zypper{},
}
// GetImporter gets an importer by its name

69
internal/index/zypper.go Normal file
View File

@ -0,0 +1,69 @@
/*
* distrohop - A utility for correlating and identifying equivalent software
* packages across different Linux distributions
*
* Copyright (C) 2025 Elara Ivy <elara@elara.ws>
*
* This file is part of distrohop.
*
* distrohop is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* distrohop is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with distrohop. If not, see <http://www.gnu.org/licenses/>.
*/
package index
import (
"encoding/xml"
"errors"
"io"
"net/http"
"net/url"
)
type Zypper struct{}
func (Zypper) Name() string {
return "zypper"
}
func (Zypper) IndexURL(baseURL, version, repo, _ string) ([]string, error) {
u, err := url.ParseRequestURI(baseURL)
if err != nil {
return nil, err
}
repomdURL := u.JoinPath(version, "repo", repo, "repodata/repomd.xml")
res, err := http.Get(repomdURL.String())
if err != nil {
return nil, err
}
defer res.Body.Close()
var data repomd
err = xml.NewDecoder(res.Body).Decode(&data)
if err != nil {
return nil, err
}
gzipFile := data.getFilelists()
if gzipFile == "" {
return nil, errors.New("no filelists found in repomd.xml")
}
filelistURL := u.JoinPath(version, "repo", repo, gzipFile)
return []string{filelistURL.String()}, nil
}
func (Zypper) ReadPkgData(r io.Reader, out chan Record) {
DNF{}.ReadPkgData(r, out)
}