Add create-repo, install and uninstall to cli

This commit is contained in:
Ettore Di Giacinto 2019-11-24 18:28:39 +01:00
parent a8b350fd8e
commit 914ac68eea
No known key found for this signature in database
GPG Key ID: 1ADA699B145A2D1C
4 changed files with 242 additions and 1 deletions

75
cmd/create-repo.go Normal file
View File

@ -0,0 +1,75 @@
// 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 (
"os"
installer "github.com/mudler/luet/pkg/installer"
. "github.com/mudler/luet/pkg/logger"
pkg "github.com/mudler/luet/pkg/package"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var createrepoCmd = &cobra.Command{
Use: "create-repo",
Short: "Create a luet repository from a build",
Long: `Generate and renew repository metadata`,
Run: func(cmd *cobra.Command, args []string) {
tree := viper.GetString("tree")
dst := viper.GetString("output")
packages := viper.GetString("packages")
name := viper.GetString("name")
uri := viper.GetString("uri")
t := viper.GetString("type")
repo, err := installer.GenerateRepository(name, uri, t, 1, packages, tree, pkg.NewInMemoryDatabase(false))
if err != nil {
Fatal("Error: " + err.Error())
}
err = repo.Write(dst)
if err != nil {
Fatal("Error: " + err.Error())
}
},
}
func init() {
path, err := os.Getwd()
if err != nil {
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)
}

86
cmd/install.go Normal file
View File

@ -0,0 +1,86 @@
// 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 (
"os"
"path/filepath"
"regexp"
"runtime"
installer "github.com/mudler/luet/pkg/installer"
. "github.com/mudler/luet/pkg/logger"
pkg "github.com/mudler/luet/pkg/package"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var installCmd = &cobra.Command{
Use: "install <pkg1> <pkg2> ...",
Short: "Install a package",
Long: `Install packages in parallel`,
Run: func(cmd *cobra.Command, args []string) {
c := installer.Repositories{}
err := viper.UnmarshalKey("system-repositories", &c)
if err != nil {
Fatal("Error: " + err.Error())
}
var toInstall []pkg.Package
for _, a := range args {
decodepackage, err := regexp.Compile(`^([<>]?\~?=?)((([^\/]+)\/)?(?U)(\S+))(-(\d+(\.\d+)*[a-z]?(_(alpha|beta|pre|rc|p)\d*)*(-r\d+)?))?$`)
if err != nil {
Fatal("Error: " + err.Error())
}
packageInfo := decodepackage.FindAllStringSubmatch(a, -1)
category := packageInfo[0][4]
name := packageInfo[0][5]
version := packageInfo[0][7]
toInstall = append(toInstall, &pkg.DefaultPackage{Name: name, Category: category, Version: version})
}
inst := installer.NewLuetInstaller(viper.GetInt("concurrency"))
inst.Repositories(c)
systemDB := pkg.NewBoltDatabase(filepath.Join(viper.GetString("system-dbpath"), "luet"))
system := &installer.System{Database: systemDB, Target: viper.GetString("system-target")}
err = inst.Install(toInstall, system)
if err != nil {
Fatal("Error: " + err.Error())
}
},
}
func init() {
path, err := os.Getwd()
if err != nil {
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

@ -61,7 +61,7 @@ func initConfig() {
Error(err)
os.Exit(1)
}
viper.SetConfigType("yaml")
viper.SetConfigName(".luet") // name of config file (without extension)
if cfgFile != "" { // enable ability to specify config file via flag
Info(">>> cfgFile: ", cfgFile)
@ -75,6 +75,8 @@ func initConfig() {
viper.AddConfigPath(dir)
viper.AddConfigPath(".")
viper.AddConfigPath("$HOME")
viper.AddConfigPath("/etc/luet")
viper.AutomaticEnv() // read in environment variables that match
// If a config file is found, read it in.

78
cmd/uninstall.go Normal file
View File

@ -0,0 +1,78 @@
// 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 (
"os"
"path/filepath"
"regexp"
"runtime"
installer "github.com/mudler/luet/pkg/installer"
. "github.com/mudler/luet/pkg/logger"
pkg "github.com/mudler/luet/pkg/package"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var uninstallCmd = &cobra.Command{
Use: "uninstall <pkg>",
Short: "Uninstall a package",
Long: `Uninstall packages`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 1 {
Fatal("Wrong number of args")
}
a := args[0]
decodepackage, err := regexp.Compile(`^([<>]?\~?=?)((([^\/]+)\/)?(?U)(\S+))(-(\d+(\.\d+)*[a-z]?(_(alpha|beta|pre|rc|p)\d*)*(-r\d+)?))?$`)
if err != nil {
Fatal("Error: " + err.Error())
}
packageInfo := decodepackage.FindAllStringSubmatch(a, -1)
category := packageInfo[0][4]
name := packageInfo[0][5]
version := packageInfo[0][7]
inst := installer.NewLuetInstaller(viper.GetInt("concurrency"))
systemDB := pkg.NewBoltDatabase(filepath.Join(viper.GetString("system-dbpath"), "luet"))
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 {
Fatal("Error: " + err.Error())
}
},
}
func init() {
path, err := os.Getwd()
if err != nil {
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)
}