Skip to content

Commit 3cc675b

Browse files
committed
Add chdir option to cli
Signed-off-by: ted chang <htchang@us.ibm.com>
1 parent be44e0a commit 3cc675b

2 files changed

Lines changed: 115 additions & 31 deletions

File tree

‎sdk/python/feast/cli.py‎

Lines changed: 59 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,22 @@
3939

4040

4141
@click.group()
42-
def cli():
42+
@click.option(
43+
"--chdir",
44+
"-c",
45+
help="Switch to a different feature repository directory before executing the given subcommand.",
46+
)
47+
@click.pass_context
48+
def cli(ctx: click.Context, chdir: str):
4349
"""
4450
Feast CLI
4551
4652
For more information, see our public docs at https://docs.feast.dev/
4753
4854
For any questions, you can reach us at https://slack.feast.dev/
4955
"""
56+
ctx.ensure_object(dict)
57+
ctx.obj["CHDIR"] = Path.cwd() if chdir is None else Path(chdir).absolute()
5058
pass
5159

5260

@@ -68,12 +76,14 @@ def entities_cmd():
6876

6977
@entities_cmd.command("describe")
7078
@click.argument("name", type=click.STRING)
71-
def entity_describe(name: str):
79+
@click.pass_context
80+
def entity_describe(ctx: click.Context, name: str):
7281
"""
7382
Describe an entity
7483
"""
75-
cli_check_repo(Path.cwd())
76-
store = FeatureStore(repo_path=str(Path.cwd()))
84+
repo = ctx.obj["CHDIR"]
85+
cli_check_repo(repo)
86+
store = FeatureStore(repo_path=str(repo))
7787

7888
try:
7989
entity = store.get_entity(name)
@@ -89,12 +99,14 @@ def entity_describe(name: str):
8999

90100

91101
@entities_cmd.command(name="list")
92-
def entity_list():
102+
@click.pass_context
103+
def entity_list(ctx: click.Context):
93104
"""
94105
List all entities
95106
"""
96-
cli_check_repo(Path.cwd())
97-
store = FeatureStore(repo_path=str(Path.cwd()))
107+
repo = ctx.obj["CHDIR"]
108+
cli_check_repo(repo)
109+
store = FeatureStore(repo_path=str(repo))
98110
table = []
99111
for entity in store.list_entities():
100112
table.append([entity.name, entity.description, entity.value_type])
@@ -114,12 +126,14 @@ def feature_views_cmd():
114126

115127
@feature_views_cmd.command("describe")
116128
@click.argument("name", type=click.STRING)
117-
def feature_view_describe(name: str):
129+
@click.pass_context
130+
def feature_view_describe(ctx: click.Context, name: str):
118131
"""
119132
Describe a feature view
120133
"""
121-
cli_check_repo(Path.cwd())
122-
store = FeatureStore(repo_path=str(Path.cwd()))
134+
repo = ctx.obj["CHDIR"]
135+
cli_check_repo(repo)
136+
store = FeatureStore(repo_path=str(repo))
123137

124138
try:
125139
feature_view = store.get_feature_view(name)
@@ -135,12 +149,14 @@ def feature_view_describe(name: str):
135149

136150

137151
@feature_views_cmd.command(name="list")
138-
def feature_view_list():
152+
@click.pass_context
153+
def feature_view_list(ctx: click.Context):
139154
"""
140155
List all feature views
141156
"""
142-
cli_check_repo(Path.cwd())
143-
store = FeatureStore(repo_path=str(Path.cwd()))
157+
repo = ctx.obj["CHDIR"]
158+
cli_check_repo(repo)
159+
store = FeatureStore(repo_path=str(repo))
144160
table = []
145161
for feature_view in store.list_feature_views():
146162
table.append([feature_view.name, feature_view.entities])
@@ -151,44 +167,50 @@ def feature_view_list():
151167

152168

153169
@cli.command("apply")
154-
def apply_total_command():
170+
@click.pass_context
171+
def apply_total_command(ctx: click.Context):
155172
"""
156173
Create or update a feature store deployment
157174
"""
158-
cli_check_repo(Path.cwd())
159-
repo_config = load_repo_config(Path.cwd())
175+
repo = ctx.obj["CHDIR"]
176+
cli_check_repo(repo)
177+
repo_config = load_repo_config(repo)
160178
tele = Telemetry()
161179
tele.log("apply")
162180
try:
163-
apply_total(repo_config, Path.cwd())
181+
apply_total(repo_config, repo)
164182
except FeastProviderLoginError as e:
165183
print(str(e))
166184

167185

168186
@cli.command("teardown")
169-
def teardown_command():
187+
@click.pass_context
188+
def teardown_command(ctx: click.Context):
170189
"""
171190
Tear down deployed feature store infrastructure
172191
"""
173-
cli_check_repo(Path.cwd())
174-
repo_config = load_repo_config(Path.cwd())
192+
repo = ctx.obj["CHDIR"]
193+
cli_check_repo(repo)
194+
repo_config = load_repo_config(repo)
175195
tele = Telemetry()
176196
tele.log("teardown")
177197

178-
teardown(repo_config, Path.cwd())
198+
teardown(repo_config, repo)
179199

180200

181201
@cli.command("registry-dump")
182-
def registry_dump_command():
202+
@click.pass_context
203+
def registry_dump_command(ctx: click.Context):
183204
"""
184205
Print contents of the metadata registry
185206
"""
186-
cli_check_repo(Path.cwd())
187-
repo_config = load_repo_config(Path.cwd())
207+
repo = ctx.obj["CHDIR"]
208+
cli_check_repo(repo)
209+
repo_config = load_repo_config(repo)
188210
tele = Telemetry()
189211
tele.log("registry-dump")
190212

191-
registry_dump(repo_config, repo_path=Path.cwd())
213+
registry_dump(repo_config, repo_path=repo)
192214

193215

194216
@cli.command("materialize")
@@ -197,7 +219,10 @@ def registry_dump_command():
197219
@click.option(
198220
"--views", "-v", help="Feature views to materialize", multiple=True,
199221
)
200-
def materialize_command(start_ts: str, end_ts: str, views: List[str]):
222+
@click.pass_context
223+
def materialize_command(
224+
ctx: click.Context, start_ts: str, end_ts: str, views: List[str]
225+
):
201226
"""
202227
Run a (non-incremental) materialization job to ingest data into the online store. Feast
203228
will read all data between START_TS and END_TS from the offline store and write it to the
@@ -206,8 +231,9 @@ def materialize_command(start_ts: str, end_ts: str, views: List[str]):
206231
207232
START_TS and END_TS should be in ISO 8601 format, e.g. '2021-07-16T19:20:01'
208233
"""
209-
cli_check_repo(Path.cwd())
210-
store = FeatureStore(repo_path=str(Path.cwd()))
234+
repo = ctx.obj["CHDIR"]
235+
cli_check_repo(repo)
236+
store = FeatureStore(repo_path=str(repo))
211237
store.materialize(
212238
feature_views=None if not views else views,
213239
start_date=datetime.fromisoformat(start_ts),
@@ -220,7 +246,8 @@ def materialize_command(start_ts: str, end_ts: str, views: List[str]):
220246
@click.option(
221247
"--views", "-v", help="Feature views to incrementally materialize", multiple=True,
222248
)
223-
def materialize_incremental_command(end_ts: str, views: List[str]):
249+
@click.pass_context
250+
def materialize_incremental_command(ctx: click.Context, end_ts: str, views: List[str]):
224251
"""
225252
Run an incremental materialization job to ingest new data into the online store. Feast will read
226253
all data from the previously ingested point to END_TS from the offline store and write it to the
@@ -229,8 +256,9 @@ def materialize_incremental_command(end_ts: str, views: List[str]):
229256
230257
END_TS should be in ISO 8601 format, e.g. '2021-07-16T19:20:01'
231258
"""
232-
cli_check_repo(Path.cwd())
233-
store = FeatureStore(repo_path=str(Path.cwd()))
259+
repo = ctx.obj["CHDIR"]
260+
cli_check_repo(repo)
261+
store = FeatureStore(repo_path=str(repo))
234262
store.materialize_incremental(
235263
feature_views=None if not views else views,
236264
end_date=datetime.fromisoformat(end_ts),

‎sdk/python/tests/test_cli_chdir.py‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import tempfile
2+
from datetime import datetime, timedelta
3+
from pathlib import Path
4+
5+
from tests.cli_utils import CliRunner
6+
7+
8+
def test_cli_chdir() -> None:
9+
"""
10+
This test simply makes sure that you can run 'feast --chdir COMMAND'
11+
to switch to a feature repository before running a COMMAND.
12+
"""
13+
runner = CliRunner()
14+
with tempfile.TemporaryDirectory() as temp_dir:
15+
# Make sure the path is absolute by resolving any symlinks
16+
temp_path = Path(temp_dir).resolve()
17+
result = runner.run(["init", "my_project"], cwd=temp_path)
18+
repo_path = temp_path / "my_project"
19+
assert result.returncode == 0
20+
21+
result = runner.run(["--chdir", repo_path, "apply"], cwd=temp_path)
22+
assert result.returncode == 0
23+
24+
result = runner.run(["--chdir", repo_path, "entities", "list"], cwd=temp_path)
25+
assert result.returncode == 0
26+
27+
result = runner.run(
28+
["--chdir", repo_path, "feature-views", "list"], cwd=temp_path
29+
)
30+
assert result.returncode == 0
31+
32+
end_date = datetime.utcnow()
33+
start_date = end_date - timedelta(days=100)
34+
result = runner.run(
35+
[
36+
"--chdir",
37+
repo_path,
38+
"materialize",
39+
start_date.isoformat(),
40+
end_date.isoformat(),
41+
],
42+
cwd=temp_path,
43+
)
44+
assert result.returncode == 0
45+
46+
result = runner.run(
47+
["--chdir", repo_path, "materialize-incremental", end_date.isoformat()],
48+
cwd=temp_path,
49+
)
50+
assert result.returncode == 0
51+
52+
result = runner.run(["--chdir", repo_path, "registry-dump"], cwd=temp_path)
53+
assert result.returncode == 0
54+
55+
result = runner.run(["--chdir", repo_path, "teardown"], cwd=temp_path)
56+
assert result.returncode == 0

0 commit comments

Comments
 (0)