Skip to content

Commit 6bf51e8

Browse files
committed
Revise command line interface
1 parent 783bc9a commit 6bf51e8

File tree

2 files changed

+64
-17
lines changed

2 files changed

+64
-17
lines changed

README.md

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,41 @@
11
# twshim
22

3-
twshim is a transparent Go wrapper around the standalone TailwindCSS CLI.
4-
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.
3+
twshim is a transparent Go wrapper around the standalone Tailwind CSS CLI.
4+
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.
5+
6+
## Example
7+
8+
```
9+
twshim -release v3.2.4 -downloads $HOME/.twshim -- -i in.css -o out.css --minify
10+
```
511

612
## Configuration
713

8-
Because all parameters are passed to the TailwindCSS executable, twshim itself is configured through environment variables.
14+
twshim can be configured through environment variables or command line arguments:
15+
16+
```
17+
-downloads string
18+
Target directory for executables (override TWSHIM_DOWNLOADS)
19+
-release string
20+
Tag of the desired release (overrides TWSHIM_RELEASE)
21+
```
22+
23+
A double dash (--) is required before the Tailwind CSS parameters to distinguish twshim configuration from Tailwind arguments.
924

10-
* `TWTAG` is the tag of the desired TailwindCSS release, e.g. `v3.2.4`.
11-
* `TWROOT` is the directory for downloaded TailwindCSS executables, e.g. `$HOME/.twshim/downloads`.
12-
1325
twshim uses `runtime.GOOS` and `runtime.GOARCH` to decide which executable to download.
1426

27+
See https://github.com/tailwindlabs/tailwindcss/releases for a list of Tailwind releases.
28+
1529
## Usage
1630

17-
You can use `go run` to invoke twshim if you want to quickly execute a specific version of TailwindCSS CLI.
31+
You can use `go run` to invoke twshim if you want to quickly execute a specific version of Tailwind CSS CLI.
1832

1933
```shell
20-
TWTAG=v3.2.4 TWROOT=$HOME/.twshim/downloads go run github.com/ngrash/twshim/cmd/twshim@v0.2.0
34+
go run github.com/ngrash/twshim/cmd/twshim@v0.3.0 -release v3.2.4 -downloads $HOME/.twshim/downloads -- --help
2135
```
2236

2337
You can also use `go get` to add twshim to your application and use the `twshim` package from your code.
2438

2539
```shell
26-
go get github.com/ngrash/twshim@v0.2.0
40+
go get github.com/ngrash/twshim@v0.3.0
2741
```

cmd/twshim/main.go

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,28 @@
11
package main
22

33
import (
4+
"flag"
5+
"fmt"
46
"github.com/ngrash/twshim"
57
"log"
68
"os"
79
)
810

11+
var (
12+
releaseFlag = flag.String("release", "", "Tag of the desired release (overrides TWSHIM_RELEASE)")
13+
downloadsFlag = flag.String("downloads", "", "Target directory for executables (override TWSHIM_DOWNLOADS)")
14+
)
15+
16+
func valueOrEnv(value, env string) (string, bool) {
17+
if value != "" {
18+
return value, true
19+
}
20+
if v, ok := os.LookupEnv(env); ok {
21+
return v, true
22+
}
23+
return "", false
24+
}
25+
926
func main() {
1027
l := log.New(os.Stderr, "twshim: ", 0)
1128
twshim.Log = l
@@ -16,21 +33,37 @@ func main() {
1633
l.Fatal(err)
1734
}
1835

19-
tag, found := os.LookupEnv("TWTAG")
20-
if !found {
21-
l.Fatal("Set TWTAG to select a release.\n\nSee https://github.com/tailwindlabs/tailwindcss/releases for available releases.")
36+
u := flag.Usage
37+
//goland:noinspection GoUnhandledErrorResult
38+
flag.Usage = func() {
39+
u()
40+
w := flag.CommandLine.Output()
41+
fmt.Fprintln(w, "See https://github.com/tailwindlabs/tailwindcss/releases for a list of releases.")
42+
fmt.Fprintln(w, "Use a double dash (--) to pass parameters to Tailwind CSS CLI.")
43+
fmt.Fprintln(w, "Example:")
44+
fmt.Fprintln(w, " twshim -release v3.2.4 -downloads /tmp/tw -- -i in.css -o out.css --minify")
45+
fmt.Fprintln(w)
46+
}
47+
flag.Parse()
48+
49+
release, ok := valueOrEnv(*releaseFlag, "TWSHIM_RELEASE")
50+
if !ok {
51+
flag.CommandLine.SetOutput(os.Stdout) // write usage to stdout to highlight error
52+
flag.Usage()
53+
l.Fatal("Insufficient parameters: Please specify release.")
2254
}
2355

24-
root, found := os.LookupEnv("TWROOT")
25-
if !found {
26-
l.Fatal("Set TWROOT to configure a download directory.")
56+
downloads, ok := valueOrEnv(*downloadsFlag, "TWSHIM_DOWNLOADS")
57+
if !ok {
58+
flag.CommandLine.SetOutput(os.Stdout) // write usage to stdout to highlight error
59+
flag.Usage()
60+
l.Fatal("Insufficient parameters: Please specify downloads directory.")
2761
}
2862

29-
cmd, err := twshim.Command(root, tag, assetName, os.Args[1:]...)
63+
cmd, err := twshim.Command(downloads, release, assetName, flag.Args()...)
3064
if err != nil {
3165
l.Fatal(err)
3266
}
33-
3467
if err := cmd.Run(); err != nil {
3568
l.Fatal(err)
3669
}

0 commit comments

Comments
 (0)