mage icon indicating copy to clipboard operation
mage copied to clipboard

Enhancement: flag for project level variables

Open thorntonrose opened this issue 11 months ago • 2 comments

Describe the feature:

I suggest adding a flag for specifying project level variables.

Example CLI usage:

mage -D pkg=./foo -D tests=TestBar test

Example Magefile usage:

func Test() {
   sh.cmd("bash", "-c", fmt.Sprintf("go test -run %s %s", tests, pkg))
}

Many other build tools do this or some variant.

What problem does this feature address?

This feature addresses the problem that environment variables have to be used for user specified, project level variables.

How does this benefit users of Mage?

Mage user can specify project level variables in a way that is not shell specific, for example mage test -D tests=TestFoo instead of TESTS=TestFoo mage test.

Additional context:

None.

thorntonrose avatar Mar 02 '25 17:03 thorntonrose

I would like to work on this feature.

thorntonrose avatar Mar 02 '25 17:03 thorntonrose

This would be a nice way to express overrides for any underlying configuration within a mage command. It would also be a nice way to keep the interface of a command the same, but express some other execution context.

For example, let's say I have an imaginary mage namespace for sql that has a command called syncusers that allows me to sync some external data with a user in some relational database. To keep all of the tooling consistent, let's say I also want to leverage syncusers for manual testing during development and scaffolding during test automation. To achieve this, I may want to have a setup where syncusers executes locally by default, but I may provide a -remote option for doing the real deal in CI/CD.

So locally I may just run

mage sql:syncusers dev

But in CI/CD I may run

mage sql:syncusers prod -remote

To add to @thorntonrose's feature and benefits above, it would be nice if the interface for a mage command could match that of the interface for running a normal go binary. It would make it easier for people to port their existing binaries over to mage commands.

TwFlem avatar Apr 11 '25 05:04 TwFlem