From fe858b3a68193c84d9f67bd3933bb0a5b8b48ef5 Mon Sep 17 00:00:00 2001 From: Walter Scheper Date: Sun, 26 Feb 2023 13:50:14 -0500 Subject: [PATCH] fix(userinfo): use stdlib to find config dir --- internal/userinfo/userinfo.go | 12 ++---- internal/userinfo/userinfo_darwin.go | 21 ---------- internal/userinfo/userinfo_darwin_test.go | 25 ----------- internal/userinfo/userinfo_linux.go | 27 ------------ internal/userinfo/userinfo_linux_test.go | 51 ----------------------- internal/userinfo/userinfo_windows.go | 27 ------------ 6 files changed, 4 insertions(+), 159 deletions(-) delete mode 100644 internal/userinfo/userinfo_darwin.go delete mode 100644 internal/userinfo/userinfo_darwin_test.go delete mode 100644 internal/userinfo/userinfo_linux.go delete mode 100644 internal/userinfo/userinfo_linux_test.go delete mode 100644 internal/userinfo/userinfo_windows.go diff --git a/internal/userinfo/userinfo.go b/internal/userinfo/userinfo.go index 9dd88c8..3e50822 100644 --- a/internal/userinfo/userinfo.go +++ b/internal/userinfo/userinfo.go @@ -17,21 +17,17 @@ package userinfo import ( - "errors" - "os/user" + "os" "path/filepath" ) // DefaultConfigFile returns the path to default location for a config file, // based on the underlying OS. func DefaultConfigFile(appName string) (string, error) { - u, err := user.Current() + configDir, err := os.UserConfigDir() if err != nil { return "", err } - if u.HomeDir == "" { - return "", errors.New("cannot determine user specific home directory") - } - cfgDir := DefaultConfigDir(u.HomeDir) - return filepath.Join(cfgDir, appName, appName+".conf"), nil + + return filepath.Join(configDir, appName, appName+".conf"), nil } diff --git a/internal/userinfo/userinfo_darwin.go b/internal/userinfo/userinfo_darwin.go deleted file mode 100644 index aa74c93..0000000 --- a/internal/userinfo/userinfo_darwin.go +++ /dev/null @@ -1,21 +0,0 @@ -// Copyright © 2017 Walter Scheper -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package userinfo - -import "path/filepath" - -// DefaultConfigDir returns the path to the OS specific user config directory. -func DefaultConfigDir(homeDir string) string { - return filepath.Join(homeDir, "Library", "Application Support") -} diff --git a/internal/userinfo/userinfo_darwin_test.go b/internal/userinfo/userinfo_darwin_test.go deleted file mode 100644 index 563f934..0000000 --- a/internal/userinfo/userinfo_darwin_test.go +++ /dev/null @@ -1,25 +0,0 @@ -package userinfo - -import ( - "os" - "path/filepath" - "testing" -) - -func TestDefaultConfigDir(t *testing.T) { - actual := DefaultConfigDir("foo") - if "foo/Library/Application Support" != actual { - t.Errorf("wrong config dir") - } -} - -func TestDefaultConfigFile(t *testing.T) { - actual, err := DefaultConfigFile("foo") - if err != nil { - t.Fatal(err) - } - expected := filepath.Join("/Users", os.Getenv("USER"), "Library/Application Support", "foo", "foo.conf") - if actual != expected { - t.Errorf("wrong config file") - } -} diff --git a/internal/userinfo/userinfo_linux.go b/internal/userinfo/userinfo_linux.go deleted file mode 100644 index b4f02ee..0000000 --- a/internal/userinfo/userinfo_linux.go +++ /dev/null @@ -1,27 +0,0 @@ -// Copyright © 2017 Walter Scheper -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package userinfo - -import ( - "os" - "path/filepath" -) - -// DefaultConfigDir returns the path to the OS specific user config directory. -func DefaultConfigDir(homeDir string) string { - if cfgDir, ok := os.LookupEnv("XDG_CONFIG_HOME"); ok { - return cfgDir - } - return filepath.Join(homeDir, ".config") -} diff --git a/internal/userinfo/userinfo_linux_test.go b/internal/userinfo/userinfo_linux_test.go deleted file mode 100644 index e6d6095..0000000 --- a/internal/userinfo/userinfo_linux_test.go +++ /dev/null @@ -1,51 +0,0 @@ -// Copyright © 2017 Walter Scheper -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package userinfo - -import ( - "os" - "path/filepath" - "testing" -) - -func TestDefaultConfigDir(t *testing.T) { - actual := DefaultConfigDir("foo") - if "foo/.config" != actual { - t.Errorf("wrong config dir") - } -} - -func TestDefaultConfigFile(t *testing.T) { - actual, err := DefaultConfigFile("foo") - if err != nil { - t.Fatal(err) - } - expected := filepath.Join("/home", os.Getenv("USER"), ".config", "foo", "foo.conf") - if actual != expected { - t.Errorf("wrong config file") - } -} - -func TestDefaultConfigDirEnv(t *testing.T) { - os.Setenv("XDG_CONFIG_HOME", "/foo") - defer func() { - if err := os.Unsetenv("XDG_CONFIG_HOME"); err != nil { - t.Fatal(err) - } - }() - actual := DefaultConfigDir("bar") - if actual != "/foo" { - t.Error("wrong config dir") - } -} diff --git a/internal/userinfo/userinfo_windows.go b/internal/userinfo/userinfo_windows.go deleted file mode 100644 index 13392db..0000000 --- a/internal/userinfo/userinfo_windows.go +++ /dev/null @@ -1,27 +0,0 @@ -// Copyright © 2017 Walter Scheper -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package userinfo - -import ( - "os" - "path/filepath" -) - -// DefaultConfigDir returns the path to the OS specific user config directory. -func DefaultConfigDir(homeDir string) string { - if cfgDir := os.Getenv("APPDATA"); cfgDir != "" { - return cfgDir - } - return filepath.Join(homeDir, "AppData", "Roaming") -}