2018-02-02 17:36:48 +01:00
package managers
import "github.com/lxc/distrobuilder/shared"
// ManagerFlags represents flags for all subcommands of a package manager.
type ManagerFlags struct {
global [ ] string
install [ ] string
remove [ ] string
clean [ ] string
update [ ] string
refresh [ ] string
}
// ManagerHooks represents custom hooks.
type ManagerHooks struct {
clean func ( ) error
}
// A Manager represents a package manager.
type Manager struct {
2018-12-28 21:28:47 +01:00
command string
flags ManagerFlags
hooks ManagerHooks
RepoHandler func ( repoAction shared . DefinitionPackagesRepository ) error
2018-02-02 17:36:48 +01:00
}
// Get returns a Manager specified by name.
func Get ( name string ) * Manager {
switch name {
2018-03-26 16:34:19 +02:00
case "apk" :
return NewApk ( )
2018-02-02 17:36:48 +01:00
case "apt" :
return NewApt ( )
2018-03-26 16:34:19 +02:00
case "dnf" :
return NewDnf ( )
2018-02-02 17:36:48 +01:00
case "pacman" :
return NewPacman ( )
2018-04-03 15:27:37 +02:00
case "portage" :
return NewPortage ( )
2018-02-02 17:36:48 +01:00
case "yum" :
return NewYum ( )
2018-12-28 21:45:42 +01:00
case "equo" :
return NewEquo ( )
2019-02-26 13:29:28 +01:00
case "zypper" :
return NewZypper ( )
2018-02-02 17:36:48 +01:00
}
return nil
}
// Install installs packages to the rootfs.
func ( m Manager ) Install ( pkgs [ ] string ) error {
if len ( m . flags . install ) == 0 || pkgs == nil || len ( pkgs ) == 0 {
return nil
}
args := append ( m . flags . global , m . flags . install ... )
args = append ( args , pkgs ... )
return shared . RunCommand ( m . command , args ... )
}
// Remove removes packages from the rootfs.
func ( m Manager ) Remove ( pkgs [ ] string ) error {
if len ( m . flags . remove ) == 0 || pkgs == nil || len ( pkgs ) == 0 {
return nil
}
args := append ( m . flags . global , m . flags . remove ... )
args = append ( args , pkgs ... )
return shared . RunCommand ( m . command , args ... )
}
// Clean cleans up cached files used by the package managers.
func ( m Manager ) Clean ( ) error {
var err error
if len ( m . flags . clean ) == 0 {
return nil
}
args := append ( m . flags . global , m . flags . clean ... )
err = shared . RunCommand ( m . command , args ... )
if err != nil {
return err
}
if m . hooks . clean != nil {
err = m . hooks . clean ( )
}
return err
}
// Refresh refreshes the local package database.
func ( m Manager ) Refresh ( ) error {
if len ( m . flags . refresh ) == 0 {
return nil
}
args := append ( m . flags . global , m . flags . refresh ... )
return shared . RunCommand ( m . command , args ... )
}
// Update updates all packages.
func ( m Manager ) Update ( ) error {
if len ( m . flags . update ) == 0 {
return nil
}
args := append ( m . flags . global , m . flags . update ... )
return shared . RunCommand ( m . command , args ... )
}