update readme | add 6 package managers and the loader/seletcor

This commit is contained in:
xdrm-brackets 2018-11-07 11:35:04 +01:00
parent 9b3ad00f00
commit 0772f0b067
10 changed files with 280 additions and 15 deletions

View File

@ -93,13 +93,12 @@ Execute the \<script\> file.
### II. Path Expressions
The syntax is pretty fast-forward, it uses 3 levels to find your configuration line : `subject@alt.field`.
The syntax is pretty fast-forward, it uses 3 levels to find your configuration line : `[subject.path].[field.path]`.
| Field | Description | Example |
| --------- | :----------------------------------- | -------------------------- |
| `subject` | The name of the program to configure. Available program names are listed in this [table](tablefile). | `sshd`, `httpd`, `nginx` |
| `alt` | Alternative file if there is more than one for a package. If **omitted** it will default to the _main_ configuration file. | `httpd@main`, `http@ports` |
| `field` | Field is a dot-separated chain of strings that match with a configuration field. If **omitted**, the \<value\> will just be added at the end of the configuration file. In the same way if the field does not point to a raw field but a parent or group containing fields, the \<value\> will be added at the end. | `sshd.AllowGroups`, `nginx.http.gzip` |
| `subject.path` | Dot-separated path to the configuration file to edit. Available program paths are listed in this [table](tablefile). | `sshd`, `httpd`, `nginx`, `nginx.vhost.default` |
| `field.path` | Dot-separated chain of strings that match with a configuration field. If **omitted**, the \<value\> will just be added at the end of the configuration file. In the same way if the field does not point to a raw field but a parent or group containing fields, the \<value\> will be added at the end of the group. | `sshd.AllowGroups`, `nginx.http.gzip` |
@ -117,6 +116,7 @@ upd
ins nginx ssh sslh
cnf nginx.http.gzip on
cnf nginx.vhost.new-site ./localConfFile
ser enable nginx
ser start nginx

29
internal/pkg/apk.go Normal file
View File

@ -0,0 +1,29 @@
package pkg
import (
"os/exec"
)
type Apk struct{}
func (d Apk) Name() string { return "apk" }
func (d Apk) Fetch() bool {
_, err := exec.Command(d.Name(), "update").Output()
return err == nil
}
func (d Apk) Upgrade() bool {
_, err := exec.Command(d.Name(), "upgrade").Output()
return err == nil
}
func (d Apk) Install(_pkg string) bool {
_, err := exec.Command(d.Name(), "add", _pkg).Output()
return err == nil
}
func (d Apk) Remove(_pkg string) bool {
_, err := exec.Command(d.Name(), "del", _pkg).Output()
return err == nil
}

40
internal/pkg/apt.go Normal file
View File

@ -0,0 +1,40 @@
package pkg
import (
"os/exec"
)
type Apt struct{}
func (d Apt) Name() string { return "apt-get" }
func (d Apt) Fetch() bool {
_, err := exec.Command(d.Name(), "update").Output()
return err == nil
}
func (d Apt) Upgrade() bool {
_, err := exec.Command(d.Name(), "upgrade").Output()
if err != nil {
return false
}
_, err = exec.Command(d.Name(), "dist-upgrade").Output()
if err != nil {
return false
}
exec.Command(d.Name(), "autoremove").Run()
return true
}
func (d Apt) Install(_pkg string) bool {
_, err := exec.Command(d.Name(), "install", _pkg).Output()
return err == nil
}
func (d Apt) Remove(_pkg string) bool {
_, err := exec.Command(d.Name(), "remove", _pkg).Output()
exec.Command(d.Name(), "autoremove").Run()
return err == nil
}

View File

@ -2,6 +2,8 @@ package pkg
// PackageManager is the common interface for all package-manager drivers (e.g. `dpkg` for debian-based, `pacman` for arch)
type PackageManager interface {
// Name of executable (to check if installed and for debug)
Name() string
// Fetch updates the package cache/databse
Fetch() bool
// Upgrade already installed packages and whatever can be upgraded with warranty (e.g. kernel)

30
internal/pkg/dnf.go Normal file
View File

@ -0,0 +1,30 @@
package pkg
import (
"os/exec"
)
type Dnf struct{}
func (d Dnf) Name() string { return "dnf" }
func (d Dnf) Fetch() bool {
return true
}
func (d Dnf) Upgrade() bool {
_, err := exec.Command(d.Name(), "upgrade").Output()
exec.Command(d.Name(), "autoremove").Run()
return err == nil
}
func (d Dnf) Install(_pkg string) bool {
_, err := exec.Command(d.Name(), "install", _pkg).Output()
return err == nil
}
func (d Dnf) Remove(_pkg string) bool {
_, err := exec.Command(d.Name(), "remove", _pkg).Output()
exec.Command(d.Name(), "autoremove").Run()
return err == nil
}

30
internal/pkg/eopkg.go Normal file
View File

@ -0,0 +1,30 @@
package pkg
import (
"os/exec"
)
type Eopkg struct{}
func (d Eopkg) Name() string { return "eopkg" }
func (d Eopkg) Fetch() bool {
return true
}
func (d Eopkg) Upgrade() bool {
_, err := exec.Command(d.Name(), "upgrade").Output()
exec.Command(d.Name(), "remove-orphans").Run()
return err == nil
}
func (d Eopkg) Install(_pkg string) bool {
_, err := exec.Command(d.Name(), "install", _pkg).Output()
return err == nil
}
func (d Eopkg) Remove(_pkg string) bool {
_, err := exec.Command(d.Name(), "remove", _pkg).Output()
exec.Command(d.Name(), "remove-orphans").Run()
return err == nil
}

80
internal/pkg/loader.go Normal file
View File

@ -0,0 +1,80 @@
package pkg
import (
"encoding/json"
"errors"
"os"
"os/exec"
"path/filepath"
"strings"
)
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")
func Load() (PackageManager, error) {
// 1. get distro name
out, err := exec.Command("lsb_release", "-is").Output()
if err != nil {
return nil, err
}
distro := strings.ToLower(strings.Trim(string(out), " \n\t"))
// 2. load config file
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()
// 3. Parse json
table := make(map[string][]string)
decoder := json.NewDecoder(file)
if err := decoder.Decode(&table); err != nil {
return nil, err
}
decoder = nil
// 4. Get available package-manager list for distro
available, exists := table[distro]
if !exists || len(available) < 1 {
return nil, ErrUnknownDistribution
}
// 5. Check each available package-manager in order
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
}
// 6. Instanciate
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
}

30
internal/pkg/pacman.go Normal file
View File

@ -0,0 +1,30 @@
package pkg
import (
"os/exec"
)
type Pacman struct{}
func (d Pacman) Name() string { return "pacman" }
func (d Pacman) Fetch() bool {
return true
}
func (d Pacman) Upgrade() bool {
_, err := exec.Command(d.Name(), "-Syu").Output()
exec.Command(d.Name(), "-Ru").Run()
return err == nil
}
func (d Pacman) Install(_pkg string) bool {
_, err := exec.Command(d.Name(), "-S", _pkg).Output()
return err == nil
}
func (d Pacman) Remove(_pkg string) bool {
_, err := exec.Command(d.Name(), "-R", _pkg).Output()
exec.Command(d.Name(), "-Ru").Run()
return err == nil
}

28
internal/pkg/yum.go Normal file
View File

@ -0,0 +1,28 @@
package pkg
import (
"os/exec"
)
type Yum struct{}
func (d Yum) Name() string { return "yum" }
func (d Yum) Fetch() bool {
return true
}
func (d Yum) Upgrade() bool {
_, err := exec.Command(d.Name(), "update").Output()
return err == nil
}
func (d Yum) Install(_pkg string) bool {
_, err := exec.Command(d.Name(), "install", _pkg).Output()
return err == nil
}
func (d Yum) Remove(_pkg string) bool {
_, err := exec.Command(d.Name(), "remove", _pkg).Output()
return err == nil
}

View File

@ -1,17 +1,13 @@
{
"ubuntu": "dpkg",
"debian": "dpkg",
"ubuntu": [ "apt-get" ],
"debian": [ "apt-get" ],
"alpine": "apk",
"alpine": [ "apk" ],
"solus": "eopkg",
"solus": [ "eopkg" ],
"arch": "pacman",
"arch": [ "pacman" ],
"fedora": "rpm",
"rhel": "rpm",
"suse": "rpm",
"opensuse": "rpm",
"gentoo": "emerge"
"centos": [ "dnf", "yum" ],
"fedora": [ "dnf", "yum" ]
}