From 7b3bc2bd1b5c989321d760fdef8c3690d0730484 Mon Sep 17 00:00:00 2001
From: Lasse Tjernaes Wardenaer <lasse.tjernaes.wardenaer@cern.ch>
Date: Tue, 15 Nov 2022 15:22:56 +0100
Subject: [PATCH] Resolve "Use default config file if $HOME is not defined"

---
 ReleaseNotes.md         |  2 ++
 cmdline/CtaAdminCmd.cpp | 23 ++++++++++++-----------
 2 files changed, 14 insertions(+), 11 deletions(-)

diff --git a/ReleaseNotes.md b/ReleaseNotes.md
index 684b3f9e08..032e1521e9 100644
--- a/ReleaseNotes.md
+++ b/ReleaseNotes.md
@@ -4,6 +4,8 @@
 - cta/CTA#133 - Change severity from ERROR to INFO for "reported a batch of retrieve jobs" log message in Scheduler.cpp
 - cta/CTA#201 - Improve error message when oracle configured without oracle support
 - cta/CTA#203 - Refactor cta-restore-deletes-files by using the connection configuration class in standalone_cli_tools/common
+### Bug fixes
+- cta/CTA#209 - handle if $HOME is not defined when choosing config file for cta-admin
 
 # v4.7.13-1
 ## Summary
diff --git a/cmdline/CtaAdminCmd.cpp b/cmdline/CtaAdminCmd.cpp
index 0c01ca0c19..17bc5dd79b 100644
--- a/cmdline/CtaAdminCmd.cpp
+++ b/cmdline/CtaAdminCmd.cpp
@@ -165,10 +165,10 @@ CtaAdminCmd::CtaAdminCmd(int argc, const char *const *const argv) :
    m_execname(argv[0])
 {
    auto &admincmd = *(m_request.mutable_admincmd());
-   
+
    m_request.set_client_cta_version(CTA_VERSION);
    m_request.set_client_xrootd_ssi_protobuf_interface_version(XROOTD_SSI_PROTOBUF_INTERFACE_VERSION);
-   
+
    // Strip path from execname
 
    size_t p = m_execname.find_last_of('/');
@@ -197,7 +197,7 @@ CtaAdminCmd::CtaAdminCmd(int argc, const char *const *const argv) :
    }
 
    // Help is a special subcommand which suppresses errors and prints usage
-   
+
    if(argc > argno && std::string(argv[argno]) == "help") {
       throwUsage();
    }
@@ -231,16 +231,17 @@ CtaAdminCmd::CtaAdminCmd(int argc, const char *const *const argv) :
 }
 
 const std::string CtaAdminCmd::getConfigFilePath() const {
-  std::filesystem::path config_file = DEFAULT_CLI_CONFIG;
-   
-  const std::filesystem::path home = std::getenv("HOME");
-  const std::filesystem::path home_dir_config_file = home / ".cta/cta-cli.conf";
-  if(std::filesystem::exists(home_dir_config_file)) { config_file = home_dir_config_file; }
+   std::filesystem::path config_file = DEFAULT_CLI_CONFIG;
 
-  if(m_config) {
+   if(std::getenv("HOME")) {
+      const std::filesystem::path home = std::getenv("HOME");
+      const std::filesystem::path home_dir_config_file = home / ".cta/cta-cli.conf";
+      if(std::filesystem::exists(home_dir_config_file)) { config_file = home_dir_config_file; }
+   }
+   if(m_config) {
     config_file = m_config.value();
-  }
-  return config_file;
+   }
+   return config_file;
 }
 
 void CtaAdminCmd::send() const
-- 
GitLab