summaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
authorEltrac <hi@guhub.cn>2025-11-11 11:04:23 +0800
committerEltrac <hi@guhub.cn>2025-11-11 11:04:23 +0800
commit06dcdf457d25a707f6a25237f09a6e186173bbb7 (patch)
treedf613d6d5cbbcccb20b4f6cfe64134c3498b3e7d /main.go
parentff99f193fee8bff9e5fdce39f05d1d95622c360a (diff)
refactor: use urfave/cli
Diffstat (limited to 'main.go')
-rw-r--r--main.go45
1 files changed, 25 insertions, 20 deletions
diff --git a/main.go b/main.go
index d986749..ca60c40 100644
--- a/main.go
+++ b/main.go
@@ -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)
+ }
}