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

Taskrc discovery: check ${XDG_CONFIG_DIR}/task/taskrc; raise execption if not found #163

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from

Conversation

adam-gaia
Copy link

@adam-gaia adam-gaia commented Jan 9, 2023

I have my taskrc in ${XDG_CONFIG_HOME}/task/taskrc, a location that taskwarrior checks by default. Since this is a default location, I would like to add support to taskw to check without needing to set the TASKRC env var.

I've added a function, find_taskrc(), that checks these locations, in this order:

  1. ${TASKRC} env var
  2. ${HOME}/.taskrc
  3. ${XDG_CONFIG_HOME}/task/taskrc

Additionally, this function raises FileNotFoundError when either

  • the found taskrc path is not a file
  • no taskrc path could be found

I'm happy to make changes to get the basic idea of searching ${XDG_CONFIG_HOME}/task/taskrc in.


raise FileNotFoundError("Unable to find taskrc. Set environment variable 'TASKRC=<file>' for a non-standard location")


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason not to assign TASKRC = find_taskrc() here? Then you wouldn't have to change anything else in this module and we could avoid the extra conditionals in this module.

taskw/warrior.py Outdated
* Specified taskrc is not a file
* No taskrc was found
"""
if "TASKRC" in os.environ.keys():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, python automatically applies the in operator to dictionary keys so would be idiomatic to write this as if "TASKRC" in os.environ:

@ryneeverett
Copy link
Contributor

I've added a function, find_taskrc(), that checks these locations, in this order:

1. `${TASKRC}` env var

2. `${HOME}/.taskrc`

3. `${XDG_CONFIG_HOME}/task/taskrc`

I'm assuming this is the order in which taskwarrior itself looks for a configuration file?

@adam-gaia
Copy link
Author

adam-gaia commented Jan 10, 2023

I'm assuming this is the order in which taskwarrior itself looks for a configuration file?

I thought so, but I'm glad you asked because that made me double check. I was wrong.

From the manual

$HOME/.taskrc
TASKRC=<directory-path>/.taskrc
XDG_CONFIG_HOME=<directory-path>/task/taskrc

I'll update to mirror that order.

Edit: Fixed in latest commit

@adam-gaia
Copy link
Author

adam-gaia commented Jan 10, 2023

By the way, thank you @ryneeverett for such a quick response!

else:
raise FileNotFoundError("Environment variable 'TASKRC' did not resolve to a taskrc file")

if "XDG_CONFIG_HOME" in os.environ.keys():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could remove the .keys() here as well.

@ryneeverett
Copy link
Contributor

Where are you reading this configuration file search order? https://taskwarrior.org/docs/configuration/ ? I don't know the whole lookup order but I'm pretty sure the TASKRC environment variable is first.

@ryneeverett
Copy link
Contributor

From the man page:

   ~/.taskrc
          User configuration file - see also taskrc(5).  Note that this can be overridden on the command line or by the  TASKRC  environment  variable.
          Also,  if ~/.taskrc doesn't exist and XDG_CONFIG_HOME environment variable is defined, taskwarrior will check if $XDG_CONFIG_HOME/task/taskrc
          exists and attempt to read it

I read this as:

  1. $TASKRC
  2. ~/.taskrc
  3. $XDG_CONFIG_HOME/task/taskrc

I suspect you got it right the first time.

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

Successfully merging this pull request may close these issues.

2 participants