From 56c1812d2acbe1783eff968f4124ca866191c8aa Mon Sep 17 00:00:00 2001
From: David Huss <dh@atoav.com>
Date: Sun, 31 Oct 2021 15:17:17 +0100
Subject: [PATCH] Try priviledge escalation

---
 common_config/__init__.py |  2 +-
 common_config/common.py   | 13 +++++++++----
 pyproject.toml            |  2 +-
 3 files changed, 11 insertions(+), 6 deletions(-)

diff --git a/common_config/__init__.py b/common_config/__init__.py
index f8fb29d..4e0b094 100644
--- a/common_config/__init__.py
+++ b/common_config/__init__.py
@@ -1,2 +1,2 @@
-__version__ = '0.1.4'
+__version__ = '0.1.6'
 from common_config.common import *
\ No newline at end of file
diff --git a/common_config/common.py b/common_config/common.py
index 27ea294..dc1ae1f 100644
--- a/common_config/common.py
+++ b/common_config/common.py
@@ -385,12 +385,13 @@ def create_config():
     try:
         selection["path"].mkdir(mode=0o755, parents=True, exist_ok=True)
     except PermissionError:
+        python_path = f"{sys.prefix}/bin/python3"
         print()
-        print("Error: Didn't have the permissions to create the config directory at {}".format(selection["path"]), file=sys.stderr)
-        print("Hint:  Change the owner of the directory temporarily to {} or run {} config create with more permissions".format(getpass.getuser(), APPLICATION_NAME))
+        print(f"Error: Didn't have the permissions to create the config directory at {selection['path']}", file=sys.stderr)
+        print(f"Hint:  Change the owner of the directory temporarily to {getpass.getuser()} or run {APPLICATION_NAME} config create with more permissions", file=sys.stderr)
+        print(f"Hint:  The python executable used by this script is located at \"{python_path}\"", file=sys.stderr)
         
         # Run again with priviledges
-        python_path = f"{sys.prefix}/bin/python3"
         subprocess.call(['sudo', python_path, *sys.argv])
         sys.exit()
 
@@ -423,12 +424,16 @@ def create_config():
     try:
         config_path.write_text(DEFAULT_CONFIG)
     except PermissionError as e:
+        python_path = f"{sys.prefix}/bin/python3"
         print()
         print(f"Error: Didn't have the permissions to write the file to {config_path}", file=sys.stderr)
         print(f"       Directory \"{selection['path']}\" belongs to user {selection['path'].owner()} (was running as user {getpass.getuser()})", file=sys.stderr)
         print()
         print(f"Hint:  Change the owner of the directory temporarily to {getpass.getuser()} or run {APPLICATION_NAME} config create with more permissions")
-        exit()
+        print(f"Hint:  The python executable used by this script is located at \"{python_path}\"", file=sys.stderr)
+        # Run again with priviledges
+        subprocess.call(['sudo', python_path, *sys.argv])
+        sys.exit()
     print(f"Default Config has been written to {config_path}")
 
 
diff --git a/pyproject.toml b/pyproject.toml
index b0f5269..626276e 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -1,6 +1,6 @@
 [tool.poetry]
 name = "common-config"
-version = "0.1.4"
+version = "0.1.6"
 description = "A config library for eigenservice"
 authors = ["David Huss <dh@atoav.com>"]
 
-- 
GitLab