Skip to content

Commit

Permalink
feat(configtree): supports overrides while import config from files
Browse files Browse the repository at this point in the history
This commit adds the support for config overrides while importing
configurations from files. Users can specify one or more override
files and they will be applied to the base configs in order. It is
expected that the overrides have the full key hierarchy such that
the command is able to match the right override with the base config.

Wrike Ticket:
  • Loading branch information
pallabpain committed Aug 5, 2024
1 parent 7943a4a commit 692aadd
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions riocli/configtree/import_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from pathlib import Path
from typing import Optional, Iterable
from typing import Iterable, Optional

import click
from benedict import benedict
Expand All @@ -23,7 +23,7 @@
from riocli.configtree.etcd import import_in_etcd
from riocli.configtree.revision import Revision
from riocli.configtree.util import Metadata, export_to_files
from riocli.constants import Symbols, Colors
from riocli.constants import Colors, Symbols
from riocli.utils.spinner import with_spinner


Expand All @@ -36,7 +36,7 @@
@click.option('--commit/--no-commit', 'commit', is_flag=True, type=bool, )
@click.option('--update-head/--no-update-head', 'update_head', is_flag=True, type=bool)
@click.option('--milestone', 'milestone', type=str,
help='Minestone name for the imported revision.')
help='Milestone name for the imported revision.')
@click.option('--etcd-endpoint', 'etcd_endpoint', type=str,
help='Import keys to local etcd instead of rapyuta.io cloud')
@click.option('--export-directory', 'export_directory', type=str,
Expand All @@ -47,6 +47,8 @@
help='Prefix to use for the key-space')
@click.option('--organization', 'with_org', is_flag=True, type=bool,
default=False, help='Operate on organization-scoped Config Trees only.')
@click.option('--override', 'overrides', type=click.Path(exists=True), default=None,
multiple=True, help='Override values for keys in the imported files.')
@click.argument('tree-name', type=str)
@click.argument('files', type=str, nargs=-1)
@click.pass_context
Expand All @@ -62,13 +64,13 @@ def import_keys(
etcd_endpoint: Optional[str],
etcd_port: Optional[int],
etcd_prefix: Optional[str],
overrides: Optional[Iterable[click.Path]],
with_org: bool,
spinner: Yaspin,
) -> None:
"""
Imports keys in a Config tree from YAML files.
"""

data = {}

for f in files:
Expand All @@ -85,6 +87,23 @@ def import_keys(
)
)

for f in overrides:
file_format = 'yaml'
if f.endswith('json'):
file_format = 'json'

o = benedict(f, format=file_format)

for file_prefix in data:
data[file_prefix].merge(o[file_prefix], overwrite=True)

spinner.write(
click.style(
'{} Overrides from file {} applied.'.format(Symbols.SUCCESS, f),
fg=Colors.CYAN,
)
)

data, metadata = split_metadata(data)

if export_directory is not None:
Expand Down

0 comments on commit 692aadd

Please sign in to comment.