Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ Carafe can check whether a formula is installed and whether it meets a minimum v
/opt/macadmins/bin/carafe check <formula> [--min-version=<version>] [--skip-not-installed]
```

When several versions of a formula are installed, `check` uses the version in use: the linked keg, or for a keg-only formula the newest installed stable version. Older kegs left on disk are not checked; `brew cleanup` removes them.

### Caching

When running many `check` commands in quick succession (e.g. from multiple Munki `installcheck_script` entries), Carafe caches the output of `brew info --json --installed` on disk for 60 seconds by default. This means only the first `check` call invokes Homebrew; all subsequent calls within the TTL window are served from the cache, significantly reducing the time for a full Munki check run.
Expand Down
17 changes: 17 additions & 0 deletions brew/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,3 +186,20 @@ func TestCachePath(t *testing.T) {
assert.Equal(t, cacheFileArm64, cachePath("/opt/homebrew/bin/brew"))
assert.Equal(t, cacheFileX86_64, cachePath("/usr/local/bin/brew"))
}

// TestInfoOutputCached_KeepsLinkedKeg pins that the cached path keeps
// linked_keg when it re-encodes a formula, so getVersion sees the version in
// use and not the oldest keg.
func TestInfoOutputCached_KeepsLinkedKeg(t *testing.T) {
cacheFile := filepath.Join(t.TempDir(), "cache.json")
data := `[{"name":"git","installed":[{"version":"2.45.0"},{"version":"2.52.0"}],"linked_keg":"2.52.0"}]`
require.NoError(t, os.WriteFile(cacheFile, []byte(data), 0600))

c := newTestConfig() // no outputs configured: brew must not be called
out, err := infoOutputCached(c, "git", cacheFile, 60*time.Second)
require.NoError(t, err)

v, err := getVersion(out)
require.NoError(t, err)
assert.Equal(t, "2.52.0", v)
}
30 changes: 29 additions & 1 deletion brew/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"regexp"
"strings"

"github.com/hashicorp/go-version"

Expand All @@ -21,6 +22,7 @@ func stripBrewRevision(v string) string {
type HomebrewFormula struct {
Name string `json:"name"`
Installed []Installed `json:"installed"`
LinkedKeg string `json:"linked_keg,omitempty"`
}

type Installed struct {
Expand Down Expand Up @@ -81,7 +83,33 @@ func getVersion(output string) (string, error) {
if len(info) == 0 || len(info[0].Installed) == 0 {
return "", nil
}
return info[0].Installed[0].Version, nil
// The linked keg is the version in use. Use it when it is a stable keg that
// brew lists as installed. Otherwise (a keg-only formula, a linked HEAD keg,
// or a link to a keg that is gone) use the newest stable keg: brew sorts
// "installed" from oldest to newest, and an old keg stays there until
// "brew cleanup" removes it. A HEAD version has no number to compare, so a
// HEAD keg is used only when no stable keg exists.
kegs := info[0].Installed
linked := info[0].LinkedKeg
if linked != "" && !isHeadVersion(linked) {
for _, k := range kegs {
if k.Version == linked {
return linked, nil
}
}
}
for i := len(kegs) - 1; i >= 0; i-- {
if !isHeadVersion(kegs[i].Version) {
return kegs[i].Version, nil
}
}
return kegs[len(kegs)-1].Version, nil
}

// isHeadVersion reports whether a keg version is a HEAD build, such as
// "HEAD-1234567".
func isHeadVersion(v string) bool {
return strings.HasPrefix(v, "HEAD")
}

func InstalledVersion(c exec.CarafeConfig, item string) (string, error) {
Expand Down
37 changes: 37 additions & 0 deletions brew/info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,43 @@ func TestGetVersion(t *testing.T) {
expected: "1.2.3",
expectError: false,
},
{
name: "old keg left on disk, linked keg is newer",
output: `[{ "installed": [{ "version": "2.45.0" }, { "version": "2.52.0" }], "linked_keg": "2.52.0" }]`,
expected: "2.52.0",
expectError: false,
},
{
name: "keg-only formula with two kegs uses the newest",
output: `[{ "installed": [{ "version": "3.4.0" }, { "version": "3.6.0" }], "linked_keg": null }]`,
expected: "3.6.0",
expectError: false,
},
{
name: "keg-only formula with stable and HEAD kegs uses the stable one",
output: `[{ "installed": [{ "version": "3.6.0" }, { "version": "HEAD-1234567" }], "linked_keg": null }]`,
expected: "3.6.0",
expectError: false,
},
{
// A linked HEAD keg cannot be compared; use the newest stable keg.
name: "linked HEAD keg next to a stable keg uses the stable one",
output: `[{ "installed": [{ "version": "1.0" }, { "version": "HEAD-1234567" }], "linked_keg": "HEAD-1234567" }]`,
expected: "1.0",
expectError: false,
},
{
name: "linked keg missing from installed falls back to the newest keg",
output: `[{ "installed": [{ "version": "1.0" }], "linked_keg": "2.0" }]`,
expected: "1.0",
expectError: false,
},
{
name: "only a HEAD keg",
output: `[{ "installed": [{ "version": "HEAD-1234567" }], "linked_keg": null }]`,
expected: "HEAD-1234567",
expectError: false,
},
{
name: "valid JSON without installed version",
output: `[{ "installed": [] }]`,
Expand Down