This commit is contained in:
2025-01-03 23:30:07 -06:00
parent 01efeab13f
commit b5f7c3fd72
818 changed files with 1112 additions and 82997 deletions

95
pkg/pkgman/apt/apt.go Normal file
View File

@ -0,0 +1,95 @@
package apt
import (
"fmt"
"git.andrewnw.xyz/CyberShell/backy/pkg/pkgman/pkgcommon"
)
// AptManager implements PackageManager for systems using APT.
type AptManager struct {
useAuth bool // Whether to use an authentication command
authCommand string // The authentication command, e.g., "sudo"
}
// DefaultAuthCommand is the default command used for authentication.
const DefaultAuthCommand = "sudo"
const DefaultPackageCommand = "apt-get"
// NewAptManager creates a new AptManager with default settings.
func NewAptManager() *AptManager {
return &AptManager{
useAuth: true,
authCommand: DefaultAuthCommand,
}
}
// Install returns the command and arguments for installing a package.
func (a *AptManager) Install(pkg, version string, args []string) (string, []string) {
baseCmd := a.prependAuthCommand(DefaultPackageCommand)
baseArgs := []string{"update", "&&", baseCmd, "install", "-y"}
if version != "" {
baseArgs = append(baseArgs, fmt.Sprintf("%s=%s", pkg, version))
} else {
baseArgs = append(baseArgs, pkg)
}
if args != nil {
baseArgs = append(baseArgs, args...)
}
return baseCmd, baseArgs
}
// Remove returns the command and arguments for removing a package.
func (a *AptManager) Remove(pkg string, args []string) (string, []string) {
baseCmd := a.prependAuthCommand(DefaultPackageCommand)
baseArgs := []string{"remove", "-y", pkg}
if args != nil {
baseArgs = append(baseArgs, args...)
}
return baseCmd, baseArgs
}
// Upgrade returns the command and arguments for upgrading a specific package.
func (a *AptManager) Upgrade(pkg, version string) (string, []string) {
baseCmd := a.prependAuthCommand(DefaultPackageCommand)
baseArgs := []string{"update", "&&", baseCmd, "install", "--only-upgrade", "-y "}
if version != "" {
baseArgs = append(baseArgs, fmt.Sprintf("%s=%s", pkg, version))
} else {
baseArgs = append(baseArgs, pkg)
}
return baseCmd, baseArgs
}
// UpgradeAll returns the command and arguments for upgrading all packages.
func (a *AptManager) UpgradeAll() (string, []string) {
baseCmd := a.prependAuthCommand(DefaultPackageCommand)
baseArgs := []string{"update", "&&", baseCmd, "upgrade", "-y"}
return baseCmd, baseArgs
}
// Configure applies functional options to customize the package manager.
func (a *AptManager) Configure(options ...pkgcommon.PackageManagerOption) {
for _, opt := range options {
opt(a)
}
}
// prependAuthCommand prepends the authentication command if UseAuth is true.
func (a *AptManager) prependAuthCommand(baseCmd string) string {
if a.useAuth {
return a.authCommand + " " + baseCmd
}
return baseCmd
}
// SetUseAuth enables or disables authentication.
func (a *AptManager) SetUseAuth(useAuth bool) {
a.useAuth = useAuth
}
// SetAuthCommand sets the authentication command.
func (a *AptManager) SetAuthCommand(authCommand string) {
a.authCommand = authCommand
}

93
pkg/pkgman/dnf/dnf.go Normal file
View File

@ -0,0 +1,93 @@
package dnf
import (
"fmt"
"git.andrewnw.xyz/CyberShell/backy/pkg/pkgman/pkgcommon"
)
// DnfManager implements PackageManager for systems using YUM.
type DnfManager struct {
useAuth bool // Whether to use an authentication command
authCommand string // The authentication command, e.g., "sudo"
}
// DefaultAuthCommand is the default command used for authentication.
const DefaultAuthCommand = "sudo"
// NewDnfManager creates a new DnfManager with default settings.
func NewDnfManager() *DnfManager {
return &DnfManager{
useAuth: true,
authCommand: DefaultAuthCommand,
}
}
// Configure applies functional options to customize the package manager.
func (y *DnfManager) Configure(options ...pkgcommon.PackageManagerOption) {
for _, opt := range options {
opt(y)
}
}
// Install returns the command and arguments for installing a package.
func (y *DnfManager) Install(pkg, version string, args []string) (string, []string) {
baseCmd := y.prependAuthCommand("dnf")
baseArgs := []string{"install", "-y"}
if version != "" {
baseArgs = append(baseArgs, fmt.Sprintf("%s-%s", pkg, version))
} else {
baseArgs = append(baseArgs, pkg)
}
if args != nil {
baseArgs = append(baseArgs, args...)
}
return baseCmd, baseArgs
}
// Remove returns the command and arguments for removing a package.
func (y *DnfManager) Remove(pkg string, args []string) (string, []string) {
baseCmd := y.prependAuthCommand("dnf")
baseArgs := []string{"remove", "-y", pkg}
if args != nil {
baseArgs = append(baseArgs, args...)
}
return baseCmd, baseArgs
}
// Upgrade returns the command and arguments for upgrading a specific package.
func (y *DnfManager) Upgrade(pkg, version string) (string, []string) {
baseCmd := y.prependAuthCommand("dnf")
baseArgs := []string{"update", "-y"}
if version != "" {
baseArgs = append(baseArgs, fmt.Sprintf("%s-%s", pkg, version))
} else {
baseArgs = append(baseArgs, pkg)
}
return baseCmd, baseArgs
}
// UpgradeAll returns the command and arguments for upgrading all packages.
func (y *DnfManager) UpgradeAll() (string, []string) {
baseCmd := y.prependAuthCommand("dnf")
baseArgs := []string{"update", "-y"}
return baseCmd, baseArgs
}
// prependAuthCommand prepends the authentication command if UseAuth is true.
func (y *DnfManager) prependAuthCommand(baseCmd string) string {
if y.useAuth {
return y.authCommand + " " + baseCmd
}
return baseCmd
}
// SetUseAuth enables or disables authentication.
func (y *DnfManager) SetUseAuth(useAuth bool) {
y.useAuth = useAuth
}
// SetAuthCommand sets the authentication command.
func (y *DnfManager) SetAuthCommand(authCommand string) {
y.authCommand = authCommand
}

View File

@ -0,0 +1,4 @@
package pkgcommon
// PackageManagerOption defines a functional option for configuring a PackageManager.
type PackageManagerOption func(interface{})

72
pkg/pkgman/pkgman.go Normal file
View File

@ -0,0 +1,72 @@
package pkgman
import (
"fmt"
"git.andrewnw.xyz/CyberShell/backy/pkg/pkgman/apt"
"git.andrewnw.xyz/CyberShell/backy/pkg/pkgman/dnf"
"git.andrewnw.xyz/CyberShell/backy/pkg/pkgman/pkgcommon"
"git.andrewnw.xyz/CyberShell/backy/pkg/pkgman/yum"
)
// PackageManager is an interface used to define common package commands. This shall be implemented by every package.
type PackageManager interface {
Install(pkg, version string, args []string) (string, []string)
Remove(pkg string, args []string) (string, []string)
Upgrade(pkg, version string) (string, []string) // Upgrade a specific package
UpgradeAll() (string, []string)
// Configure applies functional options to customize the package manager.
Configure(options ...pkgcommon.PackageManagerOption)
}
// PackageManagerFactory returns the appropriate PackageManager based on the package tool.
// Takes variable number of options.
func PackageManagerFactory(managerType string, options ...pkgcommon.PackageManagerOption) (PackageManager, error) {
var manager PackageManager
switch managerType {
case "apt":
manager = apt.NewAptManager()
case "yum":
manager = yum.NewYumManager()
case "dnf":
manager = dnf.NewDnfManager()
default:
return nil, fmt.Errorf("unsupported package manager: %s", managerType)
}
// Apply options to the manager
manager.Configure(options...)
return manager, nil
}
// WithAuth enables authentication and sets the authentication command.
func WithAuth(authCommand string) pkgcommon.PackageManagerOption {
return func(manager interface{}) {
if configurable, ok := manager.(interface {
SetUseAuth(bool)
SetAuthCommand(string)
}); ok {
configurable.SetUseAuth(true)
configurable.SetAuthCommand(authCommand)
}
}
}
// WithoutAuth disables authentication.
func WithoutAuth() pkgcommon.PackageManagerOption {
return func(manager interface{}) {
if configurable, ok := manager.(interface {
SetUseAuth(bool)
}); ok {
configurable.SetUseAuth(false)
}
}
}
// ConfigurablePackageManager defines methods for setting configuration options.
type ConfigurablePackageManager interface {
SetUseAuth(useAuth bool)
SetAuthCommand(authCommand string)
}

93
pkg/pkgman/yum/yum.go Normal file
View File

@ -0,0 +1,93 @@
package yum
import (
"fmt"
"git.andrewnw.xyz/CyberShell/backy/pkg/pkgman/pkgcommon"
)
// YumManager implements PackageManager for systems using YUM.
type YumManager struct {
useAuth bool // Whether to use an authentication command
authCommand string // The authentication command, e.g., "sudo"
}
// DefaultAuthCommand is the default command used for authentication.
const DefaultAuthCommand = "sudo"
// NewYumManager creates a new YumManager with default settings.
func NewYumManager() *YumManager {
return &YumManager{
useAuth: true,
authCommand: DefaultAuthCommand,
}
}
// Configure applies functional options to customize the package manager.
func (y *YumManager) Configure(options ...pkgcommon.PackageManagerOption) {
for _, opt := range options {
opt(y)
}
}
// Install returns the command and arguments for installing a package.
func (y *YumManager) Install(pkg, version string, args []string) (string, []string) {
baseCmd := y.prependAuthCommand("yum")
baseArgs := []string{"install", "-y"}
if version != "" {
baseArgs = append(baseArgs, fmt.Sprintf("%s-%s", pkg, version))
} else {
baseArgs = append(baseArgs, pkg)
}
if args != nil {
baseArgs = append(baseArgs, args...)
}
return baseCmd, baseArgs
}
// Remove returns the command and arguments for removing a package.
func (y *YumManager) Remove(pkg string, args []string) (string, []string) {
baseCmd := y.prependAuthCommand("yum")
baseArgs := []string{"remove", "-y", pkg}
if args != nil {
baseArgs = append(baseArgs, args...)
}
return baseCmd, baseArgs
}
// Upgrade returns the command and arguments for upgrading a specific package.
func (y *YumManager) Upgrade(pkg, version string) (string, []string) {
baseCmd := y.prependAuthCommand("yum")
baseArgs := []string{"update", "-y"}
if version != "" {
baseArgs = append(baseArgs, fmt.Sprintf("%s-%s", pkg, version))
} else {
baseArgs = append(baseArgs, pkg)
}
return baseCmd, baseArgs
}
// UpgradeAll returns the command and arguments for upgrading all packages.
func (y *YumManager) UpgradeAll() (string, []string) {
baseCmd := y.prependAuthCommand("yum")
baseArgs := []string{"update", "-y"}
return baseCmd, baseArgs
}
// prependAuthCommand prepends the authentication command if UseAuth is true.
func (y *YumManager) prependAuthCommand(baseCmd string) string {
if y.useAuth {
return y.authCommand + " " + baseCmd
}
return baseCmd
}
// SetUseAuth enables or disables authentication.
func (y *YumManager) SetUseAuth(useAuth bool) {
y.useAuth = useAuth
}
// SetAuthCommand sets the authentication command.
func (y *YumManager) SetAuthCommand(authCommand string) {
y.authCommand = authCommand
}