mirror of
https://github.com/gogrlx/snack.git
synced 2026-04-02 05:08:42 -07:00
Breaking change to Manager interface: Install/Remove/Purge now accept []Target instead of []string. Target supports Version pinning, FromRepo constraints, and Source paths — matching SaltStack's pkgs list semantics. Also adds WithRefresh, WithFromRepo, WithReinstall options.
78 lines
2.1 KiB
Go
78 lines
2.1 KiB
Go
package snack
|
|
|
|
// Package represents a system package.
|
|
type Package struct {
|
|
Name string `json:"name"`
|
|
Version string `json:"version"`
|
|
Description string `json:"description,omitempty"`
|
|
Arch string `json:"arch,omitempty"`
|
|
Repository string `json:"repository,omitempty"`
|
|
Installed bool `json:"installed"`
|
|
}
|
|
|
|
// Options holds configuration for package manager operations.
|
|
type Options struct {
|
|
Sudo bool
|
|
AssumeYes bool
|
|
DryRun bool
|
|
Root string // alternate root filesystem
|
|
Verbose bool
|
|
Refresh bool // refresh package index before operation
|
|
FromRepo string // constrain operation to a specific repository
|
|
Reinstall bool // reinstall already-installed packages
|
|
}
|
|
|
|
// Option is a functional option for package manager operations.
|
|
type Option func(*Options)
|
|
|
|
// WithSudo runs the command with sudo.
|
|
func WithSudo() Option {
|
|
return func(o *Options) { o.Sudo = true }
|
|
}
|
|
|
|
// WithAssumeYes automatically confirms prompts.
|
|
func WithAssumeYes() Option {
|
|
return func(o *Options) { o.AssumeYes = true }
|
|
}
|
|
|
|
// WithDryRun simulates the operation without making changes.
|
|
func WithDryRun() Option {
|
|
return func(o *Options) { o.DryRun = true }
|
|
}
|
|
|
|
// WithRoot sets an alternate root filesystem path.
|
|
func WithRoot(root string) Option {
|
|
return func(o *Options) { o.Root = root }
|
|
}
|
|
|
|
// WithVerbose enables verbose output.
|
|
func WithVerbose() Option {
|
|
return func(o *Options) { o.Verbose = true }
|
|
}
|
|
|
|
// WithRefresh refreshes the package database before the operation.
|
|
// Equivalent to pacman -Sy, apt-get update, apk update, etc.
|
|
func WithRefresh() Option {
|
|
return func(o *Options) { o.Refresh = true }
|
|
}
|
|
|
|
// WithFromRepo constrains the operation to a specific repository.
|
|
// e.g. "unstable" for apt, "community" for pacman.
|
|
func WithFromRepo(repo string) Option {
|
|
return func(o *Options) { o.FromRepo = repo }
|
|
}
|
|
|
|
// WithReinstall forces reinstallation of already-installed packages.
|
|
func WithReinstall() Option {
|
|
return func(o *Options) { o.Reinstall = true }
|
|
}
|
|
|
|
// ApplyOptions processes functional options into an Options struct.
|
|
func ApplyOptions(opts ...Option) Options {
|
|
var o Options
|
|
for _, opt := range opts {
|
|
opt(&o)
|
|
}
|
|
return o
|
|
}
|