Calling mix task inside a function

Is there a way we can call a mix task inside a function?
I couldn’t find it in documentation just found the Command Line examples.
Thanks

A mix task is just a module + functions. So you can always run it like any other module + function.

Mix.Tasks.MyTask.run()

You can, but you shouldn’t.

Mix.Task.run/2, can be used for that, but mix is usually not integrated in releases unless you do so manually.

But usually you do not want to do so, for reasons discussed a couple of times in the forum.

1 Like

You are better off having a shared module and function that your mix task uses. Then you can use the same module and function in your other code instead of trying to run the mix task – this will fail because releases (deployments) don’t include mix.

3 Likes