package provider import ( "context" "fmt" "os" "path/filepath" "runtime" "strings" "sync" "git.starwarslab.us/ATOM_IT/Deployr/internal/catalog" "git.starwarslab.us/ATOM_IT/Deployr/internal/run" ) // ---------------------------------------------------------------- winget --- type winget struct { once sync.Once path string } func (w *winget) Name() string { return "winget" } func (w *winget) OSes() []string { return []string{"windows"} } func (w *winget) Label() string { return "Windows Package Manager (winget)" } // resolve finds winget.exe. The PATH entry is an app-execution alias that only // exists inside a user session, so a run as SYSTEM (which is how an RMM invokes // us) has to reach into the WindowsApps payload directly. func (w *winget) resolve() string { w.once.Do(func() { if runtime.GOOS != "windows" { return } if p, ok := run.Which("winget"); ok { w.path = p return } pf := os.Getenv("ProgramFiles") if pf == "" { pf = "C:\\Program Files" } patterns := []string{ filepath.Join(pf, "WindowsApps", "Microsoft.DesktopAppInstaller_*_x64__8wekyb3d8bbwe", "winget.exe"), filepath.Join(pf, "WindowsApps", "Microsoft.DesktopAppInstaller_*_arm64__8wekyb3d8bbwe", "winget.exe"), filepath.Join(pf, "WindowsApps", "Microsoft.DesktopAppInstaller_*", "winget.exe"), } var newest string var newestTime int64 for _, pattern := range patterns { matches, _ := filepath.Glob(pattern) for _, m := range matches { info, err := os.Stat(m) if err != nil { continue } if ts := info.ModTime().Unix(); ts > newestTime { newest, newestTime = m, ts } } } w.path = newest }) return w.path } func (w *winget) Available(context.Context, Opts) bool { return w.resolve() != "" } func (w *winget) base() []string { return []string{ "--exact", "--silent", "--accept-package-agreements", "--accept-source-agreements", "--disable-interactivity", } } func (w *winget) exec(ctx context.Context, o Opts, args []string) run.Result { return run.Exec(ctx, run.Options{ Name: w.resolve(), Args: args, Timeout: o.Timeout, DryRun: o.DryRun, Log: o.Log, }) } func (w *winget) Install(ctx context.Context, src catalog.Source, o Opts) (Outcome, error) { args := append([]string{"install", "--id", src.ID}, w.base()...) args = append(args, src.Args...) // Prefer a machine-wide install so the software is there for every user -- // and because a user-scope install performed as SYSTEM lands in a profile // nobody logs into. Not every package ships a machine-scope installer, so // fall back rather than fail. scoped := append(append([]string{}, args...), "--scope", "machine") res := w.exec(ctx, o, scoped) if res.Failed() && !w.benign(res) && wantsOtherScope(res) { if o.Log != nil { o.Log.Debugf("winget: %s has no machine-scope installer, retrying in default scope", src.ID) } res = w.exec(ctx, o, args) } return w.outcome(res, src, o, "install") } func (w *winget) Update(ctx context.Context, src catalog.Source, o Opts) (Outcome, error) { args := append([]string{"upgrade", "--id", src.ID}, w.base()...) args = append(args, src.Args...) res := w.exec(ctx, o, args) return w.outcome(res, src, o, "upgrade") } func (w *winget) Uninstall(ctx context.Context, src catalog.Source, o Opts) (Outcome, error) { args := []string{"uninstall", "--id", src.ID, "--exact", "--silent", "--accept-source-agreements", "--disable-interactivity"} res := w.exec(ctx, o, args) return w.outcome(res, src, o, "uninstall") } func (w *winget) Query(ctx context.Context, src catalog.Source, o Opts) (string, bool) { res := run.Exec(ctx, run.Options{ Name: w.resolve(), Args: []string{"list", "--id", src.ID, "--exact", "--accept-source-agreements", "--disable-interactivity"}, Timeout: o.Timeout, Log: o.Log, }) if res.Failed() { return "", false } return wingetListVersion(res.Stdout, src.ID), true } // wingetListVersion pulls the version column out of `winget list` output. The // table is space-aligned and localised, so we anchor on the package id and take // the token after it rather than trusting column positions. func wingetListVersion(out, id string) string { for _, line := range strings.Split(out, "\n") { if !strings.Contains(line, id) { continue } fields := strings.Fields(line) for i, f := range fields { if strings.EqualFold(f, id) && i+1 < len(fields) { return fields[i+1] } } } return "" } // benign recognises the outcomes winget reports as errors but which mean the // machine is already in the state we asked for. func (w *winget) benign(res run.Result) bool { for _, phrase := range []string{ "already installed", "no applicable upgrade", "no newer package versions", "no available upgrade", "no installed package found matching", } { if res.Contains(phrase) { return true } } return false } func wantsOtherScope(res run.Result) bool { return res.Contains("scope") || res.Contains("no applicable installer") } func (w *winget) outcome(res run.Result, src catalog.Source, o Opts, verb string) (Outcome, error) { if o.DryRun { return Outcome{Message: "dry run"}, nil } switch { case !res.Failed(): return Outcome{}, nil case isRebootExit(res.ExitCode): return Outcome{RebootRequired: true}, nil case w.benign(res): return Outcome{NoOp: true, Message: "already up to date"}, nil default: return Outcome{}, fmt.Errorf("winget %s %s: %s", verb, src.ID, res.Error()) } } // ------------------------------------------------------------ chocolatey --- type choco struct{} func (c *choco) Name() string { return "choco" } func (c *choco) OSes() []string { return []string{"windows"} } func (c *choco) Label() string { return "Chocolatey" } func (c *choco) Available(context.Context, Opts) bool { return runtime.GOOS == "windows" && run.Available("choco") } func (c *choco) exec(ctx context.Context, o Opts, args ...string) run.Result { return run.Exec(ctx, run.Options{ Name: "choco", Args: append(args, "-y", "--no-progress", "--limit-output"), Timeout: o.Timeout, DryRun: o.DryRun, Log: o.Log, }) } func (c *choco) Install(ctx context.Context, src catalog.Source, o Opts) (Outcome, error) { res := c.exec(ctx, o, append([]string{"install", src.ID}, src.Args...)...) return c.outcome(res, src, o, "install") } func (c *choco) Update(ctx context.Context, src catalog.Source, o Opts) (Outcome, error) { res := c.exec(ctx, o, append([]string{"upgrade", src.ID}, src.Args...)...) return c.outcome(res, src, o, "upgrade") } func (c *choco) Uninstall(ctx context.Context, src catalog.Source, o Opts) (Outcome, error) { res := c.exec(ctx, o, "uninstall", src.ID, "--remove-dependencies") return c.outcome(res, src, o, "uninstall") } func (c *choco) Query(ctx context.Context, src catalog.Source, o Opts) (string, bool) { // Chocolatey 2.x dropped --local-only; 1.x needs it or `list` queries the // remote feed instead of the machine. for _, args := range [][]string{ {"list", "--local-only", "--exact", src.ID, "-r"}, {"list", "--exact", src.ID, "-r"}, } { res := run.Exec(ctx, run.Options{Name: "choco", Args: args, Timeout: o.Timeout, Log: o.Log}) if res.Failed() { continue } for _, line := range strings.Split(res.Stdout, "\n") { parts := strings.Split(strings.TrimSpace(line), "|") if len(parts) == 2 && strings.EqualFold(parts[0], src.ID) { return parts[1], true } } } return "", false } func (c *choco) outcome(res run.Result, src catalog.Source, o Opts, verb string) (Outcome, error) { if o.DryRun { return Outcome{Message: "dry run"}, nil } switch { case !res.Failed(): if res.Contains("already installed") || res.Contains("is not installed") { return Outcome{NoOp: true, Message: "already up to date"}, nil } return Outcome{RebootRequired: res.Contains("pending reboot")}, nil case isRebootExit(res.ExitCode): return Outcome{RebootRequired: true}, nil default: return Outcome{}, fmt.Errorf("choco %s %s: %s", verb, src.ID, res.Error()) } }