Skip to content

Commit b48a982

Browse files
lgarronmbrt
andauthored
Default to ~/.config/gmailctl for newly created config dirs. (#455)
* Default to ~/.config/gmailctl for newly created config dirs. This is a minimal fix for #144 It is a revision of #393 and uses a library for the internal XDG path calculation per feedback there. * Update cmd/gmailctl/cmd/root_cmd.go Co-authored-by: Michele Bertasi <405934+mbrt@users.noreply.github.com> * Update README.md --------- Co-authored-by: Michele Bertasi <405934+mbrt@users.noreply.github.com>
1 parent 1316284 commit b48a982

4 files changed

Lines changed: 26 additions & 7 deletions

File tree

README.md

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ label, archive and manage your inbox automatically.
1313
- [Motivation](#motivation)
1414
- [Install](#install)
1515
- [Usage](#usage)
16+
- [Config directory](#config-directory)
1617
- [Migrate from another solution](#migrate-from-another-solution)
1718
- [Other commands](#other-commands)
1819
- [Configuration](#configuration)
@@ -136,17 +137,24 @@ settings without leaving your command line.
136137
[![asciicast](https://asciinema.org/a/1NIWhzeJNcrN7cCe7mGjWQQnx.svg)](https://asciinema.org/a/1NIWhzeJNcrN7cCe7mGjWQQnx)
137138

138139
The easiest way to use gmailctl is to run `gmailctl edit`. This will open the
139-
local `.gmailctl/config.jsonnet` file in your editor. After you exit the editor
140-
the configuration is applied to Gmail. See [Configuration](#configuration) for
141-
the configuration file format. This is the preferred way if you want to start
142-
your filters from scratch.
140+
local config file in your editor. After you exit
141+
the editor the configuration is applied to Gmail. See
142+
[Configuration](#configuration) for the configuration file format. This is the
143+
preferred way if you want to start your filters from scratch.
143144

144145
**NOTE:** It's recommended to backup your current configuration before you apply
145146
the generated one for the first time. Your current filters will be wiped and
146147
replaced with the ones specified in the config file. The diff you'll get during
147148
the first run will probably be pretty big, but from that point on, all changes
148149
should generate a small and simple to review diff.
149150

151+
### Config directory
152+
153+
Configuration and credentials are in either:
154+
155+
- `<XDG_BASE_DIR>/gmailctl` (using the [XDG base directory spec](https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html), commonly `~/.config/gmailctl` on Linux.
156+
- `~/.gmailctl`: The previous default location. If the directory exists `gmailctl` will continue to use it for backward compatibility
157+
- Custom location: use the `--config` argument.
150158
### Migrate from another solution
151159

152160
If you want to preserve your current filters and migrate to a more sane
@@ -164,7 +172,8 @@ Example of usage:
164172

165173
```bash
166174
# download the filters to the default configuration file
167-
gmailctl download > ~/.gmailctl/config.jsonnet
175+
mkdir -p ~/.config/gmailctl
176+
gmailctl download > ~/.config/gmailctl/config.jsonnet
168177
# check that the diff is empty and no errors are present
169178
gmailctl diff
170179
# happy editing!

cmd/gmailctl/cmd/root_cmd.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"os/user"
77
"path"
88

9+
"github.com/adrg/xdg"
910
"github.com/mattn/go-isatty"
1011
"github.com/spf13/cobra"
1112
)
@@ -53,7 +54,7 @@ func init() {
5354
// Here you will define your flags and configuration settings.
5455
// Cobra supports persistent flags, which, if defined here,
5556
// will be global for your application.
56-
rootCmd.PersistentFlags().StringVar(&cfgDir, "config", "", "config directory (default is $HOME/.gmailctl)")
57+
rootCmd.PersistentFlags().StringVar(&cfgDir, "config", "", "config directory (defaults to $HOME/.gmailctl if it exists, else $HOME/.config/gmailctl)")
5758
rootCmd.PersistentFlags().StringVar(&colorFlag, "color", "auto",
5859
"whether to enable color output ('always', 'auto' or 'never')")
5960
rootCmd.PersistentFlags().Lookup("color").NoOptDefVal = "always"
@@ -71,7 +72,13 @@ func initConfig() {
7172
fmt.Println(err)
7273
os.Exit(1)
7374
}
74-
cfgDir = path.Join(usr.HomeDir, ".gmailctl")
75+
76+
legacyCfgDir := path.Join(usr.HomeDir, ".gmailctl")
77+
if _, err := os.Stat(legacyCfgDir); err != nil && os.IsNotExist(err) {
78+
cfgDir = path.Join(xdg.ConfigHome, "gmailctl")
79+
} else {
80+
cfgDir = legacyCfgDir
81+
}
7582
}
7683

7784
// shouldUseColorDiff decides, based on the value of the color flag and other

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.25.0
55
toolchain go1.26.1
66

77
require (
8+
github.com/adrg/xdg v0.5.3
89
github.com/fatih/color v1.18.1-0.20251010072730-26fd5780133d
910
github.com/google/go-jsonnet v0.21.0
1011
github.com/gorilla/mux v1.8.1

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ cloud.google.com/go/auth/oauth2adapt v0.2.8 h1:keo8NaayQZ6wimpNSmW5OPc283g65QNIi
44
cloud.google.com/go/auth/oauth2adapt v0.2.8/go.mod h1:XQ9y31RkqZCcwJWNSx2Xvric3RrU88hAYYbjDWYDL+c=
55
cloud.google.com/go/compute/metadata v0.9.0 h1:pDUj4QMoPejqq20dK0Pg2N4yG9zIkYGdBtwLoEkH9Zs=
66
cloud.google.com/go/compute/metadata v0.9.0/go.mod h1:E0bWwX5wTnLPedCKqk3pJmVgCBSM6qQI1yTBdEb3C10=
7+
github.com/adrg/xdg v0.5.3 h1:xRnxJXne7+oWDatRhR1JLnvuccuIeCoBu2rtuLqQB78=
8+
github.com/adrg/xdg v0.5.3/go.mod h1:nlTsY+NNiCBGCK2tpm09vRqfVzrc2fLmXGpBLF0zlTQ=
79
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
810
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
911
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=

0 commit comments

Comments
 (0)