2019-11-24 17:28:39 +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 (
2019-12-29 15:43:32 +00:00
"fmt"
2019-11-24 17:28:39 +00:00
"os"
"path/filepath"
installer "github.com/mudler/luet/pkg/installer"
2019-12-29 15:33:10 +00:00
. "github.com/mudler/luet/pkg/config"
2019-11-24 17:28:39 +00:00
. "github.com/mudler/luet/pkg/logger"
pkg "github.com/mudler/luet/pkg/package"
2019-12-29 15:43:32 +00:00
_gentoo "github.com/Sabayon/pkgs-checker/pkg/gentoo"
2019-11-24 17:28:39 +00:00
"github.com/spf13/cobra"
)
var installCmd = & cobra . Command {
Use : "install <pkg1> <pkg2> ..." ,
Short : "Install a package" ,
2019-11-25 18:55:30 +00:00
PreRun : func ( cmd * cobra . Command , args [ ] string ) {
2019-12-31 00:21:06 +00:00
LuetCfg . Viper . BindPFlag ( "system.database_path" , cmd . Flags ( ) . Lookup ( "system-dbpath" ) )
LuetCfg . Viper . BindPFlag ( "system.rootfs" , cmd . Flags ( ) . Lookup ( "system-target" ) )
2019-11-25 18:55:30 +00:00
} ,
Long : ` Install packages in parallel ` ,
2019-11-24 17:28:39 +00:00
Run : func ( cmd * cobra . Command , args [ ] string ) {
var toInstall [ ] pkg . Package
2019-12-31 00:21:06 +00:00
var systemDB pkg . PackageDatabase
2019-11-24 17:28:39 +00:00
for _ , a := range args {
2019-12-29 15:43:32 +00:00
gp , err := _gentoo . ParsePackageStr ( a )
2019-11-24 17:28:39 +00:00
if err != nil {
2019-12-29 15:43:32 +00:00
Fatal ( "Invalid package string " , a , ": " , err . Error ( ) )
2019-11-24 17:28:39 +00:00
}
2019-12-29 15:43:32 +00:00
if gp . Version == "" {
gp . Version = "0"
gp . Condition = _gentoo . PkgCondGreaterEqual
}
2019-11-24 17:28:39 +00:00
2019-12-29 15:43:32 +00:00
pack := & pkg . DefaultPackage {
Name : gp . Name ,
Version : fmt . Sprintf ( "%s%s%s" , gp . Condition . String ( ) , gp . Version , gp . VersionSuffix ) ,
Category : gp . Category ,
Uri : make ( [ ] string , 0 ) ,
}
toInstall = append ( toInstall , pack )
2019-11-24 17:28:39 +00:00
}
2019-11-25 18:55:30 +00:00
// 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 { }
2019-12-31 00:21:06 +00:00
for _ , repo := range LuetCfg . SystemRepositories {
if ! repo . Enable {
continue
}
toSync := installer . NewSystemRepository ( & repo )
2019-11-25 18:55:30 +00:00
s , err := toSync . Sync ( )
if err != nil {
Fatal ( "Error: " + err . Error ( ) )
}
synced = append ( synced , s )
}
2019-12-29 15:33:10 +00:00
inst := installer . NewLuetInstaller ( LuetCfg . GetGeneral ( ) . Concurrency )
2019-11-25 18:55:30 +00:00
inst . Repositories ( synced )
2019-11-24 17:28:39 +00:00
2019-12-31 00:21:06 +00:00
if LuetCfg . GetSystem ( ) . DatabaseEngine == "boltdb" {
os . MkdirAll (
filepath . Join ( LuetCfg . GetSystem ( ) . Rootfs , LuetCfg . GetSystem ( ) . DatabasePath ) ,
os . ModePerm ,
)
systemDB = pkg . NewBoltDatabase (
filepath . Join ( LuetCfg . GetSystem ( ) . Rootfs ,
filepath . Join ( LuetCfg . GetSystem ( ) . DatabasePath , "luet.db" ) ) )
} else {
systemDB = pkg . NewInMemoryDatabase ( true )
}
system := & installer . System { Database : systemDB , Target : LuetCfg . GetSystem ( ) . Rootfs }
err := inst . Install ( toInstall , system )
2019-11-24 17:28:39 +00:00
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" )
installCmd . Flags ( ) . String ( "system-target" , path , "System rootpath" )
RootCmd . AddCommand ( installCmd )
}