From 8aaeacfa20c499d6dce32cb8e4257168fea06d84 Mon Sep 17 00:00:00 2001 From: A1lo Date: Tue, 16 Jun 2026 09:42:17 +0800 Subject: [PATCH] chore: replace userdir with stdlib os.UserConfigDir --- backend/services/connection_service.go | 6 +++--- backend/services/connection_service_web.go | 5 ++--- backend/storage/local_storage.go | 5 ++--- backend/utils/confdir/dir.go | 20 ++++++++++++++++++++ backend/utils/convert/common.go | 5 ++--- go.mod | 1 - go.sum | 2 -- 7 files changed, 29 insertions(+), 15 deletions(-) create mode 100644 backend/utils/confdir/dir.go diff --git a/backend/services/connection_service.go b/backend/services/connection_service.go index f889114a..5e40c46d 100644 --- a/backend/services/connection_service.go +++ b/backend/services/connection_service.go @@ -17,11 +17,11 @@ import ( "tinyrdm/backend/consts" . "tinyrdm/backend/storage" "tinyrdm/backend/types" + "tinyrdm/backend/utils/confdir" _ "tinyrdm/backend/utils/proxy" "github.com/klauspost/compress/zip" "github.com/redis/go-redis/v9" - "github.com/vrischmann/userdir" sshagent "github.com/xanzy/ssh-agent" "golang.org/x/crypto/ssh" "golang.org/x/net/proxy" @@ -526,7 +526,7 @@ func (c *connectionService) ExportConnections() (resp types.JSResp) { // compress the connections profile with zip const connectionFilename = "connections.yaml" - inputFile, err := os.Open(path.Join(userdir.GetConfigHome(), consts.APP_DATA_FOLDER, connectionFilename)) + inputFile, err := os.Open(path.Join(confdir.GetConfigDir(), consts.APP_DATA_FOLDER, connectionFilename)) if err != nil { resp.Msg = err.Error() return @@ -602,7 +602,7 @@ func (c *connectionService) ImportConnections() (resp types.JSResp) { } defer zippedFile.Close() - outputFile, err := os.Create(path.Join(userdir.GetConfigHome(), consts.APP_DATA_FOLDER, connectionFilename)) + outputFile, err := os.Create(path.Join(confdir.GetConfigDir(), consts.APP_DATA_FOLDER, connectionFilename)) if err != nil { resp.Msg = err.Error() return diff --git a/backend/services/connection_service_web.go b/backend/services/connection_service_web.go index d486ce42..16d84138 100644 --- a/backend/services/connection_service_web.go +++ b/backend/services/connection_service_web.go @@ -12,7 +12,6 @@ import ( "tinyrdm/backend/types" "github.com/klauspost/compress/zip" - "github.com/vrischmann/userdir" ) // ExportConnectionsToBytes exports connections as zip bytes for web download @@ -20,7 +19,7 @@ func (c *connectionService) ExportConnectionsToBytes() ([]byte, string, error) { const connectionFilename = "connections.yaml" filename := "connections_" + time.Now().Format("20060102150405") + ".zip" - inputFile, err := os.Open(path.Join(userdir.GetConfigHome(), consts.APP_DATA_FOLDER, connectionFilename)) + inputFile, err := os.Open(path.Join(confdir.GetConfigDir(), consts.APP_DATA_FOLDER, connectionFilename)) if err != nil { return nil, "", err } @@ -78,7 +77,7 @@ func (c *connectionService) ImportConnectionsFromBytes(data []byte) (resp types. } defer zippedFile.Close() - outputFile, err := os.Create(path.Join(userdir.GetConfigHome(), consts.APP_DATA_FOLDER, connectionFilename)) + outputFile, err := os.Create(path.Join(confdir.GetConfigDir(), consts.APP_DATA_FOLDER, connectionFilename)) if err != nil { resp.Msg = "failed to save connections" return diff --git a/backend/storage/local_storage.go b/backend/storage/local_storage.go index 7286385c..4d92f837 100644 --- a/backend/storage/local_storage.go +++ b/backend/storage/local_storage.go @@ -5,8 +5,7 @@ import ( "path" "tinyrdm/backend/consts" - - "github.com/vrischmann/userdir" + "tinyrdm/backend/utils/confdir" ) // localStorage provides reading and writing application data to the user's @@ -18,7 +17,7 @@ type localStorage struct { // NewLocalStore returns a localStore instance. func NewLocalStore(filename string) *localStorage { return &localStorage{ - ConfPath: path.Join(userdir.GetConfigHome(), consts.APP_DATA_FOLDER, filename), + ConfPath: path.Join(confdir.GetConfigDir(), consts.APP_DATA_FOLDER, filename), } } diff --git a/backend/utils/confdir/dir.go b/backend/utils/confdir/dir.go new file mode 100644 index 00000000..ede21c14 --- /dev/null +++ b/backend/utils/confdir/dir.go @@ -0,0 +1,20 @@ +package confdir + +import ( + "log" + "os" + "sync" +) + +var confdirOnceValue = sync.OnceValue(func() string { + dir, err := os.UserConfigDir() + if err != nil { + log.Printf("get user config dir failed: %v, fallback to home dir", err) + dir, _ = os.UserHomeDir() + } + return dir +}) + +func GetConfigDir() string { + return confdirOnceValue() +} diff --git a/backend/utils/convert/common.go b/backend/utils/convert/common.go index b93bdfef..0232981a 100644 --- a/backend/utils/convert/common.go +++ b/backend/utils/convert/common.go @@ -4,12 +4,11 @@ import ( "os" "path" "tinyrdm/backend/consts" - - "github.com/vrischmann/userdir" + "tinyrdm/backend/utils/confdir" ) func writeExecuteFile(content []byte, filename string) (string, error) { - filepath := path.Join(userdir.GetConfigHome(), consts.APP_DATA_FOLDER, "decoder", filename) + filepath := path.Join(confdir.GetConfigDir(), consts.APP_DATA_FOLDER, "decoder", filename) _ = os.Mkdir(path.Dir(filepath), 0777) err := os.WriteFile(filepath, content, 0777) if err != nil { diff --git a/go.mod b/go.mod index 0dd127f3..ebb77168 100644 --- a/go.mod +++ b/go.mod @@ -12,7 +12,6 @@ require ( github.com/pierrec/lz4/v4 v4.1.27 github.com/redis/go-redis/v9 v9.20.0 github.com/vmihailenco/msgpack/v5 v5.4.1 - github.com/vrischmann/userdir v0.0.0-20151206171402-20f291cebd68 github.com/wailsapp/wails/v2 v2.12.0 github.com/xanzy/ssh-agent v0.3.3 golang.org/x/crypto v0.53.0 diff --git a/go.sum b/go.sum index cc96cce3..0a346538 100644 --- a/go.sum +++ b/go.sum @@ -152,8 +152,6 @@ github.com/vmihailenco/msgpack/v5 v5.4.1 h1:cQriyiUvjTwOHg8QZaPihLWeRAAVoCpE00IU github.com/vmihailenco/msgpack/v5 v5.4.1/go.mod h1:GaZTsDaehaPpQVyxrf5mtQlH+pc21PIudVV/E3rRQok= github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g= github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds= -github.com/vrischmann/userdir v0.0.0-20151206171402-20f291cebd68 h1:Ah2/69Z24rwD6OByyOdpJDmttftz0FTF8Q4QZ/SF1E4= -github.com/vrischmann/userdir v0.0.0-20151206171402-20f291cebd68/go.mod h1:EqKqAeKddSL9XSGnfXd/7iLncccKhR16HBKVva7ENw8= github.com/wailsapp/go-webview2 v1.0.23 h1:jmv8qhz1lHibCc79bMM/a/FqOnnzOGEisLav+a0b9P0= github.com/wailsapp/go-webview2 v1.0.23/go.mod h1:qJmWAmAmaniuKGZPWwne+uor3AHMB5PFhqiK0Bbj8kc= github.com/wailsapp/mimetype v1.4.1 h1:pQN9ycO7uo4vsUUuPeHEYoUkLVkaRntMnHJxVwYhwHs=