From 6bf51e8f9c53190379614e90dcb048b5c932534d Mon Sep 17 00:00:00 2001 From: Nico Grashoff Date: Sun, 22 Jan 2023 23:49:21 +0100 Subject: [PATCH] Revise command line interface --- README.md | 32 +++++++++++++++++++++--------- cmd/twshim/main.go | 49 ++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 64 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 79ce459..0e3e687 100644 --- a/README.md +++ b/README.md @@ -1,27 +1,41 @@ # twshim -twshim is a transparent Go wrapper around the standalone TailwindCSS CLI. -The goal of this project is to unify how developers add TailwindCSS to their Go project by taking care of downloading the executable for the current architecture. +twshim is a transparent Go wrapper around the standalone Tailwind CSS CLI. +The goal of this project is to unify how developers add Tailwind CSS to their Go project by taking care of downloading the executable for the current architecture. + +## Example + +``` +twshim -release v3.2.4 -downloads $HOME/.twshim -- -i in.css -o out.css --minify +``` ## Configuration -Because all parameters are passed to the TailwindCSS executable, twshim itself is configured through environment variables. +twshim can be configured through environment variables or command line arguments: + +``` + -downloads string + Target directory for executables (override TWSHIM_DOWNLOADS) + -release string + Tag of the desired release (overrides TWSHIM_RELEASE) +``` + +A double dash (--) is required before the Tailwind CSS parameters to distinguish twshim configuration from Tailwind arguments. -* `TWTAG` is the tag of the desired TailwindCSS release, e.g. `v3.2.4`. -* `TWROOT` is the directory for downloaded TailwindCSS executables, e.g. `$HOME/.twshim/downloads`. - twshim uses `runtime.GOOS` and `runtime.GOARCH` to decide which executable to download. +See https://github.com/tailwindlabs/tailwindcss/releases for a list of Tailwind releases. + ## Usage -You can use `go run` to invoke twshim if you want to quickly execute a specific version of TailwindCSS CLI. +You can use `go run` to invoke twshim if you want to quickly execute a specific version of Tailwind CSS CLI. ```shell -TWTAG=v3.2.4 TWROOT=$HOME/.twshim/downloads go run github.com/ngrash/twshim/cmd/twshim@v0.2.0 +go run github.com/ngrash/twshim/cmd/twshim@v0.3.0 -release v3.2.4 -downloads $HOME/.twshim/downloads -- --help ``` You can also use `go get` to add twshim to your application and use the `twshim` package from your code. ```shell -go get github.com/ngrash/twshim@v0.2.0 +go get github.com/ngrash/twshim@v0.3.0 ``` \ No newline at end of file diff --git a/cmd/twshim/main.go b/cmd/twshim/main.go index d38ae12..109adca 100644 --- a/cmd/twshim/main.go +++ b/cmd/twshim/main.go @@ -1,11 +1,28 @@ package main import ( + "flag" + "fmt" "github.com/ngrash/twshim" "log" "os" ) +var ( + releaseFlag = flag.String("release", "", "Tag of the desired release (overrides TWSHIM_RELEASE)") + downloadsFlag = flag.String("downloads", "", "Target directory for executables (override TWSHIM_DOWNLOADS)") +) + +func valueOrEnv(value, env string) (string, bool) { + if value != "" { + return value, true + } + if v, ok := os.LookupEnv(env); ok { + return v, true + } + return "", false +} + func main() { l := log.New(os.Stderr, "twshim: ", 0) twshim.Log = l @@ -16,21 +33,37 @@ func main() { l.Fatal(err) } - tag, found := os.LookupEnv("TWTAG") - if !found { - l.Fatal("Set TWTAG to select a release.\n\nSee https://github.com/tailwindlabs/tailwindcss/releases for available releases.") + u := flag.Usage + //goland:noinspection GoUnhandledErrorResult + flag.Usage = func() { + u() + w := flag.CommandLine.Output() + fmt.Fprintln(w, "See https://github.com/tailwindlabs/tailwindcss/releases for a list of releases.") + fmt.Fprintln(w, "Use a double dash (--) to pass parameters to Tailwind CSS CLI.") + fmt.Fprintln(w, "Example:") + fmt.Fprintln(w, " twshim -release v3.2.4 -downloads /tmp/tw -- -i in.css -o out.css --minify") + fmt.Fprintln(w) + } + flag.Parse() + + release, ok := valueOrEnv(*releaseFlag, "TWSHIM_RELEASE") + if !ok { + flag.CommandLine.SetOutput(os.Stdout) // write usage to stdout to highlight error + flag.Usage() + l.Fatal("Insufficient parameters: Please specify release.") } - root, found := os.LookupEnv("TWROOT") - if !found { - l.Fatal("Set TWROOT to configure a download directory.") + downloads, ok := valueOrEnv(*downloadsFlag, "TWSHIM_DOWNLOADS") + if !ok { + flag.CommandLine.SetOutput(os.Stdout) // write usage to stdout to highlight error + flag.Usage() + l.Fatal("Insufficient parameters: Please specify downloads directory.") } - cmd, err := twshim.Command(root, tag, assetName, os.Args[1:]...) + cmd, err := twshim.Command(downloads, release, assetName, flag.Args()...) if err != nil { l.Fatal(err) } - if err := cmd.Run(); err != nil { l.Fatal(err) }