From 32a39438212b3285a6ff84e3dddfe47dc6232365 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Roth?= Date: Fri, 2 Aug 2024 17:48:57 +0200 Subject: [PATCH] support ~ in rootDir as home directory --- cmd/api_serve.go | 2 +- cmd/serve.go | 2 +- context/context.go | 6 +++--- utils/config.go | 6 ++++++ 4 files changed, 11 insertions(+), 5 deletions(-) diff --git a/cmd/api_serve.go b/cmd/api_serve.go index 9b6d3402..22af04a8 100644 --- a/cmd/api_serve.go +++ b/cmd/api_serve.go @@ -34,7 +34,7 @@ func aptlyAPIServe(cmd *commander.Command, args []string) error { // anything else must fail. // E.g.: Running the service under a different user may lead to a rootDir // that exists but is not usable due to access permissions. - err = utils.DirIsAccessible(context.Config().RootDir) + err = utils.DirIsAccessible(context.Config().GetRootDir()) if err != nil { return err } diff --git a/cmd/serve.go b/cmd/serve.go index 9efaa772..be974b00 100644 --- a/cmd/serve.go +++ b/cmd/serve.go @@ -29,7 +29,7 @@ func aptlyServe(cmd *commander.Command, args []string) error { // anything else must fail. // E.g.: Running the service under a different user may lead to a rootDir // that exists but is not usable due to access permissions. - err = utils.DirIsAccessible(context.Config().RootDir) + err = utils.DirIsAccessible(context.Config().GetRootDir()) if err != nil { return err } diff --git a/context/context.go b/context/context.go index 7cc7c6ef..569b1cec 100644 --- a/context/context.go +++ b/context/context.go @@ -275,7 +275,7 @@ func (context *AptlyContext) DBPath() string { // DBPath builds path to database func (context *AptlyContext) dbPath() string { - return filepath.Join(context.config().RootDir, "db") + return filepath.Join(context.config().GetRootDir(), "db") } // Database opens and returns current instance of database @@ -406,7 +406,7 @@ func (context *AptlyContext) GetPublishedStorage(name string) aptly.PublishedSto publishedStorage, ok := context.publishedStorages[name] if !ok { if name == "" { - publishedStorage = files.NewPublishedStorage(filepath.Join(context.config().RootDir, "public"), "hardlink", "") + publishedStorage = files.NewPublishedStorage(filepath.Join(context.config().GetRootDir(), "public"), "hardlink", "") } else if strings.HasPrefix(name, "filesystem:") { params, ok := context.config().FileSystemPublishRoots[name[11:]] if !ok { @@ -464,7 +464,7 @@ func (context *AptlyContext) GetPublishedStorage(name string) aptly.PublishedSto // UploadPath builds path to upload storage func (context *AptlyContext) UploadPath() string { - return filepath.Join(context.Config().RootDir, "upload") + return filepath.Join(context.Config().GetRootDir(), "upload") } func (context *AptlyContext) pgpProvider() string { diff --git a/utils/config.go b/utils/config.go index e7f8c22f..fbe72d7d 100644 --- a/utils/config.go +++ b/utils/config.go @@ -5,6 +5,7 @@ import ( "fmt" "os" "path/filepath" + "strings" ) // ConfigStructure is structure of main configuration @@ -209,3 +210,8 @@ func SaveConfig(filename string, config *ConfigStructure) error { _, err = f.Write(encoded) return err } + +// GetRootDir returns the RootDir with expanded ~ as home directory +func (conf *ConfigStructure) GetRootDir() string { + return strings.Replace(conf.RootDir, "~", os.Getenv("HOME"), 1) +}