2019-10-30 16:52:57 +00:00
|
|
|
// 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"
|
2019-11-16 10:51:30 +00:00
|
|
|
"io/ioutil"
|
2019-10-30 16:52:57 +00:00
|
|
|
"log"
|
2019-11-04 09:16:20 +00:00
|
|
|
|
|
|
|
. "github.com/mudler/luet/pkg/logger"
|
2019-10-30 16:52:57 +00:00
|
|
|
|
|
|
|
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")
|
|
|
|
|
2019-11-03 16:24:47 +00:00
|
|
|
if len(args) != 4 {
|
2019-10-30 16:52:57 +00:00
|
|
|
log.Fatalln("Incorrect number of arguments")
|
|
|
|
}
|
2019-11-16 10:51:30 +00:00
|
|
|
databaseType := viper.GetString("database")
|
|
|
|
var db pkg.PackageDatabase
|
2019-10-30 16:52:57 +00:00
|
|
|
|
2019-11-16 10:51:30 +00:00
|
|
|
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)
|
2019-10-30 16:52:57 +00:00
|
|
|
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())
|
2019-10-30 16:52:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
defer generalRecipe.Tree().GetPackageSet().Clean()
|
|
|
|
|
|
|
|
t := args[0]
|
|
|
|
v := args[1]
|
|
|
|
version := args[2]
|
2019-11-03 16:24:47 +00:00
|
|
|
cat := args[3]
|
2019-10-30 16:52:57 +00:00
|
|
|
switch t {
|
|
|
|
case "install":
|
|
|
|
// XXX: pack needs to be the same which is present in world.
|
|
|
|
// Tree caches generated world when using FindPackage
|
2019-11-03 16:24:47 +00:00
|
|
|
pack, err := generalRecipe.Tree().FindPackage(&pkg.DefaultPackage{Category: cat, Name: v, Version: version})
|
2019-10-30 16:52:57 +00:00
|
|
|
if err != nil {
|
2019-11-04 09:16:20 +00:00
|
|
|
Fatal("Error: " + err.Error())
|
2019-10-30 16:52:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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())
|
2019-10-30 16:52:57 +00:00
|
|
|
}
|
|
|
|
fmt.Println(">>> World")
|
|
|
|
for _, packss := range world {
|
2019-10-31 11:41:12 +00:00
|
|
|
packss.Explain()
|
2019-10-30 16:52:57 +00:00
|
|
|
}
|
2019-11-16 10:51:30 +00:00
|
|
|
s := solver.NewSolver([]pkg.Package{}, world, generalRecipe.Tree().GetPackageSet())
|
2019-10-30 16:52:57 +00:00
|
|
|
solution, err := s.Install([]pkg.Package{pack})
|
|
|
|
if err != nil {
|
2019-11-04 09:16:20 +00:00
|
|
|
Fatal("Error: " + err.Error())
|
2019-10-30 16:52:57 +00:00
|
|
|
}
|
|
|
|
fmt.Println(">>> Solution")
|
|
|
|
|
|
|
|
for _, assertion := range solution {
|
2019-10-31 11:41:12 +00:00
|
|
|
assertion.Explain()
|
2019-10-30 16:52:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
queryCmd.Flags().String("input", "", "source folder")
|
|
|
|
viper.BindPFlag("input", queryCmd.Flags().Lookup("input"))
|
2019-11-16 10:51:30 +00:00
|
|
|
queryCmd.Flags().String("database", "memory", "database used for solving (memory,boltdb)")
|
|
|
|
viper.BindPFlag("database", queryCmd.Flags().Lookup("database"))
|
|
|
|
|
2019-10-30 16:52:57 +00:00
|
|
|
RootCmd.AddCommand(queryCmd)
|
|
|
|
}
|