Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add multiple valid config file locations #54

Open
pederhan opened this issue Mar 16, 2023 · 0 comments
Open

Add multiple valid config file locations #54

pederhan opened this issue Mar 16, 2023 · 0 comments

Comments

@pederhan
Copy link
Member

pederhan commented Mar 16, 2023

The application should look for the config file in multiple different locations, such as:

  • <platformdirs directory>/config.toml (current)
  • ./config.toml
  • /etc/harbor_cli/config.toml
  • /usr/local/etc/harbor_cli/config.toml

We'll have to decide what the priority should be. Configs in user directories (default appdir location, $(pwd) and /usr/local/etc) should definitely take priority over site config dirs (/etc).

Possible implementation

# dirs.py
CONFIG_DIR = Path(_PLATFORM_DIR.user_config_dir)
SITE_CONFIG_DIR = Path(_PLATFORM_DIR.site_config_dir)

# config.py

CONFIG_FILENAME = "config.toml"
DEFAULT_CONFIG_FILE = CONFIG_DIR / "config.toml"

class HarborCLIConfig(BaseModel):
    # ...

    @classmethod
    def from_file(
        cls, config_file: Path | None = None, create: bool = False
    ) -> HarborCLIConfig:
        if config_file is None:
            config_file = find_config_file()
   # ...


def find_config_file() -> Path:
    """Attempts to find a config file. Falls back on the default config file location if a config file
    cannot be found in any of the expected locations."""
    for d in [Path(""), CONFIG_DIR, SITE_CONFIG_DIR]:
        p = d / CONFIG_FILENAME
        if p.exists():
            return p
    return DEFAULT_CONFIG_FILE # fall back on default (may not exist yet)
@pederhan pederhan added this to Harbor Mar 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant