Hello, all!

I have found mbsync to work well for me, but encountered minor frustration in 
regards to the default
config file location. I like to keep my $HOME very clean (in stark contrast to 
my home), and
therefore tend to keep all my configuration files in ~/.config. This was not a 
huge issue
since the --config option exists, but I have wound up with 3 scripts on my 
system that call mbsync,
and all of them need to be passed the --command option. If for some reason I 
ever move the config
file, I will need to remember to update all 3 scripts, or else I will be left 
with a broken system
(due to my own stupidity).

Considering this, I would like to submit the following patch which has mbsync 
look for a config
file in 3 places in the following order:
    1. $MBSYNC_CONFIG - a user-set environment variable
    2. $XDG_CONFIG_HOME/mbsync
    3. ~/.mbsync (the legacy location)
This patch still respects the --config option superseding these locations.

Please let me know your thoughts, and have a lovely day.

--Wyatt Sheffield
>From bf6f07f7cc4947e678ee965c959df289cc07c9db Mon Sep 17 00:00:00 2001
From: Wyatt Sheffield <c...@wyatts.xyz>
Date: Thu, 7 May 2020 17:29:18 -0500
Subject: [PATCH] Respect XDG Base Directory specification

---
 src/config.c | 22 +++++++++++++++++++++-
 src/mbsync.1 | 20 ++++++++++++++++++--
 2 files changed, 39 insertions(+), 3 deletions(-)

diff --git a/src/config.c b/src/config.c
index 8ddfe26..cbf56ae 100644
--- a/src/config.c
+++ b/src/config.c
@@ -322,10 +322,30 @@ load_config( const char *where, int pseudo )
        int len, cops, gcops, max_size, ms, i;
        char path[_POSIX_PATH_MAX];
        char buf[1024];
+       char* xdg;
+       char* config_env;
+       int found_config;
 
        if (!where) {
                assert( !pseudo );
-               nfsnprintf( path, sizeof(path), "%s/." EXE "rc", Home );
+               // First, look to see if the user has set the MBSYNC_CONFIG 
environment
+               // variable to point to the config file and check that the file 
is
+               // readable
+               if ( (config_env = getenv("MBSYNC_CONFIG")) ) {
+                       nfsnprintf( path, sizeof(path), "%s", config_env );
+                       found_config = access(path, R_OK);
+               }
+               // If a readable file was not found, or the environment 
variable was not
+               // set, look in XDG_CONFIG_HOME for a file called mbsyncrc
+               if ( found_config != 0 &&(xdg = getenv("XDG_CONFIG_HOME")) ) {
+                       nfsnprintf( path, sizeof(path), "%s/" EXE "rc", xdg );
+                       found_config = access(path, R_OK);
+               }
+               // Finally, the legacy behavior: look for a file called 
.mbsyncrc in the
+               // user's home directory.
+               if ( found_config != 0 ) {
+                       nfsnprintf( path, sizeof(path), "%s/." EXE "rc", Home );
+               }
                cfile.file = path;
        } else
                cfile.file = where;
diff --git a/src/mbsync.1 b/src/mbsync.1
index 8f6c58b..e226472 100644
--- a/src/mbsync.1
+++ b/src/mbsync.1
@@ -48,7 +48,8 @@ Multiple replicas of each mailbox can be maintained.
 .TP
 \fB-c\fR, \fB--config\fR \fIfile\fR
 Read configuration from \fIfile\fR.
-By default, the configuration is read from ~/.mbsyncrc.
+By default, the configuration is read from $MBSYNC_CONFIG first, then
+$XDG_CONFIG_HOME/mbsyncrc, and finally ~/.mbsyncrc.
 .TP
 \fB-a\fR, \fB--all\fR
 Select all configured channels. Any channel/group specifications on the command
@@ -731,10 +732,25 @@ before the mailbox is expunged.
 There is no risk as long as the IMAP mailbox is accessed by only one client
 (including \fBmbsync\fR) at a time.
 ..
+.SH ENVIRONMENT
+.TP
+.B MBSYNC_CONFIG
+Mbsync will use this value as the location of the config file.
+..
 .SH FILES
 .TP
+.B $MBSYNC_CONFIG
+If this environment variable has been set, mbsync will attempt to read the file
+pointed to by MBSYNC_CONFIG
+.TP
+.B $XDG_CONFIG_HOME/mbsyncrc
+If there was nothing found at MBSYNC_CONFIG, and XDG_CONFIG_HOME is set
+(ususally to ~/.config), mbsync will look for its configuration file named
+mbsyncrc in that directory
+.TP
 .B ~/.mbsyncrc
-Default configuration file
+Legacy configuration file location. Used if no configuration file was found at
+either $MBSYNC_CONFIG or $XDG_CONFIG_HOME/mbsyncrc.
 .TP
 .B ~/.mbsync/
 Directory containing synchronization state files
-- 
2.26.2

_______________________________________________
isync-devel mailing list
isync-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/isync-devel

Reply via email to