|
45 | 45 | build_remove_content,
|
46 | 46 | build_start,
|
47 | 47 | download_bundle,
|
| 48 | + download_example, |
48 | 49 | emit_build_log,
|
49 | 50 | get_content,
|
| 51 | + list_examples, |
50 | 52 | search_content,
|
51 | 53 | )
|
52 | 54 | from .api import (
|
@@ -3009,6 +3011,88 @@ def system_caches_delete(
|
3009 | 3011 | ce.delete_runtime_cache(language, version, image_name, dry_run)
|
3010 | 3012 |
|
3011 | 3013 |
|
| 3014 | +@cli.group(no_args_is_help=True, help="Fetch Posit Connect jumpstart examples.") |
| 3015 | +def examples(): |
| 3016 | + pass |
| 3017 | + |
| 3018 | + |
| 3019 | +@examples.command( |
| 3020 | + name="list", |
| 3021 | + short_help="List jumpstart examples on a Posit Connect server.", |
| 3022 | +) |
| 3023 | +@server_args |
| 3024 | +@click.pass_context |
| 3025 | +def examples_list( |
| 3026 | + ctx: click.Context, |
| 3027 | + name: str, |
| 3028 | + server: Optional[str], |
| 3029 | + api_key: Optional[str], |
| 3030 | + insecure: bool, |
| 3031 | + cacert: Optional[str], |
| 3032 | + verbose: int, |
| 3033 | +): |
| 3034 | + set_verbosity(verbose) |
| 3035 | + output_params(ctx, locals().items()) |
| 3036 | + with cli_feedback("", stderr=True): |
| 3037 | + ce = RSConnectExecutor(ctx, name, server, api_key, insecure, cacert, logger=None).validate_server() |
| 3038 | + if not isinstance(ce.remote_server, RSConnectServer): |
| 3039 | + raise RSConnectException("rsconnect examples list` requires a Posit Connect server.") |
| 3040 | + examples = list_examples(ce.remote_server) |
| 3041 | + result = [{"name": ex["name"], "description": ex["description"]} for ex in examples] |
| 3042 | + json.dump(result, sys.stdout, indent=2) |
| 3043 | + |
| 3044 | + |
| 3045 | +@examples.command( |
| 3046 | + name="download", |
| 3047 | + short_help="Download a jumpstart example from a Posit Connect server.", |
| 3048 | +) |
| 3049 | +@server_args |
| 3050 | +@click.option( |
| 3051 | + "--example", |
| 3052 | + required=True, |
| 3053 | + help="The name of the example to download.", |
| 3054 | +) |
| 3055 | +@click.option( |
| 3056 | + "--output", |
| 3057 | + "-o", |
| 3058 | + type=click.Path(), |
| 3059 | + required=True, |
| 3060 | + help="Defines the output location for the download.", |
| 3061 | +) |
| 3062 | +@click.option( |
| 3063 | + "--overwrite", |
| 3064 | + is_flag=True, |
| 3065 | + help="Overwrite the output file if it already exists.", |
| 3066 | +) |
| 3067 | +@click.pass_context |
| 3068 | +def examples_download( |
| 3069 | + ctx: click.Context, |
| 3070 | + name: Optional[str], |
| 3071 | + server: Optional[str], |
| 3072 | + api_key: Optional[str], |
| 3073 | + insecure: bool, |
| 3074 | + cacert: Optional[str], |
| 3075 | + example: str, |
| 3076 | + output: str, |
| 3077 | + overwrite: bool, |
| 3078 | + verbose: int, |
| 3079 | +): |
| 3080 | + set_verbosity(verbose) |
| 3081 | + output_params(ctx, locals().items()) |
| 3082 | + with cli_feedback("", stderr=True): |
| 3083 | + ce = RSConnectExecutor(ctx, name, server, api_key, insecure, cacert, logger=None).validate_server() |
| 3084 | + if not isinstance(ce.remote_server, RSConnectServer): |
| 3085 | + raise RSConnectException("`rsconnect examples download` requires a Posit Connect server.") |
| 3086 | + if exists(output) and not overwrite: |
| 3087 | + raise RSConnectException("The output file already exists: %s" % output) |
| 3088 | + |
| 3089 | + result = download_example(ce.remote_server, example) |
| 3090 | + if not isinstance(result.response_body, bytes): |
| 3091 | + raise RSConnectException("The response body must be bytes (not string or None).") |
| 3092 | + with open(output, "wb") as f: |
| 3093 | + f.write(result.response_body) |
| 3094 | + |
| 3095 | + |
3012 | 3096 | if __name__ == "__main__":
|
3013 | 3097 | cli()
|
3014 | 3098 | click.echo()
|
|
0 commit comments