diff options
Diffstat (limited to 'main.go')
| -rw-r--r-- | main.go | 45 |
1 files changed, 25 insertions, 20 deletions
@@ -4,7 +4,10 @@ import ( "fmt" "os" "os/exec" + "context" + "log" "github.com/fatih/color" + "github.com/urfave/cli/v3" ) func main() { @@ -12,28 +15,30 @@ func main() { _, brewInstallationErr := exec.LookPath("brew") if brewInstallationErr != nil { color.Red("🍺 Homebrew not installed.") - fmt.Println("This tool is for looking up formulae/cask information about packages installed with Homebrew. If you don't have Homebrew, it's no use.") - os.Exit(1) + fmt.Println("This tool is for looking up formulae/cask information about packages installed with Homebrew.") + fmt.Println("If you don't have Homebrew, it's no use.") + os.Exit(0) } - args := os.Args - // handle arguments - var printUsage = func() { - color.New(color.FgBlue).Print("Usage: ") - fmt.Print("wthis <package>\n") - } - if len(args) < 2 { - printUsage() - os.Exit(1) - } - if args[1] == "-h" || args[1] == "--help" { - printUsage() - os.Exit(0) + // 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 { + if cmd.Args().Len() == 0 { + color.Red("❌ Please provide a formulae/cask name.") + fmt.Println("Usage: wthis <name>") + return nil + } + pkgName := cmd.Args().First() + formulae, cask := GetInfo(pkgName) + PrintInfo(formulae, cask, pkgName) + return nil + }, } - pkg := args[1] - // get formulae/cask info - formulae, cask := GetInfo(pkg) - // print info - PrintInfo(formulae, cask, pkg) + // run command and handle error + if err := cmd.Run(context.Background(), os.Args); err != nil { + log.Fatal(err) + } } |
