package provider import ( "context" "fmt" "os" "runtime" "strings" "sync" "git.starwarslab.us/ATOM_IT/Deployr/internal/catalog" "git.starwarslab.us/ATOM_IT/Deployr/internal/platform" "git.starwarslab.us/ATOM_IT/Deployr/internal/run" ) // brew drives Homebrew, as either a formula ("brew") or a cask ("cask") // provider. The two share every code path except the --cask flag. type brew struct { cask bool once sync.Once path string user string // account to run brew as; empty means "the current user" ok bool } func (b *brew) Name() string { if b.cask { return "cask" } return "brew" } func (b *brew) OSes() []string { if b.cask { return []string{"darwin"} } return []string{"darwin", "linux"} } func (b *brew) Label() string { if b.cask { return "Homebrew Cask" } return "Homebrew" } // brewPaths are the standard prefixes: Apple silicon, Intel macOS, and // Linuxbrew. PATH is checked first for a non-standard install. var brewPaths = []string{ "/opt/homebrew/bin/brew", "/usr/local/bin/brew", "/home/linuxbrew/.linuxbrew/bin/brew", } // resolve locates brew and decides which account should run it. Homebrew // refuses to run as root, so a root context (RMM, MDM, sudo) has to drop to the // account that owns the Homebrew prefix. When there is no such account, this // route is simply unavailable and the engine falls through to the next source. func (b *brew) resolve() (string, string, bool) { b.once.Do(func() { if runtime.GOOS == "windows" { return } if p, found := run.Which("brew"); found { b.path = p } else { for _, p := range brewPaths { if _, err := os.Stat(p); err == nil { b.path = p break } } } if b.path == "" { return } if os.Geteuid() != 0 { b.ok = true return } // Running as root: the owner of the brew binary is the account that // installed it and the one whose permissions the cellar expects. if owner, found := fileOwner(b.path); found && owner != "root" { b.user, b.ok = owner, true return } if u := platform.ConsoleUser(); u != "" { b.user, b.ok = u, true return } }) return b.path, b.user, b.ok } func (b *brew) Available(context.Context, Opts) bool { _, _, ok := b.resolve() return ok } func (b *brew) exec(ctx context.Context, o Opts, args ...string) run.Result { path, user, _ := b.resolve() return run.Exec(ctx, run.Options{ Name: path, Args: args, AsUser: user, Env: []string{ "NONINTERACTIVE=1", "HOMEBREW_NO_ENV_HINTS=1", "HOMEBREW_NO_ANALYTICS=1", }, Timeout: o.Timeout, DryRun: o.DryRun, Log: o.Log, }) } // flag returns --cask for the cask provider, or nothing. func (b *brew) flag() []string { if b.cask { return []string{"--cask"} } return nil } func (b *brew) Install(ctx context.Context, src catalog.Source, o Opts) (Outcome, error) { args := append([]string{"install"}, b.flag()...) args = append(args, src.ID) args = append(args, src.Args...) res := b.exec(ctx, o, args...) return b.outcome(res, src, o, "install") } func (b *brew) Update(ctx context.Context, src catalog.Source, o Opts) (Outcome, error) { args := append([]string{"upgrade"}, b.flag()...) if b.cask { // Casks whose app self-updates are skipped by default; --greedy is the // only way a patching tool actually patches them. args = append(args, "--greedy") } args = append(args, src.ID) res := b.exec(ctx, o, args...) return b.outcome(res, src, o, "upgrade") } func (b *brew) Uninstall(ctx context.Context, src catalog.Source, o Opts) (Outcome, error) { args := append([]string{"uninstall"}, b.flag()...) args = append(args, src.ID) res := b.exec(ctx, o, args...) return b.outcome(res, src, o, "uninstall") } func (b *brew) Query(ctx context.Context, src catalog.Source, o Opts) (string, bool) { args := append([]string{"list", "--versions"}, b.flag()...) args = append(args, src.ID) res := b.exec(ctx, o, args...) if res.Failed() { return "", false } fields := strings.Fields(res.Stdout) if len(fields) >= 2 { return fields[len(fields)-1], true } if len(fields) == 1 { return "", true } return "", false } func (b *brew) benign(res run.Result) bool { for _, phrase := range []string{ "already installed", "it seems there is already an app at", "no available upgrade", "already up-to-date", } { if res.Contains(phrase) { return true } } return false } func (b *brew) 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 b.benign(res) { return Outcome{NoOp: true, Message: "already up to date"}, nil } return Outcome{}, nil case b.benign(res): return Outcome{NoOp: true, Message: "already up to date"}, nil default: return Outcome{}, fmt.Errorf("brew %s %s: %s", verb, src.ID, res.Error()) } }