2018-11-07 10:35:04 +00:00
|
|
|
package pkg
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
)
|
|
|
|
|
|
|
|
var ErrUnknownDistribution = errors.New("unknown linux distribution")
|
|
|
|
var ErrNoCandidateInstalled = errors.New("no package-manager candidate installed")
|
|
|
|
var ErrNoDriverFound = errors.New("no driver found for the package manager")
|
|
|
|
|
2018-11-07 11:52:59 +00:00
|
|
|
func Load(_distro string) (PackageManager, error) {
|
2018-11-07 10:35:04 +00:00
|
|
|
|
2018-11-07 11:52:59 +00:00
|
|
|
// 1. load config file
|
2018-11-07 10:35:04 +00:00
|
|
|
driverTable := filepath.Join(os.Getenv("GOPATH"), "src/git.xdrm.io/xdrm-brackets/nix-amer/meta/pkg-drivers.json")
|
|
|
|
file, err := os.Open(driverTable)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
2018-11-07 11:52:59 +00:00
|
|
|
// 2. Parse json
|
2018-11-07 10:35:04 +00:00
|
|
|
table := make(map[string][]string)
|
|
|
|
decoder := json.NewDecoder(file)
|
|
|
|
if err := decoder.Decode(&table); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
decoder = nil
|
|
|
|
|
2018-11-07 11:52:59 +00:00
|
|
|
// 3. Get available package-manager list for distro
|
|
|
|
available, exists := table[_distro]
|
2018-11-07 10:35:04 +00:00
|
|
|
if !exists || len(available) < 1 {
|
|
|
|
return nil, ErrUnknownDistribution
|
|
|
|
}
|
|
|
|
|
2018-11-07 11:52:59 +00:00
|
|
|
// 4. Check each available package-manager in order
|
2018-11-07 10:35:04 +00:00
|
|
|
selected := ""
|
|
|
|
for _, current := range available {
|
|
|
|
if exec.Command("which", current).Run() == nil {
|
|
|
|
selected = current
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// no candidate installed
|
|
|
|
if len(selected) < 1 {
|
|
|
|
return nil, ErrNoCandidateInstalled
|
|
|
|
}
|
|
|
|
|
2018-11-07 11:52:59 +00:00
|
|
|
// 5. Instanciate
|
2018-11-07 10:35:04 +00:00
|
|
|
switch selected {
|
|
|
|
case "apt-get":
|
|
|
|
return new(Apt), nil
|
|
|
|
case "apk":
|
|
|
|
return new(Apk), nil
|
|
|
|
case "eopkg":
|
|
|
|
return new(Eopkg), nil
|
|
|
|
case "pacman":
|
|
|
|
return new(Pacman), nil
|
|
|
|
case "dnf":
|
|
|
|
return new(Dnf), nil
|
|
|
|
case "yum":
|
|
|
|
return new(Yum), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, ErrNoDriverFound
|
|
|
|
|
|
|
|
}
|