Skip to content

Commit 16b16b0

Browse files
hanxiaojina-bot
andauthored
refactor: rename cli to jina_cli (jina-ai#4890)
* chore: fix readme * chore: fix readme * chore: fix dockerignore * fix: jina-ai#4845 * style: fix overload and cli autocomplete * fix: cicd export cli Co-authored-by: Jina Dev Bot <[email protected]>
1 parent d9d2dd3 commit 16b16b0

24 files changed

+276
-105
lines changed

.dockerignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
!*requirements.txt
99
!README.md
1010
!MANIFEST.in
11-
!cli/**
11+
!jina_cli/**
1212
!daemon/**
1313
!docarray/**
1414
!fastentrypoints.py

.github/labeler.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ area/docker:
3939
- ./.dockerignore
4040

4141
area/cli:
42-
- cli/**/*
42+
- jina_cli/**/*
4343

4444
area/docarray:
4545
- docarray/**/*

.github/workflows/cd.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ jobs:
4848
echo "JINA_VERSION=${JINA_VERSION}" >> $GITHUB_ENV
4949
cd schema
5050
mkdir -p schemas
51-
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
51+
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
5252
python ../scripts/get-openapi-schemas.py
5353
npm install --prefix ~ snippet-enricher-cli
5454
~/node_modules/.bin/snippet-enricher-cli --input=gateway.json --targets=shell_curl > gateway-with-code.json

.github/workflows/tag.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ jobs:
3636
pip install . --no-cache-dir
3737
cd schema
3838
mkdir -p schemas
39-
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
39+
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
4040
python ../scripts/get-openapi-schemas.py
4141
npm install --prefix ~ snippet-enricher-cli
4242
~/node_modules/.bin/snippet-enricher-cli --input=gateway.json --targets=shell_curl > gateway-with-code.json

jina/__main__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
if __name__ == '__main__':
2-
from cli import main
2+
from jina_cli import main
33

44
main()

jina/exporter.py

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import json
2+
3+
from jina import Flow, __version__
4+
from jina.jaml import JAML
5+
from jina.logging.predefined import default_logger
6+
from jina.schemas import get_full_schema
7+
from jina_cli.export import api_to_dict
8+
9+
10+
def export_kubernetes(args):
11+
"""Export to k8s yaml files
12+
13+
:param args: args from CLI
14+
"""
15+
Flow.load_config(args.flowpath).to_kubernetes_yaml(
16+
output_base_path=args.outpath, k8s_namespace=args.k8s_namespace
17+
)
18+
19+
20+
def export_docker_compose(args):
21+
"""Export to Docker compose yaml files
22+
23+
:param args: args from CLI
24+
"""
25+
26+
Flow.load_config(args.flowpath).to_docker_compose_yaml(
27+
output_path=args.outpath, network_name=args.network_name
28+
)
29+
30+
31+
def export_flowchart(args):
32+
"""Export to flowchart file
33+
34+
:param args: args from CLI
35+
"""
36+
Flow.load_config(args.flowpath).plot(
37+
args.outpath, vertical_layout=args.vertical_layout
38+
)
39+
40+
41+
def export_schema(args):
42+
"""Export to JSON Schemas
43+
44+
:param args: args from CLI
45+
"""
46+
if args.yaml_path:
47+
dump_api = api_to_dict()
48+
for yp in args.yaml_path:
49+
f_name = (yp % __version__) if '%s' in yp else yp
50+
with open(f_name, 'w', encoding='utf8') as fp:
51+
JAML.dump(dump_api, fp)
52+
default_logger.info(f'API is exported to {f_name}')
53+
54+
if args.json_path:
55+
dump_api = api_to_dict()
56+
for jp in args.json_path:
57+
f_name = (jp % __version__) if '%s' in jp else jp
58+
with open(f_name, 'w', encoding='utf8') as fp:
59+
json.dump(dump_api, fp, sort_keys=True)
60+
default_logger.info(f'API is exported to {f_name}')
61+
62+
if args.schema_path:
63+
dump_api = get_full_schema()
64+
for jp in args.schema_path:
65+
f_name = (jp % __version__) if '%s' in jp else jp
66+
with open(f_name, 'w', encoding='utf8') as fp:
67+
json.dump(dump_api, fp, sort_keys=True)
68+
default_logger.info(f'API is exported to {f_name}')

jina/helper.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -762,7 +762,7 @@ def warn_unknown_args(unknown_args: List[str]):
762762
:param unknown_args: arguments that are possibly unknown to Jina
763763
"""
764764

765-
from cli.lookup import _build_lookup_table
765+
from jina_cli.lookup import _build_lookup_table
766766

767767
all_args = _build_lookup_table()[0]
768768
has_migration_tip = False

jina/parsers/__init__.py

+11-11
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ def get_main_parser():
150150
"""
151151
from jina.parsers.base import set_base_parser
152152
from jina.parsers.create import set_new_project_parser
153-
from jina.parsers.export_api import set_export_api_parser
153+
from jina.parsers.export import set_export_parser
154154
from jina.parsers.flow import set_flow_parser
155155
from jina.parsers.helper import _SHOW_ALL_ARGS, _chf
156156
from jina.parsers.hubble import set_hub_parser
@@ -191,6 +191,15 @@ def get_main_parser():
191191
)
192192
)
193193

194+
set_export_parser(
195+
sp.add_parser(
196+
'export',
197+
help='Export Jina API/Flow',
198+
description='Export Jina API and Flow to JSONSchema, Kubernetes YAML, or SVG flowchart.',
199+
formatter_class=_chf,
200+
)
201+
)
202+
194203
set_new_project_parser(
195204
sp.add_parser(
196205
'new',
@@ -253,19 +262,10 @@ def get_main_parser():
253262
set_client_cli_parser(
254263
sp.add_parser(
255264
'client',
256-
description='Start a Python client that connects to a remote Jina gateway',
265+
description='Start a Python client that connects to a Jina gateway',
257266
formatter_class=_chf,
258267
**(dict(help='Start a Client')) if _SHOW_ALL_ARGS else {},
259268
)
260269
)
261270

262-
set_export_api_parser(
263-
sp.add_parser(
264-
'export-api',
265-
description='Export Jina API to JSON/YAML file for 3rd party applications',
266-
formatter_class=_chf,
267-
**(dict(help='Export Jina API to file')) if _SHOW_ALL_ARGS else {},
268-
)
269-
)
270-
271271
return parser

jina/parsers/export.py

+162
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
"""Argparser module for the export API"""
2+
3+
from jina.parsers.base import set_base_parser
4+
from jina.parsers.helper import _chf
5+
6+
7+
def set_export_parser(parser=None):
8+
"""Set the parser for exporting
9+
:param parser: the parser configure
10+
11+
:return: the parser
12+
"""
13+
if not parser:
14+
parser = set_base_parser()
15+
16+
spp = parser.add_subparsers(
17+
dest='export',
18+
description='use `%(prog)-8s [sub-command] --help` '
19+
'to get detailed information about each sub-command',
20+
required=True,
21+
)
22+
23+
set_export_flowchart_parser(
24+
spp.add_parser(
25+
'flowchart',
26+
help='Export a Flow YAML file to a flowchart',
27+
formatter_class=_chf,
28+
)
29+
)
30+
31+
set_export_k8s_parser(
32+
spp.add_parser(
33+
'kubernetes',
34+
help='Export a Flow YAML file to a Kubernetes YAML bundle',
35+
formatter_class=_chf,
36+
)
37+
)
38+
39+
set_export_docker_compose_parser(
40+
spp.add_parser(
41+
'docker-compose',
42+
help='Export a Flow YAML file to a Docker Compose YAML file',
43+
formatter_class=_chf,
44+
)
45+
)
46+
47+
set_export_schema_parser(
48+
spp.add_parser(
49+
'schema',
50+
help='Export Jina Executor & Flow API to JSONSchema files',
51+
formatter_class=_chf,
52+
)
53+
)
54+
55+
return parser
56+
57+
58+
def mixin_base_io_parser(parser):
59+
"""Add basic IO parsing args
60+
:param parser: the parser configure
61+
62+
"""
63+
parser.add_argument(
64+
'flowpath', type=str, metavar='INPUT', help='The input file path of a Flow YAML'
65+
)
66+
parser.add_argument(
67+
'outpath',
68+
type=str,
69+
metavar='OUTPUT',
70+
help='The output path',
71+
)
72+
73+
74+
def set_export_docker_compose_parser(parser=None):
75+
"""Set the parser for the flow chart export
76+
77+
:param parser: an optional existing parser to build upon
78+
:return: the parser
79+
"""
80+
if not parser:
81+
parser = set_base_parser()
82+
83+
mixin_base_io_parser(parser)
84+
85+
parser.add_argument(
86+
'--network_name',
87+
type=str,
88+
help='The name of the network that will be used by the deployment name.',
89+
)
90+
return parser
91+
92+
93+
def set_export_k8s_parser(parser=None):
94+
"""Set the parser for the flow chart export
95+
96+
:param parser: an optional existing parser to build upon
97+
:return: the parser
98+
"""
99+
if not parser:
100+
parser = set_base_parser()
101+
102+
mixin_base_io_parser(parser)
103+
104+
parser.add_argument(
105+
'--k8s-namespace',
106+
type=str,
107+
help='The name of the k8s namespace to set for the configurations. If None, the name of the Flow will be used.',
108+
)
109+
return parser
110+
111+
112+
def set_export_flowchart_parser(parser=None):
113+
"""Set the parser for the flow chart export
114+
115+
:param parser: an optional existing parser to build upon
116+
:return: the parser
117+
"""
118+
if not parser:
119+
parser = set_base_parser()
120+
121+
mixin_base_io_parser(parser)
122+
123+
parser.add_argument(
124+
'--vertical-layout',
125+
action='store_true',
126+
default=False,
127+
help='If set, then the flowchart is rendered vertically from top to down.',
128+
)
129+
return parser
130+
131+
132+
def set_export_schema_parser(parser=None):
133+
"""Set the parser for the API export
134+
135+
:param parser: an optional existing parser to build upon
136+
:return: the parser
137+
"""
138+
if not parser:
139+
parser = set_base_parser()
140+
141+
parser.add_argument(
142+
'--yaml-path',
143+
type=str,
144+
nargs='*',
145+
metavar='PATH',
146+
help='The YAML file path for storing the exported API',
147+
)
148+
parser.add_argument(
149+
'--json-path',
150+
type=str,
151+
nargs='*',
152+
metavar='PATH',
153+
help='The JSON file path for storing the exported API',
154+
)
155+
parser.add_argument(
156+
'--schema-path',
157+
type=str,
158+
nargs='*',
159+
metavar='PATH',
160+
help='The JSONSchema file path for storing the exported API',
161+
)
162+
return parser

jina/parsers/export_api.py

-35
This file was deleted.

jina/schemas/deployment.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
from cli.export import api_to_dict
2-
31
from jina.schemas.helper import _cli_to_schema
2+
from jina_cli.export import api_to_dict
43

54
schema_deployment = _cli_to_schema(
65
api_to_dict(),

jina/schemas/flow.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
from cli.export import api_to_dict
21
from jina.schemas.helper import _cli_to_schema
2+
from jina_cli.export import api_to_dict
33

44
_schema_flow_with = _cli_to_schema(
55
api_to_dict(),

0 commit comments

Comments
 (0)