luet/cmd/query.go

113 lines
3.1 KiB
Go
Raw Normal View History

// Copyright © 2019 Ettore Di Giacinto <mudler@gentoo.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 cmd
import (
"fmt"
"io/ioutil"
"log"
2019-11-04 09:16:20 +00:00
. "github.com/mudler/luet/pkg/logger"
pkg "github.com/mudler/luet/pkg/package"
"github.com/mudler/luet/pkg/solver"
tree "github.com/mudler/luet/pkg/tree"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var queryCmd = &cobra.Command{
Use: "query install <pkg>",
Short: "query other package manager tree into luet",
Long: `Parses external PM and produces a luet parsable tree`,
Run: func(cmd *cobra.Command, args []string) {
input := viper.GetString("input")
if len(args) != 4 {
log.Fatalln("Incorrect number of arguments")
}
databaseType := viper.GetString("database")
var db pkg.PackageDatabase
switch databaseType {
case "memory":
db = pkg.NewInMemoryDatabase(false)
case "boltdb":
tmpdir, err := ioutil.TempDir("", "package")
if err != nil {
Fatal(err)
}
db = pkg.NewBoltDatabase(tmpdir)
}
defer db.Clean()
generalRecipe := tree.NewGeneralRecipe(db)
fmt.Println("Loading generated tree from " + input)
err := generalRecipe.Load(input)
if err != nil {
2019-11-04 09:16:20 +00:00
Fatal("Error: " + err.Error())
}
defer generalRecipe.Tree().GetPackageSet().Clean()
t := args[0]
v := args[1]
version := args[2]
cat := args[3]
switch t {
case "install":
// XXX: pack needs to be the same which is present in world.
// Tree caches generated world when using FindPackage
pack, err := generalRecipe.Tree().FindPackage(&pkg.DefaultPackage{Category: cat, Name: v, Version: version})
if err != nil {
2019-11-04 09:16:20 +00:00
Fatal("Error: " + err.Error())
}
fmt.Println("Install query from " + input + " [" + v + "]")
world, err := generalRecipe.Tree().World()
if err != nil {
2019-11-04 09:16:20 +00:00
Fatal("Error: " + err.Error())
}
fmt.Println(">>> World")
for _, packss := range world {
2019-10-31 11:41:12 +00:00
packss.Explain()
}
s := solver.NewSolver([]pkg.Package{}, world, generalRecipe.Tree().GetPackageSet())
solution, err := s.Install([]pkg.Package{pack})
if err != nil {
2019-11-04 09:16:20 +00:00
Fatal("Error: " + err.Error())
}
fmt.Println(">>> Solution")
for _, assertion := range solution {
2019-10-31 11:41:12 +00:00
assertion.Explain()
}
}
},
}
func init() {
queryCmd.Flags().String("input", "", "source folder")
viper.BindPFlag("input", queryCmd.Flags().Lookup("input"))
queryCmd.Flags().String("database", "memory", "database used for solving (memory,boltdb)")
viper.BindPFlag("database", queryCmd.Flags().Lookup("database"))
RootCmd.AddCommand(queryCmd)
}