summaryrefslogtreecommitdiff
path: root/main.go
blob: d7ada827025cfff7f6455c396209058846a7254c (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
38
39
40
41
42
43
44
45
46
package main

import (
	"context"
	"fmt"
	"github.com/fatih/color"
	"github.com/urfave/cli/v3"
	"log"
	"os"
)

func main() {
	EnsureBrewAvailable()

	// declare CLI app
	cmd := &cli.Command{
		Name:  "wthis",
		Usage: "Fetch information of a Homebrew formulae or cask, like how it got installed, reverse dependencies, etc.",
		Action: func(ctx context.Context, cmd *cli.Command) error {
			// check args
			if cmd.Args().Len() == 0 {
				printErrorAndExit("Please provide a formulae/cask name.")
			}
			pkgName := cmd.Args().First()
			// prompt
			showLoadingPrompt(fmt.Sprintf("What the heck is \"%s\"", pkgName))
			// search, tidy up, and print
			formula, cask := GetBrewInfo(pkgName)
			stat := NewStatistics(formula, cask, pkgName)
			hideLoadingPrompt()
			stat.Print()
			return nil
		},
	}

	// run command and handle error
	if err := cmd.Run(context.Background(), os.Args); err != nil {
		log.Fatal(err)
	}
}

func printErrorAndExit(msg string) {
	hideLoadingPrompt()
	color.Red("❌ %s", msg)
	os.Exit(1)
}