luet/pkg/repository/loader.go

91 lines
2.3 KiB
Go
Raw Normal View History

2019-12-27 19:12:08 +00:00
// Copyright © 2019 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/>.
package repository
import (
"io/ioutil"
"path"
2020-11-07 17:28:23 +00:00
"path/filepath"
2019-12-27 19:12:08 +00:00
"regexp"
"github.com/mudler/luet/pkg/api/core/types"
2019-12-27 19:12:08 +00:00
. "github.com/mudler/luet/pkg/config"
. "github.com/mudler/luet/pkg/logger"
)
func LoadRepositories(c *LuetConfig) error {
var regexRepo = regexp.MustCompile(`.yml$|.yaml$`)
2020-11-07 17:56:25 +00:00
var err error
rootfs := ""
2019-12-27 19:12:08 +00:00
2020-11-07 17:28:23 +00:00
// Respect the rootfs param on read repositories
2020-11-07 17:56:25 +00:00
if !c.ConfigFromHost {
rootfs, err = c.GetSystem().GetRootFsAbs()
if err != nil {
return err
}
2020-11-07 17:28:23 +00:00
}
2019-12-27 19:12:08 +00:00
for _, rdir := range c.RepositoriesConfDir {
2020-11-07 17:28:23 +00:00
rdir = filepath.Join(rootfs, rdir)
2019-12-27 19:12:08 +00:00
Debug("Parsing Repository Directory", rdir, "...")
files, err := ioutil.ReadDir(rdir)
if err != nil {
2020-03-07 23:07:20 +00:00
Debug("Skip dir", rdir, ":", err.Error())
2019-12-27 19:12:08 +00:00
continue
}
for _, file := range files {
if file.IsDir() {
continue
}
if !regexRepo.MatchString(file.Name()) {
Debug("File", file.Name(), "skipped.")
continue
}
content, err := ioutil.ReadFile(path.Join(rdir, file.Name()))
if err != nil {
Warning("On read file", file.Name(), ":", err.Error())
Warning("File", file.Name(), "skipped.")
continue
}
r, err := types.LoadRepository(content)
2019-12-27 19:12:08 +00:00
if err != nil {
Warning("On parse file", file.Name(), ":", err.Error())
Warning("File", file.Name(), "skipped.")
continue
}
if r.Name == "" || len(r.Urls) == 0 || r.Type == "" {
Warning("Invalid repository ", file.Name())
Warning("File", file.Name(), "skipped.")
continue
}
c.AddSystemRepository(*r)
}
}
return nil
}