Skip to content

Commit 248dc3f

Browse files
authored
feat: check $XDG_HOME_CONFIG/wishlist/config.ya?ml as well (#74)
* feat: check ~/.ssh/config.ya?ml as well Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com> * Update cmd/wishlist/main.go * Update cmd/wishlist/main.go * Update cmd/wishlist/main.go * feat: read from xdg config Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com> * fix: rm Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com> * docs: readme Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>
1 parent f6502db commit 248dc3f

3 files changed

Lines changed: 61 additions & 15 deletions

File tree

‎README.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,15 @@ The config files are tried in the following order:
130130
- the `-config` flag in either YAML or SSH config formats
131131
- `.wishlist/config.yaml`
132132
- `.wishlist/config.yml`
133+
- `.wishlist/config`
134+
- `[[user config dir]]/wishlist/config.yaml`[^1]
135+
- `[[user config dir]]/wishlist/config.yml`[^1]
136+
- `[[user config dir]]/wishlist/config`[^1]
133137
- `$HOME/.ssh/config`
134138
- `/etc/ssh/ssh_config`
135139

140+
[^1]: i.e. `[[user config dir]]`: On Unix systems, it will be `$XDG_CONFIG_HOME` as specified by https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html if non-empty, else `$HOME/.config`. On Darwin, it will be `$HOME/Library/Application Support`. On Windows, it will be `%AppData%`. On Plan 9, it will be `$home/lib`.
141+
136142
The first one that is loaded and parsed without errors will be used.
137143
This means that if you have your common used hosts in your `~/.ssh/config`, you can simply run `wishlist` and get it running right away.
138144
It also means that if you don't want that, you can pass a path to `-config`, and it can be either a YAML or a SSH config file.

‎cmd/wishlist/main.go‎

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ import (
77
"os"
88
"path/filepath"
99
"runtime/debug"
10+
"strings"
1011

1112
tea "github.com/charmbracelet/bubbletea"
1213
"github.com/charmbracelet/keygen"
1314
"github.com/charmbracelet/wish"
1415
"github.com/charmbracelet/wish/activeterm"
1516
lm "github.com/charmbracelet/wish/logging"
1617
"github.com/charmbracelet/wishlist"
17-
"github.com/charmbracelet/wishlist/home"
1818
"github.com/charmbracelet/wishlist/sshconfig"
1919
"github.com/gliderlabs/ssh"
2020
"github.com/hashicorp/go-multierror"
@@ -120,7 +120,8 @@ var serverCmd = &cobra.Command{
120120
var configFile string
121121

122122
func init() {
123-
rootCmd.PersistentFlags().StringVarP(&configFile, "config", "c", "", "Path to the config file to use. Defaults to, in order of preference: $PWD/.wishlist/config.yaml, $PWD/.wishlist/config.yml, $HOME/.ssh/config, /etc/ssh/ssh_config")
123+
paths, _ := userConfigPaths()
124+
rootCmd.PersistentFlags().StringVarP(&configFile, "config", "c", "", "Path to the config file to use. Defaults to, in order of preference: "+strings.Join(paths, ", "))
124125
rootCmd.AddCommand(serverCmd, manCmd)
125126
}
126127

@@ -133,20 +134,37 @@ func main() {
133134
}
134135
}
135136

136-
func getConfig(path string) (wishlist.Config, error) {
137+
func userConfigPaths() ([]string, error) {
138+
cfg, err := os.UserConfigDir()
139+
if err != nil {
140+
return nil, fmt.Errorf("could not get user config dir: %w", err)
141+
}
142+
143+
home, err := os.UserHomeDir()
144+
if err != nil {
145+
return nil, fmt.Errorf("could not get user home dir: %w", err)
146+
}
147+
148+
cfg = filepath.Join(cfg, "wishlist")
149+
return []string{
150+
".wishlist/config.yaml",
151+
".wishlist/config.yml",
152+
".wishlist/config",
153+
filepath.Join(cfg, "config.yaml"),
154+
filepath.Join(cfg, "config.yml"),
155+
filepath.Join(cfg, "config"),
156+
filepath.Join(home, ".ssh", "config"),
157+
"/etc/ssh/ssh_config",
158+
}, nil
159+
}
160+
161+
func getConfig(configFile string) (wishlist.Config, error) {
137162
var allErrs error
138-
for _, fn := range []func() string{
139-
func() string { return path },
140-
func() string { return ".wishlist/config.yaml" },
141-
func() string { return ".wishlist/config.yml" },
142-
func() string { return ".wishlist/config" },
143-
func() string {
144-
s, _ := home.ExpandPath("~/.ssh/config")
145-
return s
146-
},
147-
func() string { return "/etc/ssh/ssh_config" },
148-
} {
149-
path := fn()
163+
paths, err := userConfigPaths()
164+
if err != nil {
165+
return wishlist.Config{}, err
166+
}
167+
for _, path := range append([]string{configFile}, paths...) {
150168
if path == "" {
151169
continue
152170
}

‎cmd/wishlist/main_test.go‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,25 @@ func TestGetConfig(t *testing.T) {
5656
})
5757
})
5858
}
59+
60+
func TestAllPossibleConfigPaths(t *testing.T) {
61+
cfg, err := os.UserConfigDir()
62+
require.NoError(t, err)
63+
64+
home, err := os.UserHomeDir()
65+
require.NoError(t, err)
66+
67+
paths, err := userConfigPaths()
68+
require.NoError(t, err)
69+
require.Len(t, paths, 8)
70+
require.Equal(t, []string{
71+
".wishlist/config.yaml",
72+
".wishlist/config.yml",
73+
".wishlist/config",
74+
filepath.Join(cfg, "wishlist", "config.yaml"),
75+
filepath.Join(cfg, "wishlist", "config.yml"),
76+
filepath.Join(cfg, "wishlist", "config"),
77+
filepath.Join(home, ".ssh", "config"),
78+
"/etc/ssh/ssh_config",
79+
}, paths)
80+
}

0 commit comments

Comments
 (0)
⌂ 换网址