2020-02-21 20:56:32 +00:00
|
|
|
// Copyright © 2020 Ettore Di Giacinto <mudler@gentoo.org>
|
|
|
|
// Daniele Rondina <geaaru@sabayonlinux.org>
|
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation; either version 2 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program 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 General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License along
|
|
|
|
// with this program; if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2020-05-23 06:51:33 +00:00
|
|
|
package cmd_helpers
|
2020-02-21 20:56:32 +00:00
|
|
|
|
|
|
|
import (
|
2020-03-14 10:13:09 +00:00
|
|
|
"errors"
|
2020-02-21 20:56:32 +00:00
|
|
|
"fmt"
|
2020-03-14 10:13:09 +00:00
|
|
|
"regexp"
|
2020-11-29 10:45:38 +00:00
|
|
|
"strings"
|
2020-02-21 20:56:32 +00:00
|
|
|
|
2021-04-12 17:00:36 +00:00
|
|
|
. "github.com/mudler/luet/pkg/logger"
|
|
|
|
|
2020-02-21 20:56:32 +00:00
|
|
|
_gentoo "github.com/Sabayon/pkgs-checker/pkg/gentoo"
|
|
|
|
pkg "github.com/mudler/luet/pkg/package"
|
|
|
|
)
|
|
|
|
|
2020-03-14 10:13:09 +00:00
|
|
|
func CreateRegexArray(rgx []string) ([]*regexp.Regexp, error) {
|
|
|
|
ans := make([]*regexp.Regexp, len(rgx))
|
|
|
|
if len(rgx) > 0 {
|
|
|
|
for idx, reg := range rgx {
|
|
|
|
re := regexp.MustCompile(reg)
|
|
|
|
if re == nil {
|
|
|
|
return nil, errors.New("Invalid regex " + reg + "!")
|
|
|
|
}
|
|
|
|
ans[idx] = re
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ans, nil
|
|
|
|
}
|
|
|
|
|
2020-11-29 10:45:38 +00:00
|
|
|
func packageData(p string) (string, string) {
|
|
|
|
cat := ""
|
|
|
|
name := ""
|
|
|
|
if strings.Contains(p, "/") {
|
|
|
|
packagedata := strings.Split(p, "/")
|
|
|
|
cat = packagedata[0]
|
|
|
|
name = packagedata[1]
|
|
|
|
} else {
|
|
|
|
name = p
|
|
|
|
}
|
|
|
|
return cat, name
|
|
|
|
}
|
|
|
|
|
2021-09-20 17:21:00 +00:00
|
|
|
func packageHasGentooSelector(v string) bool {
|
|
|
|
return (strings.HasPrefix(v, "=") || strings.HasPrefix(v, ">") ||
|
|
|
|
strings.HasPrefix(v, "<"))
|
|
|
|
}
|
2020-11-29 10:45:38 +00:00
|
|
|
|
2021-09-20 17:21:00 +00:00
|
|
|
func gentooVersion(gp *_gentoo.GentooPackage) string {
|
2020-11-29 10:45:38 +00:00
|
|
|
|
2021-09-20 17:21:00 +00:00
|
|
|
condition := gp.Condition.String()
|
|
|
|
if condition == "=" {
|
|
|
|
condition = ""
|
2020-02-21 20:56:32 +00:00
|
|
|
}
|
|
|
|
|
2021-09-20 17:21:00 +00:00
|
|
|
pkgVersion := fmt.Sprintf("%s%s%s",
|
|
|
|
condition,
|
|
|
|
gp.Version,
|
|
|
|
gp.VersionSuffix,
|
|
|
|
)
|
2020-02-21 20:56:32 +00:00
|
|
|
if gp.VersionBuild != "" {
|
|
|
|
pkgVersion = fmt.Sprintf("%s%s%s+%s",
|
2021-09-20 17:21:00 +00:00
|
|
|
condition,
|
2020-02-21 20:56:32 +00:00
|
|
|
gp.Version,
|
|
|
|
gp.VersionSuffix,
|
|
|
|
gp.VersionBuild,
|
|
|
|
)
|
|
|
|
}
|
2021-09-20 17:21:00 +00:00
|
|
|
return pkgVersion
|
|
|
|
}
|
2020-02-21 20:56:32 +00:00
|
|
|
|
2021-09-20 17:21:00 +00:00
|
|
|
func ParsePackageStr(p string) (*pkg.DefaultPackage, error) {
|
|
|
|
|
|
|
|
if packageHasGentooSelector(p) {
|
|
|
|
gp, err := _gentoo.ParsePackageStr(p)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if gp.Version == "" {
|
|
|
|
gp.Version = "0"
|
|
|
|
gp.Condition = _gentoo.PkgCondGreaterEqual
|
|
|
|
}
|
|
|
|
|
|
|
|
return &pkg.DefaultPackage{
|
|
|
|
Name: gp.Name,
|
|
|
|
Category: gp.Category,
|
|
|
|
Version: gentooVersion(gp),
|
|
|
|
Uri: make([]string, 0),
|
|
|
|
}, nil
|
2020-02-21 20:56:32 +00:00
|
|
|
}
|
|
|
|
|
2021-09-20 17:21:00 +00:00
|
|
|
ver := ">=0"
|
|
|
|
cat := ""
|
|
|
|
name := ""
|
|
|
|
|
|
|
|
if strings.Contains(p, "@") {
|
|
|
|
packageinfo := strings.Split(p, "@")
|
|
|
|
ver = packageinfo[1]
|
|
|
|
cat, name = packageData(packageinfo[0])
|
|
|
|
} else {
|
|
|
|
cat, name = packageData(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &pkg.DefaultPackage{
|
|
|
|
Name: name,
|
|
|
|
Category: cat,
|
|
|
|
Version: ver,
|
|
|
|
Uri: make([]string, 0),
|
|
|
|
}, nil
|
2020-02-21 20:56:32 +00:00
|
|
|
}
|
2021-04-12 17:00:36 +00:00
|
|
|
|
|
|
|
func CheckErr(err error) {
|
|
|
|
if err != nil {
|
|
|
|
Fatal(err)
|
|
|
|
}
|
|
|
|
}
|