2023-05-18 16:07:24 +00:00
|
|
|
package distro
|
|
|
|
|
2023-05-18 21:37:07 +00:00
|
|
|
import (
|
2023-05-23 22:36:46 +00:00
|
|
|
"errors"
|
2023-05-18 21:37:07 +00:00
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
var mu sync.Mutex
|
|
|
|
var distros []distribution
|
|
|
|
|
|
|
|
type distribution interface {
|
2023-05-23 16:24:01 +00:00
|
|
|
Distro() Distro
|
2023-05-18 21:37:07 +00:00
|
|
|
Equal(Distro) bool
|
|
|
|
Packages() (packages []string, err error)
|
2023-05-23 22:36:46 +00:00
|
|
|
Install(pkg string, headers bool) (err error)
|
2023-05-23 22:00:20 +00:00
|
|
|
Kernels() (kernels []KernelInfo, err error)
|
2023-05-18 21:37:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Register(d distribution) {
|
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
|
|
|
|
|
|
|
distros = append(distros, d)
|
|
|
|
}
|
|
|
|
|
2023-05-18 22:02:41 +00:00
|
|
|
func List() (dds []Distro) {
|
|
|
|
for _, dd := range distros {
|
2023-05-23 16:24:01 +00:00
|
|
|
dds = append(dds, dd.Distro())
|
2023-05-18 22:02:41 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-18 16:07:24 +00:00
|
|
|
type Distro struct {
|
|
|
|
ID ID
|
|
|
|
Release string
|
|
|
|
}
|
2023-05-18 21:37:07 +00:00
|
|
|
|
2023-05-24 16:29:52 +00:00
|
|
|
func (d Distro) String() string {
|
|
|
|
return d.ID.String() + " " + d.Release
|
|
|
|
}
|
|
|
|
|
2023-05-18 21:37:07 +00:00
|
|
|
func (d Distro) Packages() (packages []string, err error) {
|
|
|
|
for _, dd := range distros {
|
2023-05-23 16:24:01 +00:00
|
|
|
if d.ID != None && d.ID != dd.Distro().ID {
|
2023-05-18 21:37:07 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if d.Release != "" && !dd.Equal(d) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
var pkgs []string
|
|
|
|
pkgs, err = dd.Packages()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
packages = append(packages, pkgs...)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2023-05-23 22:00:20 +00:00
|
|
|
|
2023-05-23 22:36:46 +00:00
|
|
|
func (d Distro) Install(pkg string, headers bool) (err error) {
|
|
|
|
for _, dd := range distros {
|
|
|
|
if !dd.Equal(d) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
return dd.Install(pkg, headers)
|
|
|
|
}
|
|
|
|
return errors.New("not found")
|
|
|
|
}
|
|
|
|
|
2023-05-23 22:00:20 +00:00
|
|
|
func (d Distro) Kernels() (kernels []KernelInfo, err error) {
|
|
|
|
for _, dd := range distros {
|
|
|
|
if dd.Equal(d) {
|
|
|
|
return dd.Kernels()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2023-05-23 23:02:09 +00:00
|
|
|
|
|
|
|
func (d Distro) Equal(to Distro) bool {
|
|
|
|
for _, dd := range distros {
|
|
|
|
if dd.Equal(d) {
|
|
|
|
return dd.Equal(to)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|