Ensure required cmd in Makefile targets


To conditionally install the swag command required for my openapi-generate target, I find this approach to be both easy and portable. It works well because swag is a Go binary, so I have a way to install it regardless of the user's platform, using go install:

GOPATH ?= $(HOME)/go/bin
SWAG = $(GOPATH)/swag
openapi-generate:
    test -x $(SWAG) || go install github.com/swaggo/swag/cmd/swag
    $(SWAG) init
    $(SWAG) fmt

It's important to note, the variables must be set outside the target, openapi-generate.


Licensed under CC BY Creative Commons License ~ ✉ torstein.k.johansen @ gmail ~ 🐘 @skybert@hachyderm.io ~ 🎥 youtube.com/@skybert