summaryrefslogtreecommitdiff
path: root/brewcheck.go
blob: ac3478648a4ba4b5174074d84d8be8ffd6e3566b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package main

import (
	"fmt"
	"os"
	"os/exec"
	"path/filepath"
	"time"

	"github.com/adrg/xdg"
	"github.com/fatih/color"
)

const TTL = 3 * 24 * time.Hour // 3 days

func EnsureBrewAvailable() {
	cache := filepath.Join(xdg.CacheHome, "wthis", "brew-checked")

	// 1. check if cache file exists and is fresh
	if info, err := os.Stat(cache); err == nil {
		if time.Since(info.ModTime()) < TTL {
			return // recently verified that brew exists
		}
	}

	// 2. otherwise, actually check
	if _, err := exec.LookPath("brew"); err != nil {
		// brew not found — no cache write
		color.Red("🍺 Homebrew not installed.")
		fmt.Println("This tool works only with Homebrew formulae and casks.")
		os.Exit(1)
	}

	// 3. write cache
	_ = os.MkdirAll(filepath.Dir(cache), 0o755)
	_ = os.WriteFile(cache, []byte{}, 0o644)
}