summaryrefslogtreecommitdiff
path: root/brewcheck.go
blob: 02546b13deb4f90b26777ac2f338ecdac8065f61 (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
package main

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

	"github.com/fatih/color"
)

const TTL = 3 * 24 * time.Hour // 3 days
const cacheName = "brew-checked"

func EnsureBrewAvailable() {
	// 1. check if cache file exists and is fresh
	data, _ := ReadCache(cacheName, TTL)
	t, _ := time.Parse(time.RFC3339, string(data))
	if time.Since(t) < TTL {
		return
	}

	// 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
	WriteCache(cacheName, []byte(time.Now().Format(time.RFC3339)))
}