Skip to content

Commit

Permalink
refactor: rename cli to jina_cli (#4890)
Browse files Browse the repository at this point in the history
* chore: fix readme

* chore: fix readme

* chore: fix dockerignore

* fix: #4845

* style: fix overload and cli autocomplete

* fix: cicd export cli

Co-authored-by: Jina Dev Bot <dev-bot@jina.ai>
  • Loading branch information
hanxiao and jina-bot authored Jun 7, 2022
1 parent d9d2dd3 commit 16b16b0
Show file tree
Hide file tree
Showing 24 changed files with 276 additions and 105 deletions.
2 changes: 1 addition & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
!*requirements.txt
!README.md
!MANIFEST.in
!cli/**
!jina_cli/**
!daemon/**
!docarray/**
!fastentrypoints.py
2 changes: 1 addition & 1 deletion .github/labeler.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ area/docker:
- ./.dockerignore

area/cli:
- cli/**/*
- jina_cli/**/*

area/docarray:
- docarray/**/*
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ jobs:
echo "JINA_VERSION=${JINA_VERSION}" >> $GITHUB_ENV
cd schema
mkdir -p schemas
jina export-api --schema-path schemas/"$JINA_VERSION.json" schemas/master.json schemas/master --yaml-path "$JINA_VERSION.yml" master.yml --json-path "$JINA_VERSION.json" master.json master
jina export schema --schema-path schemas/"$JINA_VERSION.json" schemas/master.json schemas/master --yaml-path "$JINA_VERSION.yml" master.yml --json-path "$JINA_VERSION.json" master.json master
python ../scripts/get-openapi-schemas.py
npm install --prefix ~ snippet-enricher-cli
~/node_modules/.bin/snippet-enricher-cli --input=gateway.json --targets=shell_curl > gateway-with-code.json
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/tag.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jobs:
pip install . --no-cache-dir
cd schema
mkdir -p schemas
jina export-api --schema-path schemas/"$JINA_VERSION.json" schemas/latest.json schemas/latest --yaml-path "$JINA_VERSION.yml" latest.yml --json-path "$JINA_VERSION.json" latest.json latest
jina export schema --schema-path schemas/"$JINA_VERSION.json" schemas/latest.json schemas/latest --yaml-path "$JINA_VERSION.yml" latest.yml --json-path "$JINA_VERSION.json" latest.json latest
python ../scripts/get-openapi-schemas.py
npm install --prefix ~ snippet-enricher-cli
~/node_modules/.bin/snippet-enricher-cli --input=gateway.json --targets=shell_curl > gateway-with-code.json
Expand Down
2 changes: 1 addition & 1 deletion jina/__main__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
if __name__ == '__main__':
from cli import main
from jina_cli import main

main()
68 changes: 68 additions & 0 deletions jina/exporter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import json

from jina import Flow, __version__
from jina.jaml import JAML
from jina.logging.predefined import default_logger
from jina.schemas import get_full_schema
from jina_cli.export import api_to_dict


def export_kubernetes(args):
"""Export to k8s yaml files
:param args: args from CLI
"""
Flow.load_config(args.flowpath).to_kubernetes_yaml(
output_base_path=args.outpath, k8s_namespace=args.k8s_namespace
)


def export_docker_compose(args):
"""Export to Docker compose yaml files
:param args: args from CLI
"""

Flow.load_config(args.flowpath).to_docker_compose_yaml(
output_path=args.outpath, network_name=args.network_name
)


def export_flowchart(args):
"""Export to flowchart file
:param args: args from CLI
"""
Flow.load_config(args.flowpath).plot(
args.outpath, vertical_layout=args.vertical_layout
)


def export_schema(args):
"""Export to JSON Schemas
:param args: args from CLI
"""
if args.yaml_path:
dump_api = api_to_dict()
for yp in args.yaml_path:
f_name = (yp % __version__) if '%s' in yp else yp
with open(f_name, 'w', encoding='utf8') as fp:
JAML.dump(dump_api, fp)
default_logger.info(f'API is exported to {f_name}')

if args.json_path:
dump_api = api_to_dict()
for jp in args.json_path:
f_name = (jp % __version__) if '%s' in jp else jp
with open(f_name, 'w', encoding='utf8') as fp:
json.dump(dump_api, fp, sort_keys=True)
default_logger.info(f'API is exported to {f_name}')

if args.schema_path:
dump_api = get_full_schema()
for jp in args.schema_path:
f_name = (jp % __version__) if '%s' in jp else jp
with open(f_name, 'w', encoding='utf8') as fp:
json.dump(dump_api, fp, sort_keys=True)
default_logger.info(f'API is exported to {f_name}')
2 changes: 1 addition & 1 deletion jina/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -762,7 +762,7 @@ def warn_unknown_args(unknown_args: List[str]):
:param unknown_args: arguments that are possibly unknown to Jina
"""

from cli.lookup import _build_lookup_table
from jina_cli.lookup import _build_lookup_table

all_args = _build_lookup_table()[0]
has_migration_tip = False
Expand Down
22 changes: 11 additions & 11 deletions jina/parsers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def get_main_parser():
"""
from jina.parsers.base import set_base_parser
from jina.parsers.create import set_new_project_parser
from jina.parsers.export_api import set_export_api_parser
from jina.parsers.export import set_export_parser
from jina.parsers.flow import set_flow_parser
from jina.parsers.helper import _SHOW_ALL_ARGS, _chf
from jina.parsers.hubble import set_hub_parser
Expand Down Expand Up @@ -191,6 +191,15 @@ def get_main_parser():
)
)

set_export_parser(
sp.add_parser(
'export',
help='Export Jina API/Flow',
description='Export Jina API and Flow to JSONSchema, Kubernetes YAML, or SVG flowchart.',
formatter_class=_chf,
)
)

set_new_project_parser(
sp.add_parser(
'new',
Expand Down Expand Up @@ -253,19 +262,10 @@ def get_main_parser():
set_client_cli_parser(
sp.add_parser(
'client',
description='Start a Python client that connects to a remote Jina gateway',
description='Start a Python client that connects to a Jina gateway',
formatter_class=_chf,
**(dict(help='Start a Client')) if _SHOW_ALL_ARGS else {},
)
)

set_export_api_parser(
sp.add_parser(
'export-api',
description='Export Jina API to JSON/YAML file for 3rd party applications',
formatter_class=_chf,
**(dict(help='Export Jina API to file')) if _SHOW_ALL_ARGS else {},
)
)

return parser
162 changes: 162 additions & 0 deletions jina/parsers/export.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
"""Argparser module for the export API"""

from jina.parsers.base import set_base_parser
from jina.parsers.helper import _chf


def set_export_parser(parser=None):
"""Set the parser for exporting
:param parser: the parser configure
:return: the parser
"""
if not parser:
parser = set_base_parser()

spp = parser.add_subparsers(
dest='export',
description='use `%(prog)-8s [sub-command] --help` '
'to get detailed information about each sub-command',
required=True,
)

set_export_flowchart_parser(
spp.add_parser(
'flowchart',
help='Export a Flow YAML file to a flowchart',
formatter_class=_chf,
)
)

set_export_k8s_parser(
spp.add_parser(
'kubernetes',
help='Export a Flow YAML file to a Kubernetes YAML bundle',
formatter_class=_chf,
)
)

set_export_docker_compose_parser(
spp.add_parser(
'docker-compose',
help='Export a Flow YAML file to a Docker Compose YAML file',
formatter_class=_chf,
)
)

set_export_schema_parser(
spp.add_parser(
'schema',
help='Export Jina Executor & Flow API to JSONSchema files',
formatter_class=_chf,
)
)

return parser


def mixin_base_io_parser(parser):
"""Add basic IO parsing args
:param parser: the parser configure
"""
parser.add_argument(
'flowpath', type=str, metavar='INPUT', help='The input file path of a Flow YAML'
)
parser.add_argument(
'outpath',
type=str,
metavar='OUTPUT',
help='The output path',
)


def set_export_docker_compose_parser(parser=None):
"""Set the parser for the flow chart export
:param parser: an optional existing parser to build upon
:return: the parser
"""
if not parser:
parser = set_base_parser()

mixin_base_io_parser(parser)

parser.add_argument(
'--network_name',
type=str,
help='The name of the network that will be used by the deployment name.',
)
return parser


def set_export_k8s_parser(parser=None):
"""Set the parser for the flow chart export
:param parser: an optional existing parser to build upon
:return: the parser
"""
if not parser:
parser = set_base_parser()

mixin_base_io_parser(parser)

parser.add_argument(
'--k8s-namespace',
type=str,
help='The name of the k8s namespace to set for the configurations. If None, the name of the Flow will be used.',
)
return parser


def set_export_flowchart_parser(parser=None):
"""Set the parser for the flow chart export
:param parser: an optional existing parser to build upon
:return: the parser
"""
if not parser:
parser = set_base_parser()

mixin_base_io_parser(parser)

parser.add_argument(
'--vertical-layout',
action='store_true',
default=False,
help='If set, then the flowchart is rendered vertically from top to down.',
)
return parser


def set_export_schema_parser(parser=None):
"""Set the parser for the API export
:param parser: an optional existing parser to build upon
:return: the parser
"""
if not parser:
parser = set_base_parser()

parser.add_argument(
'--yaml-path',
type=str,
nargs='*',
metavar='PATH',
help='The YAML file path for storing the exported API',
)
parser.add_argument(
'--json-path',
type=str,
nargs='*',
metavar='PATH',
help='The JSON file path for storing the exported API',
)
parser.add_argument(
'--schema-path',
type=str,
nargs='*',
metavar='PATH',
help='The JSONSchema file path for storing the exported API',
)
return parser
35 changes: 0 additions & 35 deletions jina/parsers/export_api.py

This file was deleted.

3 changes: 1 addition & 2 deletions jina/schemas/deployment.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from cli.export import api_to_dict

from jina.schemas.helper import _cli_to_schema
from jina_cli.export import api_to_dict

schema_deployment = _cli_to_schema(
api_to_dict(),
Expand Down
2 changes: 1 addition & 1 deletion jina/schemas/flow.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from cli.export import api_to_dict
from jina.schemas.helper import _cli_to_schema
from jina_cli.export import api_to_dict

_schema_flow_with = _cli_to_schema(
api_to_dict(),
Expand Down
Loading

0 comments on commit 16b16b0

Please sign in to comment.