Do not register viper bindings in init

Otherwise they get overlapped. Use PreRun instead
This commit is contained in:
Ettore Di Giacinto 2019-11-25 19:55:30 +01:00
parent 914ac68eea
commit b751b02830
No known key found for this signature in database
GPG Key ID: 1ADA699B145A2D1C
7 changed files with 62 additions and 43 deletions

View File

@ -34,10 +34,20 @@ var buildCmd = &cobra.Command{
Use: "build <package name> <package name> <package name> ...",
Short: "build a package or a tree",
Long: `build packages or trees from luet tree definitions. Packages are in [category]/[name]-[version] form`,
PreRun: func(cmd *cobra.Command, args []string) {
viper.BindPFlag("tree", cmd.Flags().Lookup("tree"))
viper.BindPFlag("destination", cmd.Flags().Lookup("destination"))
viper.BindPFlag("backend", cmd.Flags().Lookup("backend"))
viper.BindPFlag("concurrency", cmd.Flags().Lookup("concurrency"))
viper.BindPFlag("privileged", cmd.Flags().Lookup("privileged"))
viper.BindPFlag("database", cmd.Flags().Lookup("database"))
viper.BindPFlag("revdeps", cmd.Flags().Lookup("revdeps"))
viper.BindPFlag("all", cmd.Flags().Lookup("all"))
},
Run: func(cmd *cobra.Command, args []string) {
src := viper.GetString("tree")
dst := viper.GetString("output")
dst := viper.GetString("destination")
concurrency := viper.GetInt("concurrency")
backendType := viper.GetString("backend")
privileged := viper.GetBool("privileged")
@ -70,6 +80,8 @@ var buildCmd = &cobra.Command{
generalRecipe := tree.NewCompilerRecipe(db)
Info("Loading", src)
Info("Building in", dst)
err := generalRecipe.Load(src)
if err != nil {
Fatal("Error: " + err.Error())
@ -110,6 +122,7 @@ var buildCmd = &cobra.Command{
Fatal("Error: " + err.Error())
}
Info(":package: Selecting ", p.GetName(), p.GetVersion())
spec.SetOutputPath(dst)
compilerSpecs.Add(spec)
}
}
@ -141,21 +154,13 @@ func init() {
Fatal(err)
}
buildCmd.Flags().String("tree", path, "Source luet tree")
viper.BindPFlag("tree", buildCmd.Flags().Lookup("tree"))
buildCmd.Flags().String("output", path, "Destination folder")
viper.BindPFlag("output", buildCmd.Flags().Lookup("output"))
buildCmd.Flags().String("backend", "docker", "backend used (docker,img)")
viper.BindPFlag("backend", buildCmd.Flags().Lookup("backend"))
buildCmd.Flags().Int("concurrency", runtime.NumCPU(), "Concurrency")
viper.BindPFlag("concurrency", buildCmd.Flags().Lookup("concurrency"))
buildCmd.Flags().Bool("privileged", false, "Privileged (Keep permissions)")
viper.BindPFlag("privileged", buildCmd.Flags().Lookup("privileged"))
buildCmd.Flags().String("database", "memory", "database used for solving (memory,boltdb)")
viper.BindPFlag("database", buildCmd.Flags().Lookup("database"))
buildCmd.Flags().Bool("revdeps", false, "Build with revdeps")
viper.BindPFlag("revdeps", buildCmd.Flags().Lookup("revdeps"))
buildCmd.Flags().Bool("all", false, "Build all packages in the tree")
viper.BindPFlag("all", buildCmd.Flags().Lookup("all"))
buildCmd.Flags().String("destination", path, "Destination folder")
RootCmd.AddCommand(buildCmd)
}

View File

@ -31,6 +31,11 @@ var convertCmd = &cobra.Command{
Use: "convert",
Short: "convert other package manager tree into luet",
Long: `Parses external PM and produces a luet parsable tree`,
PreRun: func(cmd *cobra.Command, args []string) {
viper.BindPFlag("type", cmd.Flags().Lookup("type"))
viper.BindPFlag("concurrency", cmd.Flags().Lookup("concurrency"))
viper.BindPFlag("database", cmd.Flags().Lookup("database"))
},
Run: func(cmd *cobra.Command, args []string) {
t := viper.GetString("type")
@ -87,11 +92,8 @@ var convertCmd = &cobra.Command{
func init() {
convertCmd.Flags().String("type", "gentoo", "source type")
viper.BindPFlag("type", convertCmd.Flags().Lookup("type"))
convertCmd.Flags().Int("concurrency", runtime.NumCPU(), "Concurrency")
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)
}

View File

@ -30,6 +30,14 @@ var createrepoCmd = &cobra.Command{
Use: "create-repo",
Short: "Create a luet repository from a build",
Long: `Generate and renew repository metadata`,
PreRun: func(cmd *cobra.Command, args []string) {
viper.BindPFlag("packages", cmd.Flags().Lookup("packages"))
viper.BindPFlag("tree", cmd.Flags().Lookup("tree"))
viper.BindPFlag("output", cmd.Flags().Lookup("output"))
viper.BindPFlag("name", cmd.Flags().Lookup("name"))
viper.BindPFlag("uri", cmd.Flags().Lookup("uri"))
viper.BindPFlag("type", cmd.Flags().Lookup("type"))
},
Run: func(cmd *cobra.Command, args []string) {
tree := viper.GetString("tree")
@ -56,20 +64,11 @@ func init() {
Fatal(err)
}
createrepoCmd.Flags().String("packages", path, "Packages folder (output from build)")
viper.BindPFlag("packages", createrepoCmd.Flags().Lookup("packages"))
createrepoCmd.Flags().String("tree", path, "Source luet tree")
viper.BindPFlag("tree", createrepoCmd.Flags().Lookup("tree"))
createrepoCmd.Flags().String("output", path, "Destination folder")
viper.BindPFlag("output", createrepoCmd.Flags().Lookup("output"))
createrepoCmd.Flags().String("name", "luet", "Repository name")
viper.BindPFlag("name", createrepoCmd.Flags().Lookup("name"))
createrepoCmd.Flags().String("uri", path, "Repository uri")
viper.BindPFlag("uri", createrepoCmd.Flags().Lookup("uri"))
createrepoCmd.Flags().String("type", "local", "Repository type (local)")
viper.BindPFlag("type", createrepoCmd.Flags().Lookup("type"))
RootCmd.AddCommand(createrepoCmd)
}

View File

@ -32,13 +32,19 @@ import (
var installCmd = &cobra.Command{
Use: "install <pkg1> <pkg2> ...",
Short: "Install a package",
Long: `Install packages in parallel`,
PreRun: func(cmd *cobra.Command, args []string) {
viper.BindPFlag("system-dbpath", cmd.Flags().Lookup("system-dbpath"))
viper.BindPFlag("system-target", cmd.Flags().Lookup("system-target"))
viper.BindPFlag("concurrency", cmd.Flags().Lookup("concurrency"))
},
Long: `Install packages in parallel`,
Run: func(cmd *cobra.Command, args []string) {
c := installer.Repositories{}
c := []*installer.LuetRepository{}
err := viper.UnmarshalKey("system-repositories", &c)
if err != nil {
Fatal("Error: " + err.Error())
}
var toInstall []pkg.Package
for _, a := range args {
@ -55,11 +61,22 @@ var installCmd = &cobra.Command{
}
// This shouldn't be necessary, but we need to unmarshal the repositories to a concrete struct, thus we need to port them back to the Repositories type
synced := installer.Repositories{}
for _, toSync := range c {
s, err := toSync.Sync()
if err != nil {
Fatal("Error: " + err.Error())
}
synced = append(synced, s)
}
inst := installer.NewLuetInstaller(viper.GetInt("concurrency"))
inst.Repositories(c)
inst.Repositories(synced)
systemDB := pkg.NewBoltDatabase(filepath.Join(viper.GetString("system-dbpath"), "luet"))
os.MkdirAll(viper.GetString("system-dbpath"), os.ModePerm)
systemDB := pkg.NewBoltDatabase(filepath.Join(viper.GetString("system-dbpath"), "luet.db"))
system := &installer.System{Database: systemDB, Target: viper.GetString("system-target")}
err = inst.Install(toInstall, system)
if err != nil {
@ -74,13 +91,8 @@ func init() {
Fatal(err)
}
installCmd.Flags().String("system-dbpath", path, "System db path")
viper.BindPFlag("system-dbpath", installCmd.Flags().Lookup("system-dbpath"))
installCmd.Flags().String("system-target", path, "System rootpath")
viper.BindPFlag("system-target", installCmd.Flags().Lookup("system-target"))
installCmd.Flags().Int("concurrency", runtime.NumCPU(), "Concurrency")
viper.BindPFlag("concurrency", installCmd.Flags().Lookup("concurrency"))
RootCmd.AddCommand(installCmd)
}

View File

@ -33,6 +33,10 @@ 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`,
PreRun: func(cmd *cobra.Command, args []string) {
viper.BindPFlag("input", cmd.Flags().Lookup("input"))
viper.BindPFlag("database", cmd.Flags().Lookup("database"))
},
Run: func(cmd *cobra.Command, args []string) {
input := viper.GetString("input")
@ -104,9 +108,7 @@ var queryCmd = &cobra.Command{
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)
}

View File

@ -33,6 +33,11 @@ var uninstallCmd = &cobra.Command{
Use: "uninstall <pkg>",
Short: "Uninstall a package",
Long: `Uninstall packages`,
PreRun: func(cmd *cobra.Command, args []string) {
viper.BindPFlag("system-dbpath", cmd.Flags().Lookup("system-dbpath"))
viper.BindPFlag("system-target", cmd.Flags().Lookup("system-target"))
viper.BindPFlag("concurrency", cmd.Flags().Lookup("concurrency"))
},
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 1 {
Fatal("Wrong number of args")
@ -50,8 +55,8 @@ var uninstallCmd = &cobra.Command{
version := packageInfo[0][7]
inst := installer.NewLuetInstaller(viper.GetInt("concurrency"))
systemDB := pkg.NewBoltDatabase(filepath.Join(viper.GetString("system-dbpath"), "luet"))
os.MkdirAll(viper.GetString("system-dbpath"), os.ModePerm)
systemDB := pkg.NewBoltDatabase(filepath.Join(viper.GetString("system-dbpath"), "luet.db"))
system := &installer.System{Database: systemDB, Target: viper.GetString("system-target")}
err = inst.Uninstall(&pkg.DefaultPackage{Name: name, Category: category, Version: version}, system)
if err != nil {
@ -66,13 +71,7 @@ func init() {
Fatal(err)
}
uninstallCmd.Flags().String("system-dbpath", path, "System db path")
viper.BindPFlag("system-dbpath", uninstallCmd.Flags().Lookup("system-dbpath"))
uninstallCmd.Flags().String("system-target", path, "System rootpath")
viper.BindPFlag("system-target", uninstallCmd.Flags().Lookup("system-target"))
uninstallCmd.Flags().Int("concurrency", runtime.NumCPU(), "Concurrency")
viper.BindPFlag("concurrency", uninstallCmd.Flags().Lookup("concurrency"))
RootCmd.AddCommand(uninstallCmd)
}

View File

@ -115,7 +115,7 @@ func (l *LuetInstaller) Install(p []pkg.Package, s *System) error {
// Get installed definition
installed, err := s.World()
if err != nil {
return errors.Wrap(err, "Failed generating installed world ")
return errors.Wrap(err, "Failed generating installed world")
}
// compute a "big" world