0

Have a makefile in which rule dev-create-empty-migration, currently, this rule has hardcoded argument accounts_table, this argument should not be hardcoded but should be passed as an argument at the time of rule invocation. e. g. make dev-create-empty-migration accounts_table.

Any ideas how to do that?

.PHONY: dev-create-empty-migration
dev-create-empty-migration:
    migrate create -ext sql -dir
./pkg/acc/repo/postgres/migrations accounts_table
Amira Bedhiafi
  • 8,088
  • 6
  • 24
  • 60
Apollo111
  • 1
  • 1

1 Answers1

1

You should use a variable and store something in it. By using ?=, make first searches for an argument. If found, it uses the argument, otherwise the default content.

$ cat Makefile 
VAR     ?= derp
test:
        @echo $(VAR)
$ make
derp
$ make VAR=lala
lala

Just because it's fun, you can also do something as follows. This omits the use of additional arguments, but you could of course make some hybrid.

$ cat Makefile 
VAR     := None

dev-create-empty-migration-%_table:
        $(eval VAR=$(patsubst dev-create-empty-migration-%,%,$@))
        @echo $(VAR)
$ make dev-create-empty-migration-derp_table
derp_table                 
Bayou
  • 3,293
  • 1
  • 9
  • 22