|
2 | 2 |
|
3 | 3 | import logging |
4 | 4 | import pickle |
5 | | -from os import mkdir, path |
| 5 | +import platform |
| 6 | +from os import environ, mkdir, path |
6 | 7 | from typing import List, Tuple, Union |
7 | 8 |
|
8 | | -CONFIG_DIR = path.join(path.expanduser("~"), ".soco-cli") |
| 9 | +if platform.system() == "Linux": |
| 10 | + if "XDG_CONFIG_HOME" in environ: |
| 11 | + CONFIG_DIR = path.join(path.expandvars("${XDG_CONFIG_HOME}"), "soco-cli") |
| 12 | + else: |
| 13 | + CONFIG_DIR = path.join(path.expanduser("~user"), ".config", "soco-cli") |
| 14 | +elif platform.system() == "Windows": |
| 15 | + CONFIG_DIR = path.join(path.expandvars("%LOCALAPPDATA%"), "soco-cli") |
| 16 | +else: |
| 17 | + CONFIG_DIR = path.join(path.expanduser("~user"), ".soco-cli") |
9 | 18 | ALIAS_FILE = path.join(CONFIG_DIR, "aliases.pickle") |
10 | 19 |
|
11 | 20 |
|
@@ -45,13 +54,19 @@ def remove_alias(self, alias_name: str) -> bool: |
45 | 54 | def alias_names(self) -> List[str]: |
46 | 55 | return list(self._aliases.keys()) |
47 | 56 |
|
| 57 | + def _confirm_config_dir() -> bool: |
| 58 | + if not path.exists(CONFIG_DIR): |
| 59 | + logging.info("Creating directory '{}'".format(SOCO_CLI_DIR)) |
| 60 | + try: |
| 61 | + mkdir(CONFIG_DIR) |
| 62 | + return True |
| 63 | + except: |
| 64 | + error_report("Failed to create directory '{}'".format(CONFIG_DIR)) |
| 65 | + return False |
| 66 | + |
48 | 67 | def save_aliases(self) -> None: |
49 | | - if not path.exists(CONFIG_DIR): |
50 | | - try: |
51 | | - logging.info("Creating directory '{}'".format(CONFIG_DIR)) |
52 | | - mkdir(CONFIG_DIR) |
53 | | - except: |
54 | | - pass |
| 68 | + if not _confirm_config_dir(): |
| 69 | + pass |
55 | 70 | with open(ALIAS_FILE, "wb") as f: |
56 | 71 | logging.info("Saving aliases") |
57 | 72 | pickle.dump(self._aliases, f) |
|
0 commit comments