|
| 1 | +from typing import List |
| 2 | + |
| 3 | +import typer |
| 4 | +from rich import print_json, print |
| 5 | +from rich.panel import Panel |
| 6 | +from rich.columns import Columns |
| 7 | +from rich.json import JSON |
| 8 | + |
| 9 | +from nettowel.cli._common import ( |
| 10 | + auto_complete_paths, |
| 11 | + read_yaml, |
| 12 | + get_typer_app, |
| 13 | +) |
| 14 | +from nettowel.exceptions import NettowelInputError |
| 15 | + |
| 16 | +from nettowel import jsonpatch |
| 17 | + |
| 18 | + |
| 19 | +app = get_typer_app(help="JSON Patch [RFC 6902](http://tools.ietf.org/html/rfc6902)") |
| 20 | + |
| 21 | + |
| 22 | +@app.command() |
| 23 | +def create( |
| 24 | + ctx: typer.Context, |
| 25 | + src_file_name: typer.FileText = typer.Argument( |
| 26 | + ..., |
| 27 | + exists=True, |
| 28 | + file_okay=True, |
| 29 | + dir_okay=False, |
| 30 | + readable=True, |
| 31 | + resolve_path=True, |
| 32 | + allow_dash=True, |
| 33 | + metavar="src", |
| 34 | + help="Source data (YAML/JSON)", |
| 35 | + autocompletion=auto_complete_paths, |
| 36 | + ), |
| 37 | + dst_file_name: typer.FileText = typer.Argument( |
| 38 | + ..., |
| 39 | + exists=True, |
| 40 | + file_okay=True, |
| 41 | + dir_okay=False, |
| 42 | + readable=True, |
| 43 | + resolve_path=True, |
| 44 | + allow_dash=True, |
| 45 | + metavar="dst", |
| 46 | + help="Destination data (YAML/JSON)", |
| 47 | + autocompletion=auto_complete_paths, |
| 48 | + ), |
| 49 | + json: bool = typer.Option(False, "--json", help="JSON output"), |
| 50 | + raw: bool = typer.Option(False, "--raw", help="Raw result output"), |
| 51 | + only_result: bool = typer.Option( |
| 52 | + False, "--print-result-only", help="Only print the result" |
| 53 | + ), |
| 54 | +) -> None: |
| 55 | + try: |
| 56 | + src = read_yaml(src_file_name) |
| 57 | + dst = read_yaml(dst_file_name) |
| 58 | + |
| 59 | + patch = jsonpatch.create(src=src, dst=dst) |
| 60 | + patch_str = patch.to_string() |
| 61 | + if json or raw: |
| 62 | + print_json(json=patch_str) |
| 63 | + else: |
| 64 | + panels: List[Panel] = [] |
| 65 | + if not only_result: |
| 66 | + panels.append( |
| 67 | + Panel( |
| 68 | + JSON.from_data(src), title="[yellow]Source", border_style="blue" |
| 69 | + ) |
| 70 | + ) |
| 71 | + panels.append( |
| 72 | + Panel( |
| 73 | + JSON.from_data(dst), |
| 74 | + title="[yellow]Destrination", |
| 75 | + border_style="blue", |
| 76 | + ) |
| 77 | + ) |
| 78 | + panels.append( |
| 79 | + Panel(JSON(patch_str), title="[yellow]JSON Patch", border_style="blue") |
| 80 | + ) |
| 81 | + print(Columns(panels, equal=True)) |
| 82 | + raise typer.Exit(0) |
| 83 | + except NettowelInputError as exc: |
| 84 | + typer.echo("Input is not valide", err=True) |
| 85 | + typer.echo(str(exc), err=True) |
| 86 | + raise typer.Exit(3) |
| 87 | + |
| 88 | + |
| 89 | +@app.command() |
| 90 | +def apply( |
| 91 | + ctx: typer.Context, |
| 92 | + patch_file_name: typer.FileText = typer.Argument( |
| 93 | + ..., |
| 94 | + exists=True, |
| 95 | + file_okay=True, |
| 96 | + dir_okay=False, |
| 97 | + readable=True, |
| 98 | + resolve_path=True, |
| 99 | + allow_dash=True, |
| 100 | + metavar="patch", |
| 101 | + help="Patch opterations (list of mappings) (YAML/JSON)", |
| 102 | + autocompletion=auto_complete_paths, |
| 103 | + ), |
| 104 | + data_file_name: typer.FileText = typer.Argument( |
| 105 | + ..., |
| 106 | + exists=True, |
| 107 | + file_okay=True, |
| 108 | + dir_okay=False, |
| 109 | + readable=True, |
| 110 | + resolve_path=True, |
| 111 | + allow_dash=True, |
| 112 | + metavar="data", |
| 113 | + help="Data to patch (YAML/JSON)", |
| 114 | + autocompletion=auto_complete_paths, |
| 115 | + ), |
| 116 | + json: bool = typer.Option(False, "--json", help="JSON output"), |
| 117 | + raw: bool = typer.Option(False, "--raw", help="Raw result output"), |
| 118 | + only_result: bool = typer.Option( |
| 119 | + False, "--print-result-only", help="Only print the result" |
| 120 | + ), |
| 121 | +) -> None: |
| 122 | + try: |
| 123 | + patch_data = read_yaml(patch_file_name) |
| 124 | + data_input = read_yaml(data_file_name) |
| 125 | + |
| 126 | + new_data = jsonpatch.apply(patch=patch_data, data=data_input) |
| 127 | + |
| 128 | + if json or raw: |
| 129 | + print_json(data=new_data) |
| 130 | + else: |
| 131 | + panels: List[Panel] = [] |
| 132 | + if not only_result: |
| 133 | + panels.append( |
| 134 | + Panel( |
| 135 | + JSON.from_data(patch_data), |
| 136 | + title="[yellow]Patch", |
| 137 | + border_style="blue", |
| 138 | + ) |
| 139 | + ) |
| 140 | + panels.append( |
| 141 | + Panel( |
| 142 | + JSON.from_data(data_input), |
| 143 | + title="[yellow]Input Data", |
| 144 | + border_style="blue", |
| 145 | + ) |
| 146 | + ) |
| 147 | + panels.append( |
| 148 | + Panel( |
| 149 | + JSON.from_data(data=new_data), |
| 150 | + title="[yellow]Patched Data with JSON Patch", |
| 151 | + border_style="blue", |
| 152 | + ) |
| 153 | + ) |
| 154 | + print(Columns(panels, equal=True)) |
| 155 | + raise typer.Exit(0) |
| 156 | + except NettowelInputError as exc: |
| 157 | + typer.echo("Input is not valide", err=True) |
| 158 | + typer.echo(str(exc), err=True) |
| 159 | + raise typer.Exit(3) |
| 160 | + |
| 161 | + |
| 162 | +if __name__ == "__main__": |
| 163 | + app() |
0 commit comments