Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to define functions that aren't exposed as tasks #3

Open
nickjj opened this issue Feb 20, 2021 · 3 comments
Open

Ability to define functions that aren't exposed as tasks #3

nickjj opened this issue Feb 20, 2021 · 3 comments

Comments

@nickjj
Copy link

nickjj commented Feb 20, 2021

Hey, great idea btw. Really enjoying this pattern.

But I ran into a situation where I would very much like to add a function into my Taskfile but not have it be runnable directly, or perhaps as a compromise not be listed in the help menu.

This comes in handy when you want to define a "helper" function that multiple tasks can call.

For example, currently I have some tasks that look like this:

function flask {
  ## Run any Flask commands
  docker-compose exec web flask "${@}"
}

function flake8 {
  ## Lint Python code with flake8
  docker-compose exec web flake8 "${@}"
}

It would be really nice if I could do this instead:

function _web {
  docker-compose exec web "${@}"
}

function flask {
  ## Run any Flask commands
  _web flask "${@}"
}

function flake8 {
  ## Lint Python code with flake8
  _web flake8 "${@}"
}

And then the help menu would only show the flask and flake8 commands and running _web directly would throw a command not found.

I figured we could mark these helper functions with an underscore to distinguish what should be private or not.

Method I've tried so far that partially works

You can do compgen -A function | grep -v "^_" | cat -n which hides them from the task list but technically you can still run the helper functions directly.

Any thoughts on how to do this in a better way?

@GustavoKatel
Copy link

not sure if this is the best way, but you check if the helper task if any arg was passed (i.e.: $#)

@brenwell
Copy link

brenwell commented Feb 21, 2021

I am doing this, maybe it helps

#!/bin/bash

function sayhellomars() {
  echo "hello mars"
}

function public:hellomars() {
  sayhellomars
}

function public:helloworld() {
  echo "hello world"
}


# DEFAULT public: list all possible commands
function public:help {
    echo "$0 <task> <args>"
    echo "Tasks:"
    compgen -A function | sed -En 's/public:(.*)/\1/p' | cat -n
}

TIMEFORMAT="Task completed in %3lR"
time "public:${@:-help}" # make help default

./taskfile help

~/bin(master*) » ./taskfile help
./taskfile <task> <args>
Tasks:
     1	hellomars
     2	helloworld
     3	help

./taskfile sayhellomars

~/bin(master*) » ./taskfile sayhellomars
./taskfile: line 27: public:sayhellomars: command not found

@nickjj
Copy link
Author

nickjj commented Feb 21, 2021

@brenwell Ah I see, the time "public:${@:-help}" line is allowing non-public functions to be not found. I wonder how we can switch the logic so that all functions except functions prefixed with private: or _ aren't. This way you don't need to prefix every public function with public:.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants