// Package provider wraps the package managers and installer formats deployr can // drive. Every provider answers the same five questions — am I present, can I // install, update, uninstall, and what version is here — so the engine can walk // a catalog entry's sources without caring which route wins. package provider import ( "context" "sort" "strings" "time" "git.starwarslab.us/ATOM_IT/Deployr/internal/catalog" "git.starwarslab.us/ATOM_IT/Deployr/internal/logx" ) // Opts is the ambient configuration for a provider operation. type Opts struct { DryRun bool Timeout time.Duration Log *logx.Logger CacheDir string Arch string RequireChecksum bool // Detect carries the app's presence rules, which the direct provider needs // to locate an uninstall entry it did not create. Detect catalog.Detect // KeepDownloads leaves fetched installers in the cache directory. KeepDownloads bool } // Outcome describes what a provider actually did. type Outcome struct { // Version installed, when the provider can report it. Version string // RebootRequired is set when the installer returned 3010/1641. RebootRequired bool // NoOp is set when the request was already satisfied. NoOp bool // Message is a short human explanation, e.g. "already up to date". Message string } // Provider is one install route. type Provider interface { // Name is the identifier used in catalog sources. Name() string // OSes lists the GOOS values this provider can operate on. OSes() []string // Label is a human description for `deployr doctor`. Label() string // Available reports whether this route is usable on this machine right now. Available(ctx context.Context, o Opts) bool // Install makes the package present. Install(ctx context.Context, src catalog.Source, o Opts) (Outcome, error) // Update brings an installed package to the latest version. Update(ctx context.Context, src catalog.Source, o Opts) (Outcome, error) // Uninstall removes the package. Uninstall(ctx context.Context, src catalog.Source, o Opts) (Outcome, error) // Query returns the version this provider believes is installed. Query(ctx context.Context, src catalog.Source, o Opts) (string, bool) } var registry = map[string]Provider{} func register(p Provider) { registry[p.Name()] = p } func init() { register(&winget{}) register(&choco{}) register(&brew{cask: false}) register(&brew{cask: true}) register(newApt()) register(&rpmMgr{name: "dnf", bins: []string{"dnf5", "dnf", "yum"}}) register(&rpmMgr{name: "zypper", bins: []string{"zypper"}}) register(&pacman{}) register(&flatpak{}) register(&snap{}) register(&direct{}) register(&printerDriver{}) } // Get returns the provider registered under name. func Get(name string) (Provider, bool) { p, ok := registry[strings.ToLower(strings.TrimSpace(name))] return p, ok } // All returns every provider, sorted by name. func All() []Provider { out := make([]Provider, 0, len(registry)) for _, p := range registry { out = append(out, p) } sort.Slice(out, func(i, j int) bool { return out[i].Name() < out[j].Name() }) return out } // ForOS returns the providers that can run on a GOOS, sorted by name. func ForOS(goos string) []Provider { var out []Provider for _, p := range All() { for _, os := range p.OSes() { if os == goos { out = append(out, p) break } } } return out } // Names lists every registered provider name. func Names() []string { out := make([]string, 0, len(registry)) for name := range registry { out = append(out, name) } sort.Strings(out) return out } // Windows installers signal "done, but reboot" rather than failing. const ( exitRebootInitiated = 1641 exitRebootRequired = 3010 ) func isRebootExit(code int) bool { return code == exitRebootInitiated || code == exitRebootRequired }