Skip to content

Commit

Permalink
feat(configtree): support yaml format while exporting trees
Browse files Browse the repository at this point in the history
The rio configtree export command only supports exporting keys of a tree
in json format. This commit allows choosing between yaml or json and
exposes it as a command line flag.
  • Loading branch information
pallabpain committed Aug 6, 2024
1 parent e8eda8a commit 25efc43
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions riocli/configtree/export_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from pathlib import Path
from typing import Optional

import click
from benedict import benedict
from click_help_colors import HelpColorsCommand
from yaspin.core import Yaspin

from riocli.config import new_v2_client
from riocli.configtree.util import combine_metadata, export_to_files, unflatten_keys
from riocli.configtree.util import export_to_files, unflatten_keys
from riocli.constants import Symbols, Colors
from riocli.utils.spinner import with_spinner

Expand All @@ -34,6 +34,8 @@
default=False, help='Operate on organization-scoped Config Trees only.')
@click.option('--export-directory', 'export_directory', type=str,
help='Path to the directory for exporting files.')
@click.option('--format', '-f', 'file_format', type=click.Choice(['json', 'yaml']),
default='json', help='Format of the exported files.')
@click.argument('tree-name', type=str)
@click.argument('rev-id', type=str, required=False)
@click.pass_context
Expand All @@ -44,15 +46,15 @@ def export_keys(
rev_id: Optional[str],
with_org: bool,
export_directory: Optional[str],
file_format: Optional[str],
spinner: Yaspin,
) -> None:
"""
Export keys of the Config tree to files.
"""

"""Export keys of the Config tree to files."""
if export_directory is None:
export_directory = '.'

export_directory = Path(export_directory).absolute()

try:
client = new_v2_client(with_project=(not with_org))
tree = client.get_config_tree(tree_name=tree_name, rev_id=rev_id, include_data=True)
Expand All @@ -61,14 +63,16 @@ def export_keys(

keys = tree.get('keys')
if not isinstance(keys, dict):
raise Exception('Keys are not dictionary')
raise Exception('Keys are not a dictionary')

data = unflatten_keys(keys)

export_to_files(base_dir=export_directory, data=data, file_format='json')
export_to_files(base_dir=export_directory, data=data, file_format=file_format)

spinner.text = click.style(f'Keys exported to {export_directory}', fg=Colors.GREEN)
spinner.ok(Symbols.SUCCESS)
except Exception as e:
spinner.red.text = str(e)
spinner.text = click.style(f'Failed to export keys: {e}', fg=Colors.RED)
spinner.red.fail(Symbols.ERROR)
raise SystemExit(1) from e

Expand Down

0 comments on commit 25efc43

Please sign in to comment.