luet/cmd/convert.go

98 lines
2.8 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 (
"io/ioutil"
2019-11-03 09:50:07 +00:00
"runtime"
. "github.com/mudler/luet/pkg/logger"
pkg "github.com/mudler/luet/pkg/package"
tree "github.com/mudler/luet/pkg/tree"
"github.com/mudler/luet/pkg/tree/builder/gentoo"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var convertCmd = &cobra.Command{
Use: "convert",
Short: "convert other package manager tree into luet",
Long: `Parses external PM and produces a luet parsable tree`,
Run: func(cmd *cobra.Command, args []string) {
t := viper.GetString("type")
2019-11-01 11:41:31 +00:00
c := viper.GetInt("concurrency")
databaseType := viper.GetString("database")
var db pkg.PackageDatabase
if len(args) != 2 {
2019-11-04 09:16:20 +00:00
Fatal("Incorrect number of arguments")
}
input := args[0]
output := args[1]
2019-11-02 17:00:06 +00:00
Info("Converting trees from " + input + " [" + t + "]")
var builder tree.Parser
switch t {
case "gentoo":
builder = gentoo.NewGentooBuilder(&gentoo.SimpleEbuildParser{}, c, gentoo.InMemory)
default: // dup
builder = gentoo.NewGentooBuilder(&gentoo.SimpleEbuildParser{}, c, gentoo.InMemory)
}
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()
packageTree, err := builder.Generate(input)
if err != nil {
2019-11-04 09:16:20 +00:00
Fatal("Error: " + err.Error())
}
defer packageTree.GetPackageSet().Clean()
Info("Tree generated")
generalRecipe := tree.NewGeneralRecipe(db)
Info("Saving generated tree to " + output)
generalRecipe.WithTree(packageTree)
err = generalRecipe.Save(output)
if err != nil {
2019-11-04 09:16:20 +00:00
Fatal("Error: " + err.Error())
}
},
}
func init() {
convertCmd.Flags().String("type", "gentoo", "source type")
viper.BindPFlag("type", convertCmd.Flags().Lookup("type"))
2019-11-03 09:50:07 +00:00
convertCmd.Flags().Int("concurrency", runtime.NumCPU(), "Concurrency")
2019-11-01 11:41:31 +00:00
viper.BindPFlag("concurrency", convertCmd.Flags().Lookup("concurrency"))
convertCmd.Flags().String("database", "memory", "database used for solving (memory,boltdb)")
viper.BindPFlag("database", convertCmd.Flags().Lookup("database"))
RootCmd.AddCommand(convertCmd)
}