summaryrefslogtreecommitdiff
path: root/main.go
blob: 237fdb7503b758ced200dd7c6f5da7747cef593a (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 (
    "fmt"
    "os"
    "context"
    "log"
    "github.com/fatih/color"
    "github.com/urfave/cli/v3"
)

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)
}