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

feat: respect XDG spec for configuration files #300

Merged
merged 4 commits into from
Feb 17, 2023
Merged

feat: respect XDG spec for configuration files #300

merged 4 commits into from
Feb 17, 2023

Conversation

dkvc
Copy link
Contributor

@dkvc dkvc commented Feb 15, 2023

Use Path.expanduser from pathlib module instead of os module
Remove redundant DEFAULT_CONFIG_PATH in commands.py
Closes #126

@AuHau
Copy link
Owner

AuHau commented Feb 15, 2023

Thanks for the new PR! I'm bit confused thought. Shouldn't this PR add support for the XDG spec? I don't see usage of the expected env. variable... Or does the Path module handle this as expected?

@dkvc
Copy link
Contributor Author

dkvc commented Feb 15, 2023

Thanks for the new PR! I'm bit confused thought. Shouldn't this PR add support for the XDG spec? I don't see usage of the expected env. variable... Or does the Path module handle this as expected?

The Path module handles it automatically. This PR actually updates the old os.path.expanduser to pathlib.Path.expanduser. The main benefit of this PR is removing redundant DEFAULT_CONFIG_PATH and adding pathlib.Path.expanduser; the latter automatically is updated version of old one and uses environment variables set by user/default.

@AuHau
Copy link
Owner

AuHau commented Feb 15, 2023

Hmmm, I don't know about it. Quick googling nor the documentation did not confirm that the pathlib.Path.expanduser should follow XDG spec. Do you have a machine that uses XDG? Can you confirm that when the XDG_CONFIG_HOME is set it changes the behavior?

@dkvc
Copy link
Contributor Author

dkvc commented Feb 16, 2023

Yes, you're correct. I'm sorry. It looks like neither pathlib.Path.expanduser nor os.path.expanduser follows XDG spec for HOME config. They only check if $HOME env is set or uses default variable. In most cases, this is fine since most Linux distros set $XDG_CONFIG_HOME as empty or root directory.

Env variables on Podman container:
Env variables on Podman container

Env variables on default Fedora installation:
Env variables on default Fedora installation

There is still an option to check if it is set, by checking if it is set. But what would happen if it defaults to root directory as in Podman container (First image)? 😨

@AuHau
Copy link
Owner

AuHau commented Feb 16, 2023

It looks like neither pathlib.Path.expanduser nor os.path.expanduser follows XDG spec for HOME config.

Yeah, I thought so. That is the reason why #126 was created...

There is still an option to check if it is set, by checking if it is set. But what would happen if it defaults to root directory as in Podman container (First image)?

Yeah I would go with the route to check if it is set and if so, to use that otherwise, fall back to the pathlib.Path.expanduser()...

IMHO, it is not our problem as to what is the XDG_CONFIG_HOME set to. By the XDG spec this is a place where "Where user-specific configurations should be written", if it is set to root, then it is a problem of the OS environment or users configuration... Or maybe there is some reason for it... Who knows...

If XDG_CONFIG_HOME is set, use it;
or else default to $HOME(~) directory.
@dkvc
Copy link
Contributor Author

dkvc commented Feb 16, 2023

By the XDG spec this is a place where "Where user-specific configurations should be written", if it is set to root, then it is a problem of the OS environment or users configuration... Or maybe there is some reason for it... Who knows...

Sorry, it was only that specific container.. Hence, we can just check if XDG_CONFIG_HOME is set. If it is set, use it; else default to $HOME.

There you go. Done!

Copy link
Owner

@AuHau AuHau left a comment

Choose a reason for hiding this comment

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

Nice! Thanks a lot!

Copy link
Owner

@AuHau AuHau left a comment

Choose a reason for hiding this comment

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

Actually 🙈 I thought about it a bit more and realize that this is a breaking change, which we can and should avoid in this form.

The problem is that existing users have their configuration under ~/.togglrc. For users that have configured XDG_CONFIG_HOME and updated toggl-cli to the new version with this change, toggl-cli will look for configuration under $XDG_CONFIG_HOME/.togglrc, which will fail because it does not exist. But it exists under ~/.togglrc... Which will break things.

I would therefore suggest this solution. If XDG_CONFIG_HOME is configured, and $XDG_CONFIG_HOME/.togglrc does not exists, fallback to ~/.togglrc. We could also log a warning for this case.

We could potentially move the configuration from the old place to the new one, but IMHO, that is too proactive and magical behavior, and I would leave the move up to the user so he is aware of that.

@dkvc
Copy link
Contributor Author

dkvc commented Feb 16, 2023

If XDG_CONFIG_HOME is configured, and $XDG_CONFIG_HOME/.togglrc does not exists, fallback to ~/.togglrc. We could also log a warning for this case.

Yes, I didn't consider this possibility. I think this sentence is misinterpreting or I might be wrong;
If $XDG_CONFIG_HOME is configured and ~/.togglrc doesn't exist, then use $XDG_CONFIG_HOME/.togglrc. Else fallback to ~/.togglrc.

@StanczakDominik
Copy link
Collaborator

Yeah, fallback to trying ~/.togglrc if we tried $XDG_CONFIG_HOME and that didn't exist 😄

@dkvc
Copy link
Contributor Author

dkvc commented Feb 16, 2023

Yeah, fallback to trying ~/.togglrc if we tried $XDG_CONFIG_HOME and that didn't exist 😄

Wait, I'm confused. Aren't we creating $XDG_CONFIG_HOME/.togglrc?.
It does mean we have to check if ~/.togglrc exists, right?

Edit: This should be better I guess. If 👍 , I will push the changes.

_file_path = Path.expanduser(Path('~/.togglrc'))
    if "XDG_CONFIG_HOME" in os.environ and not _file_path.exists():
        DEFAULT_CONFIG_PATH = Path(os.environ["XDG_CONFIG_HOME"]).joinpath(".togglrc")
    else:
        DEFAULT_CONFIG_PATH = _file_path

@AuHau
Copy link
Owner

AuHau commented Feb 17, 2023

Your solution is almost there, but not completely. The cases I want to cover are these:
image

So basically $XDG_CONFIG_HOME/.togglrc is used only if it exists or no .togglrc (eq. ~/.togglrc) exists yet (and XDG_CONFIG_HOME is configured).

@dkvc
Copy link
Contributor Author

dkvc commented Feb 17, 2023

I suppose this should be able to cover all the mentioned edge cases. Shall warnings.warn be added to describe about this modification?

_old_file_path = Path.expanduser(Path('~/.togglrc'))
    
    if "XDG_CONFIG_HOME" in os.environ:
        _new_file_path = Path(os.environ["XDG_CONFIG_HOME"]).joinpath(".togglrc")
        
        if _new_file_path.exists() or not _old_file_path.exists():
            DEFAULT_CONFIG_PATH = _new_file_path
        else:
            DEFAULT_CONFIG_PATH = _old_file_path
            
    else:
        DEFAULT_CONFIG_PATH = _old_file_path

@AuHau
Copy link
Owner

AuHau commented Feb 17, 2023

Yes, that looks about right! 👍

if XDG_CONFIG_HOME in os.env:
    if XDG_CONFIG_HOME/.togglrc exists or ~/.togglrc does not exist:
        USE XDG_CONFIG_HOME/.togglrc
    else:
        FALLBACK to ~/.togglrc
else:
    FALLBACK to ~/.togglrc
@dkvc dkvc requested a review from AuHau February 17, 2023 12:25
Copy link
Owner

@AuHau AuHau left a comment

Choose a reason for hiding this comment

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

LGTM! Thanks for the tweaks! 👍

@AuHau AuHau changed the title Use pathlib library for toggl-cli Path feat: respect XDG spec for configuration files Feb 17, 2023
@AuHau AuHau merged commit 5086039 into AuHau:master Feb 17, 2023
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.

Follow XDG Base Directory Spec
3 participants