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

Use XDG Base Directories #96

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

aDogCalledSpot
Copy link

@aDogCalledSpot aDogCalledSpot commented Jan 31, 2020

The XDG Base Directories are currently unsupported. You can find the specification here. A brief summary is available on the ArchWiki.

Using the specification gives the user more flexibility in where certain files should go. Programs can also take advantage of the directories being used by, for example, having backup programs exclude $XDG_CACHE_HOME.

Which files go where should be discussed. My proposal would be:

  • TEMPLATE_FILE doesn't appear provide any user-specific content so it would be placed into $XDG_CACHE_HOME
  • CONFIG_FILE is a configuration file and should go into $XDG_CONFIG_HOME
  • SERVER_INFO_FILE goes into $XDG_CACHE_HOME
  • SPLIT_TUNNEL_FILE goes into $XDG_DATA_HOME
  • OVPN_FILE goes into $XDG_DATA_HOME
  • PASSFILE goes into $XDG_DATA_HOME
  • Log files of any sort have been put into $XDG_CACHE_HOME. It should be noted that there are programs which handle this differently.
  • Backups are user-specific and have been put $XDG_DATA_HOME

In order to not break any current configurations, it will first be checked if "$HOME/.pvpn-cli" exists and if it does it will be used as the directory in which all files would be placed (as before).

The function purge_configuration currently deletes everything in the config directory. For now, I have kept it that it only deletes the configuration file, this could be changed to also delete everything in the data and/or cache directory.

@Rafficer
Copy link
Owner

Thank you for your contribution.

How much did you test this already?

What I don't like that much is that even when the xdg variables aren't defined it defaults to the "recommended" paths.

This would also require documentation about each file and where it would be located. Which is currently quite easy: Everything is in ~/.pvpn-cli.

@aDogCalledSpot
Copy link
Author

How much did you test this already

So far I have made sure that all the path files are set correctly. I have not extensively tested functionality. I'll make sure to run more tests once we have agreed on where everything should go.

What I don't like that much is that even when the xdg variables aren't defined it defaults to the "recommended" paths.

It is what is stated in the specification and is followed by many programs. No distributions I am aware of set the environment variables by default yet ~/.config is used by various programs.

This would also require documentation about each file and where it would be located.

Where would you suggest we put this? Would it be sufficient to write it into the README?

@Rafficer
Copy link
Owner

So far I have made sure that all the path files are set correctly. I have not extensively tested functionality. I'll make sure to run more tests once we have agreed on where everything should go.

One thing I've noticed is that you can't use it anymore when the base directories don't exist already. (~/.local/share)

Where would you suggest we put this? Would it be sufficient to write it into the README?

Probably DOCUMENTATION.md or something the like, (doesn't exist yet, but very much needed in the future) which would be for a technical documentation.

@@ -80,13 +80,15 @@ def cli():
"""Run user's input command."""

# Initial log values
change_file_owner(os.path.join(CONFIG_DIR, "pvpn-cli.log"))
change_file_owner(os.path.join(CACHE_DIR, "pvpn-cli.log"))
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't this be LOG_DIR?

Copy link
Author

Choose a reason for hiding this comment

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

There currently isn't a LOG_DIR as there isn't one listed in the specification. As log files are not necessary to run the program they are placed into the CACHE_DIR. I could make a separate LOG_DIR and have that be the same as CACHE_DIR to make the code clearer.

Copy link
Contributor

Choose a reason for hiding this comment

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

Choose a reason for hiding this comment

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

The specification does state the following

The $XDG_STATE_HOME contains state data that should persist between (application) restarts, but that is not important or portable enough to the user that it should be stored in $XDG_DATA_HOME. It may contain:

  • actions history (logs, history, recently used files, …)
  • [......]

This was was already stated in the stack overflow link above as well.

Admittedly I have not defined this environment variable in my own implementation. (I only have XDG_DATA_HOME and XDG_CONFIG_HOME environment variable defined).

While it may not be set in stone, I think the XDG Base Directories is the best and most widely accepted standard that we have. Implementing this will further encourage adoption, keep $HOME directories clean and easier to maintain. This change also is somewhat trivial and provides more value than the pain of adding it.

While I understand that this seems to scatter the various files needed by protonvpn_cli among the different directories, keep in mind that this is the case with system applications as well (/etc for config files, /var/log for logs, etc....). Having the $HOME organized thus does not increase its complexity. Rather it only improves upon organization facilitating automated maintenance/archival tasks.

@aDogCalledSpot
Copy link
Author

aDogCalledSpot commented Jan 31, 2020

One thing I've noticed is that you can't use it anymore when the base directories don't exist already.

They would be created by init_cli(). Without the XDG directories, the config directory is also created in logger.py and the user data wouldn't be created.

I could check in order whether the configuration files can be found in the XDG directory (if the environment variable is set or the default if it is not set) and then the home directory. If no files are found the user is told to run protonvpn init and the program exits.

Probably DOCUMENTATION.md or something the like

Would this only be an internal documentation? Where configuration files are stored should also be available to users, so a man page would be pretty good. Though this should probably be a separate issue.

@Rafficer
Copy link
Owner

Rafficer commented Feb 1, 2020

They would be created by init_cli(). Without the XDG directories, the config directory is also created in logger.py and the user data wouldn't be created.

Not in the current form. It's basically mkdir vs mkdir -p.

Would this only be an internal documentation? Where configuration files are stored should also be available to users, so a man page would be pretty good. Though this should probably be a separate issue.

Not sure what you mean by internal. It would just be another file in this git repository.

Comment on lines +19 to +23
xdg_config_str = os.getenv("XDG_CONFIG_HOME")
if not xdg_config_str:
xdg_config = os.path.join(home_path, ".config")
else:
xdg_config = os.path.realpath(xdg_config_str)
Copy link

Choose a reason for hiding this comment

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

nit: perhaps you can simplify it by writing

xdg_config = os.path.realpath(
    os.getenv("XDG_CONFIG_HOME", os.path.join(home_path, ".config"))
)

Choose a reason for hiding this comment

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

You could also use the XDG module (last updated April of 2021) to facilitate the base directory specification.

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.

5 participants