Skip to content

Commit

Permalink
Merge pull request #86 from Cronocide/main
Browse files Browse the repository at this point in the history
Added support for configuration from ENV
  • Loading branch information
glomatico authored Mar 21, 2024
2 parents 56f01dc + 6551722 commit e76803a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ A Python script to download Apple Music songs/music videos/albums/playlists. Thi
```

## Configuration
You can configure gamdl by using the command line arguments or the config file. The config file is created automatically when you run gamdl for the first time at `~/.gamdl/config.json` on Linux and `%USERPROFILE%\.gamdl\config.json` on Windows. Config file values can be overridden using command line arguments.
You can configure gamdl by using the command line arguments, environment variables, or the config file. The config file is created automatically when you run gamdl for the first time at `~/.gamdl/config.json` on Linux and `%USERPROFILE%\.gamdl\config.json` on Windows. Environment variables are prefixed with `GAMDL_` and are the capitalized config file key for their respective option. Config file values and environment variables can be overridden using command line arguments.
| Command line argument / Config file key | Description | Default value |
| --------------------------------------------------------------- | ---------------------------------------------------------------------- | ---------------------------------- |
| `-f`, `--final-path` / `final_path` | Path where the downloaded files will be saved. | `./Apple Music` |
Expand Down
19 changes: 12 additions & 7 deletions gamdl/cli.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from __future__ import annotations

import os
import json
import logging
from pathlib import Path

import click

from . import __version__
from .constants import *
from .downloader import Downloader
Expand All @@ -26,18 +26,23 @@ def no_config_callback(
ctx: click.Context, param: click.Parameter, no_config_file: bool
) -> click.Context:
if no_config_file:
for param in ctx.command.params:
env_param = "GAMDL_" + param.name.upper().replace("-", "_")
if os.getenv(env_param) is not None:
ctx.params[param.name] = param.type_cast_value(ctx, os.getenv(env_param))
return ctx
if not ctx.params["config_location"].exists():
write_default_config_file(ctx)
with open(ctx.params["config_location"], "r") as f:
config_file = dict(json.load(f))
for param in ctx.command.params:
if (
config_file.get(param.name) is not None
and not ctx.get_parameter_source(param.name)
== click.core.ParameterSource.COMMANDLINE
):
ctx.params[param.name] = param.type_cast_value(ctx, config_file[param.name])
if (not ctx.get_parameter_source(param.name)
== click.core.ParameterSource.COMMANDLINE):
if config_file.get(param.name) is not None:
ctx.params[param.name] = param.type_cast_value(ctx, config_file[param.name])
env_param = "GAMDL_" + param.name.upper().replace("-", "_")
if os.getenv(env_param) is not None:
ctx.params[param.name] = param.type_cast_value(ctx, os.getenv(env_param))
return ctx


Expand Down

0 comments on commit e76803a

Please sign in to comment.