Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 5 additions & 26 deletions auth/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ import (
"net/url"
"os"
"path/filepath"
"runtime"
"slices"
"sync"

"github.com/columnar-tech/dbc/internal"
"github.com/pelletier/go-toml/v2"
)

Expand Down Expand Up @@ -136,32 +136,11 @@ func init() {
}

func getCredentialPath() (string, error) {
dir := os.Getenv("XDG_DATA_HOME")
if dir == "" {
switch runtime.GOOS {
case "windows":
dir = os.Getenv("LocalAppData")
if dir == "" {
return "", errors.New("%LocalAppData% is not set")
}
case "darwin":
home, err := os.UserHomeDir()
if err != nil {
return "", fmt.Errorf("failed to get user home directory: %w", err)
}
dir = filepath.Join(home, "Library")
default: // unix
home, err := os.UserHomeDir()
if err != nil {
return "", fmt.Errorf("failed to get user home directory: %w", err)
}
dir = filepath.Join(home, ".local", "share")
}
} else if !filepath.IsAbs(dir) {
return "", errors.New("path in $XDG_DATA_HOME is relative")
dbcConfigDir, err := internal.GetDbcConfigDir()
if err != nil {
return "", fmt.Errorf("failed to get credentials path: %v", err)
}

return filepath.Join(dir, "dbc", "credentials", "credentials.toml"), nil
return filepath.Join(dbcConfigDir, "credentials", "credentials.toml"), nil
}

func loadCreds() ([]Credential, error) {
Expand Down
10 changes: 3 additions & 7 deletions drivers.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"github.com/Masterminds/semver/v3"
"github.com/ProtonMail/gopenpgp/v3/crypto"
"github.com/columnar-tech/dbc/auth"
"github.com/columnar-tech/dbc/internal"
"github.com/go-faster/yaml"
"github.com/google/uuid"
machineid "github.com/zeroshade/machine-id"
Expand Down Expand Up @@ -108,20 +109,15 @@ func init() {
mid, _ = machineid.ProtectedID()

// get user config dir
userdir, err := os.UserConfigDir()
dbcConfigDir, err := internal.GetDbcConfigDir()
if err != nil {
// if we can't get the dir for some reason, just generate a new UUID
uid = uuid.New()
return
}

// try to read the existing UUID file
dirname := "columnar"
if runtime.GOOS == "windows" || runtime.GOOS == "darwin" {
dirname = "Columnar"
}

fp := filepath.Join(userdir, dirname, "dbc", "uid.uuid")
fp := filepath.Join(dbcConfigDir, "uid.uuid")
data, err := os.ReadFile(fp)
if err == nil {
if err = uid.UnmarshalBinary(data); err == nil {
Expand Down
25 changes: 25 additions & 0 deletions internal/dirs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package internal

import (
"fmt"
"os"
"path/filepath"
"runtime"
)

// Get a platform-specific config dir for reading and writing dbc config files
// and credentails
func GetDbcConfigDir() (string, error) {
userdir, err := os.UserConfigDir()
if err != nil {
return "", fmt.Errorf("failed to get dbc configuration directory: %v", err)
}

orgDirName := "columnar"
if runtime.GOOS == "windows" || runtime.GOOS == "darwin" {
orgDirName = "Columnar"
}
dbcDirName := "dbc"
Comment on lines +13 to +22
Copy link
Member

Choose a reason for hiding this comment

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

but you were the one who said I should use XDG_DATA_HOME if it exists.... lol

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh right. 🤦 This still fixes the original issue that was reported. Let me think about it and I'll tweak this so it uses XDG_DATA_DIR on Linuxes.

Copy link
Member Author

Choose a reason for hiding this comment

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

Which would make this:

  • macOS + Windows: os.UserConfigDir()
  • Linux: check XDG_DATA_DIR, fallback to os.UserConfigDir()

Copy link
Member

Choose a reason for hiding this comment

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

it should be XDG_DATA_HOME, there is no XDG_DATA_DIR there is XDG_DATA_DIRS which is a list of paths

Copy link
Member

Choose a reason for hiding this comment

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

and we should still check XDG_DATA_HOME on macOS too. According to Apple's docs, the equivalent for XDG_DATA_HOME on macOS is ~/Library/

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh, that last bit I didn't know.

Copy link
Member Author

Choose a reason for hiding this comment

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

Maybe my idea was bad, I'll think about it more when I'm back.

Copy link
Member

Choose a reason for hiding this comment

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

yea, that's why it was using the logic I laid out. which was separate from the XDG_CONFIG_HOME (os.UserConfigDir) heh


return filepath.Join(userdir, orgDirName, dbcDirName), nil
}
63 changes: 63 additions & 0 deletions internal/dirs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package internal

import (
"os"
"path/filepath"
"runtime"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestGetDbcConfigDirBasicTest(t *testing.T) {
dir, err := GetDbcConfigDir()
require.NoError(t, err)
assert.NotEmpty(t, dir)
assert.True(t, filepath.IsAbs(dir), "should return absolute path")
}

func TestGetDbcConfigDirCapitalization(t *testing.T) {
dir, err := GetDbcConfigDir()
require.NoError(t, err)
assert.NotEmpty(t, dir)

parent := filepath.Dir(dir)
orgName := filepath.Base(parent)

if runtime.GOOS == "windows" || runtime.GOOS == "darwin" {
assert.Equal(t, "Columnar", orgName)
} else {
assert.Equal(t, "columnar", orgName)
}
}

func TestGetDbcConfigDirDarwin(t *testing.T) {
if runtime.GOOS != "darwin" {
t.Skip("skipping macOS-specific test")
}
dir, err := GetDbcConfigDir()
require.NoError(t, err)
home, _ := os.UserHomeDir()
assert.Equal(t, filepath.Join(home, "Library/Application Support/Columnar/dbc"), dir)
}

func TestGetDbcConfigDirLinux(t *testing.T) {
if runtime.GOOS != "linux" {
t.Skip("skipping Linux-specific test")
}
dir, err := GetDbcConfigDir()
require.NoError(t, err)
home, _ := os.UserHomeDir()
assert.Equal(t, filepath.Join(home, ".config/columnar/dbc"), dir)
}

func TestGetDbcConfigDirWindows(t *testing.T) {
if runtime.GOOS != "windows" {
t.Skip("skipping Windows-specific test")
}
dir, err := GetDbcConfigDir()
require.NoError(t, err)
appData := os.Getenv("AppData")
assert.Equal(t, filepath.Join(appData, "Columnar", "dbc"), dir)
}
Loading