faas-gitops 0.3.1__tar.gz → 0.4.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. {faas_gitops-0.3.1/src/faas_gitops.egg-info → faas_gitops-0.4.1}/PKG-INFO +1 -1
  2. {faas_gitops-0.3.1 → faas_gitops-0.4.1}/pyproject.toml +1 -1
  3. {faas_gitops-0.3.1 → faas_gitops-0.4.1}/src/faas_gitops/adapters/git_cli_gateway.py +32 -15
  4. {faas_gitops-0.3.1 → faas_gitops-0.4.1}/src/faas_gitops/cli/app.py +82 -16
  5. faas_gitops-0.4.1/src/faas_gitops/cli/parser.py +120 -0
  6. faas_gitops-0.4.1/src/faas_gitops/config.py +37 -0
  7. faas_gitops-0.4.1/src/faas_gitops/filtering.py +28 -0
  8. {faas_gitops-0.3.1 → faas_gitops-0.4.1}/src/faas_gitops/protocols/git_gateway.py +18 -0
  9. faas_gitops-0.4.1/src/faas_gitops/result.py +151 -0
  10. {faas_gitops-0.3.1 → faas_gitops-0.4.1}/src/faas_gitops/usecases/apply_function_changes.py +16 -15
  11. faas_gitops-0.4.1/src/faas_gitops/usecases/apply_plan.py +63 -0
  12. {faas_gitops-0.3.1 → faas_gitops-0.4.1}/src/faas_gitops/usecases/apply_template_changes.py +14 -7
  13. {faas_gitops-0.3.1 → faas_gitops-0.4.1}/src/faas_gitops/usecases/detect_function_drift.py +16 -8
  14. faas_gitops-0.4.1/src/faas_gitops/usecases/plan.py +115 -0
  15. {faas_gitops-0.3.1 → faas_gitops-0.4.1}/src/faas_gitops/usecases/render_definitions.py +4 -0
  16. faas_gitops-0.4.1/src/faas_gitops/usecases/snapshot.py +71 -0
  17. {faas_gitops-0.3.1 → faas_gitops-0.4.1}/src/faas_gitops/usecases/sync_functions.py +9 -3
  18. {faas_gitops-0.3.1 → faas_gitops-0.4.1}/src/faas_gitops/usecases/sync_templates.py +11 -3
  19. {faas_gitops-0.3.1 → faas_gitops-0.4.1/src/faas_gitops.egg-info}/PKG-INFO +1 -1
  20. {faas_gitops-0.3.1 → faas_gitops-0.4.1}/src/faas_gitops.egg-info/SOURCES.txt +9 -1
  21. {faas_gitops-0.3.1 → faas_gitops-0.4.1}/tests/test_apply_function_changes.py +16 -5
  22. faas_gitops-0.4.1/tests/test_apply_plan.py +160 -0
  23. {faas_gitops-0.3.1 → faas_gitops-0.4.1}/tests/test_apply_template_changes.py +16 -4
  24. {faas_gitops-0.3.1 → faas_gitops-0.4.1}/tests/test_detect_function_drift.py +9 -2
  25. {faas_gitops-0.3.1 → faas_gitops-0.4.1}/tests/test_git_cli_gateway.py +7 -0
  26. faas_gitops-0.4.1/tests/test_plan.py +146 -0
  27. {faas_gitops-0.3.1 → faas_gitops-0.4.1}/tests/test_render_definitions.py +34 -0
  28. faas_gitops-0.4.1/tests/test_result_contract.py +172 -0
  29. faas_gitops-0.4.1/tests/test_scope_args.py +148 -0
  30. faas_gitops-0.4.1/tests/test_snapshot.py +167 -0
  31. {faas_gitops-0.3.1 → faas_gitops-0.4.1}/tests/test_sync_functions.py +18 -3
  32. {faas_gitops-0.3.1 → faas_gitops-0.4.1}/tests/test_sync_templates.py +16 -1
  33. faas_gitops-0.3.1/src/faas_gitops/cli/parser.py +0 -42
  34. faas_gitops-0.3.1/src/faas_gitops/cli/presenters.py +0 -41
  35. faas_gitops-0.3.1/src/faas_gitops/config.py +0 -30
  36. faas_gitops-0.3.1/src/faas_gitops/filtering.py +0 -9
  37. {faas_gitops-0.3.1 → faas_gitops-0.4.1}/MANIFEST.in +0 -0
  38. {faas_gitops-0.3.1 → faas_gitops-0.4.1}/setup.cfg +0 -0
  39. {faas_gitops-0.3.1 → faas_gitops-0.4.1}/src/faas_gitops/__init__.py +0 -0
  40. {faas_gitops-0.3.1 → faas_gitops-0.4.1}/src/faas_gitops/adapters/__init__.py +0 -0
  41. {faas_gitops-0.3.1 → faas_gitops-0.4.1}/src/faas_gitops/adapters/bizy_function_provider.py +0 -0
  42. {faas_gitops-0.3.1 → faas_gitops-0.4.1}/src/faas_gitops/adapters/bizy_template_provider.py +0 -0
  43. {faas_gitops-0.3.1 → faas_gitops-0.4.1}/src/faas_gitops/cli/__init__.py +0 -0
  44. {faas_gitops-0.3.1 → faas_gitops-0.4.1}/src/faas_gitops/infra/__init__.py +0 -0
  45. {faas_gitops-0.3.1 → faas_gitops-0.4.1}/src/faas_gitops/infra/yaml_utils.py +0 -0
  46. {faas_gitops-0.3.1 → faas_gitops-0.4.1}/src/faas_gitops/layout.py +0 -0
  47. {faas_gitops-0.3.1 → faas_gitops-0.4.1}/src/faas_gitops/models.py +0 -0
  48. {faas_gitops-0.3.1 → faas_gitops-0.4.1}/src/faas_gitops/protocols/__init__.py +0 -0
  49. {faas_gitops-0.3.1 → faas_gitops-0.4.1}/src/faas_gitops/protocols/faas_function.py +0 -0
  50. {faas_gitops-0.3.1 → faas_gitops-0.4.1}/src/faas_gitops/protocols/faas_template.py +0 -0
  51. {faas_gitops-0.3.1 → faas_gitops-0.4.1}/src/faas_gitops/usecases/__init__.py +0 -0
  52. {faas_gitops-0.3.1 → faas_gitops-0.4.1}/src/faas_gitops/usecases/deploy_functions.py +0 -0
  53. {faas_gitops-0.3.1 → faas_gitops-0.4.1}/src/faas_gitops/usecases/detect_drift.py +0 -0
  54. {faas_gitops-0.3.1 → faas_gitops-0.4.1}/src/faas_gitops/usecases/detect_template_drift.py +0 -0
  55. {faas_gitops-0.3.1 → faas_gitops-0.4.1}/src/faas_gitops.egg-info/dependency_links.txt +0 -0
  56. {faas_gitops-0.3.1 → faas_gitops-0.4.1}/src/faas_gitops.egg-info/entry_points.txt +0 -0
  57. {faas_gitops-0.3.1 → faas_gitops-0.4.1}/src/faas_gitops.egg-info/requires.txt +0 -0
  58. {faas_gitops-0.3.1 → faas_gitops-0.4.1}/src/faas_gitops.egg-info/top_level.txt +0 -0
  59. {faas_gitops-0.3.1 → faas_gitops-0.4.1}/tests/test_deploy_functions.py +0 -0
  60. {faas_gitops-0.3.1 → faas_gitops-0.4.1}/tests/test_detect_drift.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: faas-gitops
3
- Version: 0.3.1
3
+ Version: 0.4.1
4
4
  Summary: Declarative GitOps for FaaS
5
5
  Requires-Python: >=3.12
6
6
  Requires-Dist: aiofiles>=23.0
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "faas-gitops"
7
- version = "0.3.1"
7
+ version = "0.4.1"
8
8
  description = "Declarative GitOps for FaaS"
9
9
  requires-python = ">=3.12"
10
10
  dependencies = [
@@ -1,9 +1,10 @@
1
1
  from __future__ import annotations
2
2
 
3
3
  import asyncio
4
+ import re
4
5
  from pathlib import Path
5
6
  from loguru import logger
6
- from faas_gitops.protocols.git_gateway import PushResult
7
+ from faas_gitops.protocols.git_gateway import PrResult, PushResult
7
8
 
8
9
 
9
10
  async def _git(*args: str, cwd: Path | None = None, check: bool = True) -> str:
@@ -68,6 +69,30 @@ class GitCliGateway:
68
69
  async def checkout(self, repo_path: Path, ref: str) -> None:
69
70
  await _git("checkout", ref, cwd=repo_path)
70
71
 
72
+ async def checkout_origin(self, repo_path: Path, ref: str) -> None:
73
+ await _git("fetch", "origin", cwd=repo_path)
74
+ await _git("checkout", "-B", ref, f"origin/{ref}", cwd=repo_path)
75
+
76
+ async def short_sha(self, repo_path: Path, ref: str) -> str:
77
+ return await _git("rev-parse", "--short", f"origin/{ref}", cwd=repo_path)
78
+
79
+ async def create_pr(self, repo_path: Path, branch: str, title: str, body: str) -> PrResult:
80
+ proc = await asyncio.create_subprocess_exec(
81
+ "gh", "pr", "create", "--base", "main", "--head", branch,
82
+ "--title", title, "--body", body,
83
+ cwd=repo_path,
84
+ stdout=asyncio.subprocess.PIPE,
85
+ stderr=asyncio.subprocess.PIPE,
86
+ )
87
+ stdout, stderr = await proc.communicate()
88
+ out, err = stdout.decode().strip(), stderr.decode().strip()
89
+ # success → URL on stdout; "PR already exists" → gh prints an indented URL on stderr
90
+ if url := out:
91
+ return PrResult(url=url, created=True)
92
+ if m := re.search(r"https?://\S+", err):
93
+ return PrResult(url=m.group(0), created=False)
94
+ raise RuntimeError(f"gh pr create failed: {err or out}")
95
+
71
96
  async def commit_and_push(
72
97
  self,
73
98
  repo_path: Path,
@@ -78,7 +103,8 @@ class GitCliGateway:
78
103
  force: bool = False,
79
104
  create_pr: bool = False,
80
105
  ) -> PushResult:
81
- await _git("add", prefix, cwd=repo_path)
106
+ if (repo_path / prefix).exists():
107
+ await _git("add", prefix, cwd=repo_path)
82
108
  proc = await asyncio.create_subprocess_exec(
83
109
  "git", "diff", "--cached", "--quiet",
84
110
  cwd=repo_path,
@@ -92,16 +118,7 @@ class GitCliGateway:
92
118
  if not force:
93
119
  await _git("pull", "--rebase", "origin", branch, cwd=repo_path, check=False)
94
120
  await _git("push", "origin", branch, *(["--force"] if force else []), cwd=repo_path)
95
- if create_pr:
96
- proc = await asyncio.create_subprocess_exec(
97
- "gh", "pr", "create", "--fill", "--base", "main", "--head", branch,
98
- cwd=repo_path,
99
- stdout=asyncio.subprocess.PIPE,
100
- stderr=asyncio.subprocess.PIPE,
101
- )
102
- stdout, stderr = await proc.communicate()
103
- out = stdout.decode().strip()
104
- err = stderr.decode().strip()
105
- url = out or next((l for l in err.splitlines() if l.startswith("http")), None)
106
- return PushResult(pushed=True, url=url)
107
- return PushResult(pushed=True)
121
+ if not create_pr:
122
+ return PushResult(pushed=True)
123
+ pr = await self.create_pr(repo_path, branch, title=msg.splitlines()[0], body=msg)
124
+ return PushResult(pushed=True, url=pr.url)
@@ -1,6 +1,7 @@
1
1
  from __future__ import annotations
2
2
 
3
3
  import asyncio
4
+ import json
4
5
  import tempfile
5
6
  from contextlib import AsyncExitStack
6
7
  from pathlib import Path
@@ -11,16 +12,19 @@ from loguru import logger
11
12
  from faas_gitops.adapters.bizy_function_provider import BizyFunctionProvider
12
13
  from faas_gitops.adapters.bizy_template_provider import BizyTemplateProvider
13
14
  from faas_gitops.adapters.git_cli_gateway import GitCliGateway
14
- from faas_gitops.cli.parser import build_parser
15
- from faas_gitops.cli.presenters import print_apply_report, print_deploy_report, print_drift_report
16
- from faas_gitops.config import load_settings
15
+ from faas_gitops.cli.parser import build_parser, scope_from_args
16
+ from faas_gitops.result import print_apply_report, print_deploy_report, print_drift_report
17
+ from faas_gitops.config import load_settings, warn_deprecated_env
17
18
  from faas_gitops.models import ApplyResult
19
+ from faas_gitops.usecases.plan import Plan, PlanBuild
18
20
  from faas_gitops.usecases.apply_function_changes import ApplyFunctionChanges
21
+ from faas_gitops.usecases.apply_plan import ApplyPlan, StalePlanError
19
22
  from faas_gitops.usecases.apply_template_changes import ApplyTemplateChanges
20
23
  from faas_gitops.usecases.deploy_functions import DeployFunctions
21
24
  from faas_gitops.usecases.detect_function_drift import DetectFunctionDrift
22
25
  from faas_gitops.usecases.detect_template_drift import DetectTemplateDrift
23
26
  from faas_gitops.usecases.render_definitions import render_definitions
27
+ from faas_gitops.usecases.snapshot import snapshot
24
28
  from faas_gitops.usecases.sync_functions import SyncFunctions
25
29
  from faas_gitops.usecases.sync_templates import SyncTemplates
26
30
 
@@ -36,13 +40,16 @@ async def _make_faas(stack: AsyncExitStack) -> AsyncFaaSActionProvider:
36
40
 
37
41
 
38
42
  async def async_main() -> int:
43
+ warn_deprecated_env()
39
44
  settings = load_settings()
40
- parser = build_parser(settings)
41
- args = parser.parse_args()
45
+ base_parser = build_parser(settings)
46
+ args = base_parser.parse_args()
42
47
  if not args.command:
43
- parser.print_help()
48
+ base_parser.print_help()
44
49
  return 1
45
50
 
51
+ # F00: scope comes from CLI args only (SCOPED_* env vars are ignored; see warn above)
52
+ settings = scope_from_args(args)
46
53
  repo_slug: str = args.repo
47
54
 
48
55
  async with AsyncExitStack() as stack:
@@ -50,7 +57,9 @@ async def async_main() -> int:
50
57
  faas = await _make_faas(stack)
51
58
  provider = BizyTemplateProvider(faas)
52
59
  git = _make_git()
53
- push = await SyncTemplates(provider, git, settings).run(await git.clone_repo(repo_slug))
60
+ push = await SyncTemplates(
61
+ provider, git, settings, branch=args.branch, force=args.force,
62
+ ).run(await git.clone_repo(repo_slug))
54
63
  logger.info(push.url if push.pushed and push.url else ("branch pushed" if push.pushed else "no changes"))
55
64
  return 0
56
65
 
@@ -60,52 +69,109 @@ async def async_main() -> int:
60
69
  tpl_provider = BizyTemplateProvider(faas)
61
70
  git = _make_git()
62
71
  repo_path = await git.clone_repo(repo_slug)
63
- push = await SyncFunctions(provider, git, settings, tpl_provider=tpl_provider, branch=args.branch).run(repo_path)
72
+ push = await SyncFunctions(
73
+ provider, git, settings, tpl_provider=tpl_provider, branch=args.branch, force=args.force,
74
+ ).run(repo_path)
64
75
  logger.info(push.url if push.pushed and push.url else ("branch pushed" if push.pushed else "no changes"))
65
76
  return 0
66
77
 
78
+ if args.command == "snapshot":
79
+ faas = await _make_faas(stack)
80
+ git = _make_git()
81
+ repo_path = await git.clone_repo(repo_slug)
82
+ pr = await snapshot(
83
+ BizyFunctionProvider(faas), BizyTemplateProvider(faas), git, settings,
84
+ repo_path, branch=args.branch, force=args.force,
85
+ )
86
+ logger.info(pr.url if pr else "no changes")
87
+ return 0
88
+
89
+ if args.command == "plan":
90
+ git = _make_git()
91
+ repo_path = await git.clone_repo(repo_slug)
92
+ plan = await PlanBuild(git, settings).run(
93
+ repo_path, base=args.to, ref=args.from_ref, repo_slug=repo_slug,
94
+ )
95
+ plan.log()
96
+ name = f"{args.from_ref}_vs_{args.to}".replace("/", "_")
97
+ out = Path(args.output or f"runs/plan_{name}.json")
98
+ out.parent.mkdir(parents=True, exist_ok=True)
99
+ out.write_text(json.dumps(plan.payload(), ensure_ascii=False, indent=2, sort_keys=True) + "\n")
100
+ logger.info(f"plan written to {out.resolve()}")
101
+ return 0 if plan.kind == "none" else 2
102
+
103
+ if args.command == "apply-plan":
104
+ faas = await _make_faas(stack)
105
+ git = _make_git()
106
+ repo_path = await git.clone_repo(repo_slug)
107
+ plan = Plan.from_dict(json.loads(Path(args.plan).read_text()))
108
+ dry_run = args.dry_run
109
+ try:
110
+ tpl_result, fn_result = await ApplyPlan(
111
+ BizyTemplateProvider(faas), BizyFunctionProvider(faas), settings, git,
112
+ ).run(repo_path, plan, dry_run=dry_run)
113
+ except StalePlanError as e:
114
+ logger.error(str(e))
115
+ return 1
116
+
117
+ def _out(suffix: str) -> str | None:
118
+ return f"{args.output}.{suffix}" if args.output else None
119
+
120
+ rc = max(
121
+ print_apply_report(tpl_result, dry_run, _out("templates"), domain="templates"),
122
+ print_apply_report(fn_result, dry_run, _out("functions"), domain="functions"),
123
+ )
124
+ if not dry_run and args.auto_deploy and fn_result.applied_versions:
125
+ deploy_result = await DeployFunctions(BizyFunctionProvider(faas), settings).run(
126
+ fn_result.applied_versions, repo_path
127
+ )
128
+ rc = max(rc, print_deploy_report(deploy_result, _out("deploy")))
129
+ return rc
130
+
67
131
  if args.command == "render-definitions":
68
132
  faas = await _make_faas(stack)
69
133
  tpl_provider = BizyTemplateProvider(faas)
70
134
  git = _make_git()
71
135
  repo_path = await git.clone_repo(repo_slug)
72
- n = await render_definitions(tpl_provider, repo_path, branch=args.branch, git=git, max_workers=settings.max_workers)
136
+ n = await render_definitions(tpl_provider, repo_path, branch=args.branch, git=git, max_workers=settings.max_workers, scope=settings)
73
137
  logger.info(f"render-definitions: {n} file(s) written")
74
138
  return 0
75
139
 
76
140
  if args.command == "detect-template-changes":
77
141
  git = _make_git()
78
142
  repo_path = await git.clone_repo(repo_slug)
79
- return print_drift_report(await DetectTemplateDrift().run(repo_path))
143
+ return print_drift_report(await DetectTemplateDrift().run(repo_path), args.output)
80
144
 
81
145
  if args.command == "detect-function-changes":
82
146
  git = _make_git()
83
147
  repo_path = await git.clone_repo(repo_slug)
84
- return print_drift_report(await DetectFunctionDrift(settings).run(repo_path))
148
+ return print_drift_report(await DetectFunctionDrift(settings).run(repo_path), args.output)
85
149
 
86
150
  if args.command == "apply-template-changes":
87
151
  faas = await _make_faas(stack)
88
152
  git = _make_git()
89
153
  repo_path = await git.clone_repo(repo_slug)
90
- result = await ApplyTemplateChanges(BizyTemplateProvider(faas)).run(
154
+ result = await ApplyTemplateChanges(BizyTemplateProvider(faas), git).run(
91
155
  repo_path, dry_run=getattr(args, "dry_run", False),
92
156
  )
93
- return print_apply_report(result, getattr(args, "dry_run", False))
157
+ return print_apply_report(result, getattr(args, "dry_run", False), args.output)
94
158
 
95
159
  if args.command == "apply-function-changes":
96
160
  faas = await _make_faas(stack)
97
161
  git = _make_git()
98
162
  repo_path = await git.clone_repo(repo_slug)
99
163
  dry_run = getattr(args, "dry_run", False)
100
- result = await ApplyFunctionChanges(BizyFunctionProvider(faas), settings).run(
164
+ result = await ApplyFunctionChanges(BizyFunctionProvider(faas), settings, git).run(
101
165
  repo_path, dry_run=dry_run, repo_slug=repo_slug,
102
166
  )
167
+ deploy_rc = 0
103
168
  if not dry_run and getattr(args, "auto_deploy", False) and result.applied_versions:
104
169
  deploy_result = await DeployFunctions(BizyFunctionProvider(faas), settings).run(
105
170
  result.applied_versions, repo_path
106
171
  )
107
- print_deploy_report(deploy_result)
108
- return print_apply_report(result, dry_run)
172
+ deploy_out = f"{args.output}.deploy.json" if args.output else None
173
+ deploy_rc = print_deploy_report(deploy_result, deploy_out)
174
+ return deploy_rc or print_apply_report(result, dry_run, args.output)
109
175
 
110
176
  logger.warning(f"unknown command: {args.command}")
111
177
  return 1
@@ -0,0 +1,120 @@
1
+ from __future__ import annotations
2
+
3
+ import argparse
4
+
5
+ from faas_gitops.config import Settings
6
+ from faas_gitops.filtering import match_scope
7
+
8
+
9
+ def _csv_set(value: str | None) -> set[str] | None:
10
+ return {s for s in (value or "").split(",") if s} or None
11
+
12
+
13
+ def add_scope_args(p: argparse.ArgumentParser) -> None:
14
+ """F00: domain-split scope args replacing SCOPED_* env vars.
15
+
16
+ Functions and templates are two independent tag namespaces on the FaaS
17
+ side — hence --func-tag / --tpl-tag instead of a single --tag.
18
+ """
19
+ g = p.add_argument_group("scope")
20
+ g.add_argument("--func-tag", action="append", default=None,
21
+ help="function tag filter (repeatable, union); replaces SCOPED_FUNCTION_TAGS")
22
+ g.add_argument("--functions", default=None,
23
+ help="comma-separated function ids; replaces SCOPED_FUNCTIONS")
24
+ g.add_argument("--tpl-tag", action="append", default=None,
25
+ help="template tag filter (repeatable, union); replaces SCOPED_TEMPLATE_TAGS")
26
+ g.add_argument("--templates", default=None,
27
+ help="comma-separated template names org/tpl; replaces SCOPED_TEMPLATES")
28
+
29
+
30
+ def scope_from_args(args: argparse.Namespace) -> Settings:
31
+ """Build scope Settings from parsed CLI args (env vars are NOT read — F00)."""
32
+ return Settings(
33
+ scoped_functions=_csv_set(args.functions),
34
+ scoped_function_tags=_csv_set(",".join(args.func_tag)) if args.func_tag else None,
35
+ scoped_templates=_csv_set(args.templates),
36
+ scoped_template_tags=_csv_set(",".join(args.tpl_tag)) if args.tpl_tag else None,
37
+ )
38
+
39
+
40
+ def in_function_scope(item_id: str, tags: list[str], settings: Settings) -> bool:
41
+ """Function-domain gate (empty function scope → False, per match_scope)."""
42
+ return match_scope(item_id, tags, settings.scoped_functions, settings.scoped_function_tags)
43
+
44
+
45
+ def in_template_scope(item_id: str, tags: list[str], settings: Settings) -> bool:
46
+ """Template-domain gate (empty template scope → False, per match_scope)."""
47
+ return match_scope(item_id, tags, settings.scoped_templates, settings.scoped_template_tags)
48
+
49
+
50
+ def build_parser(settings: Settings) -> argparse.ArgumentParser:
51
+ parser = argparse.ArgumentParser(prog="faas-gitops")
52
+ sub = parser.add_subparsers(dest="command")
53
+
54
+ def _add_repo(p: argparse.ArgumentParser) -> None:
55
+ p.add_argument("--repo", default=settings.default_repo)
56
+ p.add_argument("-o", "--output", default=None,
57
+ help="write result JSON (F01 three-bucket contract) to this path")
58
+
59
+ def _add_force(p: argparse.ArgumentParser) -> None:
60
+ p.add_argument("--force", action=argparse.BooleanOptionalAction, default=True,
61
+ help="recreate the branch from latest main and force-push (default); "
62
+ "--no-force appends to the existing remote branch instead")
63
+
64
+ p = sub.add_parser("sync-templates")
65
+ p.add_argument("--branch", default="sync/faas-templates")
66
+ _add_force(p)
67
+ add_scope_args(p)
68
+ _add_repo(p)
69
+
70
+ p = sub.add_parser("sync-functions")
71
+ p.add_argument("--branch", default="sync/faas-functions")
72
+ _add_force(p)
73
+ add_scope_args(p)
74
+ _add_repo(p)
75
+
76
+ p = sub.add_parser("snapshot")
77
+ p.add_argument("--branch", default=None, help="default: remote/state-<date>-<HHMM>")
78
+ _add_force(p)
79
+ add_scope_args(p)
80
+ _add_repo(p)
81
+
82
+ p = sub.add_parser("plan")
83
+ p.add_argument("--from", required=True, dest="from_ref",
84
+ help="branch/ref carrying the changes (compared via origin refs)")
85
+ p.add_argument("--to", default="main", help="baseline ref (default: main)")
86
+ add_scope_args(p)
87
+ _add_repo(p)
88
+
89
+ p = sub.add_parser("render-definitions")
90
+ p.add_argument("--branch", default=None)
91
+ add_scope_args(p)
92
+ _add_repo(p)
93
+
94
+ p = sub.add_parser("detect-template-changes")
95
+ add_scope_args(p)
96
+ _add_repo(p)
97
+
98
+ p = sub.add_parser("detect-function-changes")
99
+ add_scope_args(p)
100
+ _add_repo(p)
101
+
102
+ p = sub.add_parser("apply-template-changes")
103
+ p.add_argument("--dry-run", action="store_true")
104
+ add_scope_args(p)
105
+ _add_repo(p)
106
+
107
+ p = sub.add_parser("apply-plan")
108
+ p.add_argument("--plan", required=True, help="path to plan.json produced by `faas-gitops plan`")
109
+ p.add_argument("--dry-run", action="store_true")
110
+ p.add_argument("--auto-deploy", action="store_true")
111
+ add_scope_args(p)
112
+ _add_repo(p)
113
+
114
+ p = sub.add_parser("apply-function-changes")
115
+ p.add_argument("--dry-run", action="store_true")
116
+ p.add_argument("--auto-deploy", action="store_true")
117
+ add_scope_args(p)
118
+ _add_repo(p)
119
+
120
+ return parser
@@ -0,0 +1,37 @@
1
+ from __future__ import annotations
2
+
3
+ import os
4
+ from dataclasses import dataclass
5
+
6
+ from loguru import logger
7
+
8
+
9
+ @dataclass(frozen=True)
10
+ class Settings:
11
+ default_repo: str = "siliconflow/faas-gitops-test"
12
+ scoped_templates: set[str] | None = None
13
+ scoped_template_tags: set[str] | None = None
14
+ scoped_functions: set[str] | None = None
15
+ scoped_function_tags: set[str] | None = None
16
+ max_workers: int = 8
17
+
18
+
19
+ _DEPRECATED_ENV_VARS = {
20
+ "DEFAULT_REPO": "--repo",
21
+ "SCOPED_FUNCTIONS": "--functions",
22
+ "SCOPED_FUNCTION_TAGS": "--func-tag",
23
+ "SCOPED_TEMPLATES": "--templates",
24
+ "SCOPED_TEMPLATE_TAGS": "--tpl-tag",
25
+ }
26
+
27
+
28
+ def warn_deprecated_env() -> None:
29
+ """Warn (and ignore) legacy SCOPED_* / DEFAULT_REPO env vars; scope is CLI-only now."""
30
+ for var, flag in _DEPRECATED_ENV_VARS.items():
31
+ if os.getenv(var):
32
+ logger.warning(f"env {var} is deprecated and ignored; use '{flag} ${{{var}}}' instead")
33
+
34
+
35
+ def load_settings() -> Settings:
36
+ """Load non-scope settings (max_workers only). Scope comes from CLI args (F00)."""
37
+ return Settings(max_workers=int(os.getenv("MAX_WORKERS", "128")))
@@ -0,0 +1,28 @@
1
+ from __future__ import annotations
2
+
3
+
4
+ def match_scope(item_id: str, tags: list[str], id_set: set[str] | None, tag_set: set[str] | None) -> bool:
5
+ """Explicit-scope rule: an item matches only when a scope is specified.
6
+
7
+ No id/tag filter given → nothing matches, so an un-scoped command can never
8
+ accidentally pull the entire FaaS estate.
9
+
10
+ Args:
11
+ item_id: Template name (``org/tpl``) or function id.
12
+ tags: The item's tags, matched against ``tag_set``.
13
+ id_set: Allowed ids, e.g. ``{"fn1", "fn2"}``.
14
+ tag_set: Allowed tags, e.g. ``{"gitops"}``; ids and tags union when both given.
15
+
16
+ Example:
17
+ >>> match_scope("fn1", ["gold"], None, None) # nothing specified
18
+ False
19
+ >>> match_scope("fn1", ["silver"], {"fn1"}, None) # id arm
20
+ True
21
+ >>> match_scope("fn9", ["gold"], {"fn1"}, {"gold"}) # tag arm rescues
22
+ True
23
+ """
24
+ if id_set is None and tag_set is None:
25
+ return False
26
+ by_id = id_set is not None and item_id in id_set
27
+ by_tag = tag_set is not None and tags is not None and bool(set(tags) & tag_set)
28
+ return by_id or by_tag
@@ -11,6 +11,12 @@ class PushResult:
11
11
  url: str | None = None
12
12
 
13
13
 
14
+ @dataclass(frozen=True)
15
+ class PrResult:
16
+ url: str
17
+ created: bool
18
+
19
+
14
20
  @runtime_checkable
15
21
  class GitWriter(Protocol):
16
22
 
@@ -39,3 +45,15 @@ class GitWriter(Protocol):
39
45
  async def checkout(self, repo_path: Path, ref: str) -> None:
40
46
  """Switch to the given branch or ref."""
41
47
  ...
48
+
49
+ async def checkout_origin(self, repo_path: Path, ref: str) -> None:
50
+ """Materialize ``origin/<ref>`` in the working tree (fetch + hard reset)."""
51
+ ...
52
+
53
+ async def short_sha(self, repo_path: Path, ref: str) -> str:
54
+ """Short sha of ``origin/<ref>``."""
55
+ ...
56
+
57
+ async def create_pr(self, repo_path: Path, branch: str, title: str, body: str) -> PrResult:
58
+ """Open a PR from ``branch`` to main."""
59
+ ...
@@ -0,0 +1,151 @@
1
+ """F01 result contract: data-model driven reports.
2
+
3
+ A `Report` is the single model behind every subcommand's output —
4
+ summary line, (bucket, prefix) rows, JSON payload, exit code — so adding a
5
+ command means declaring buckets, not hand-rolling logger calls.
6
+ """
7
+ from __future__ import annotations
8
+
9
+ import json
10
+ from dataclasses import dataclass, field
11
+ from pathlib import Path
12
+ from typing import Any, Iterator
13
+
14
+ from loguru import logger
15
+
16
+ from faas_gitops.models import ApplyResult, DeployResult, DriftResult
17
+
18
+ EXIT_OK = 0
19
+ EXIT_FAILED = 1
20
+ EXIT_ACTION_NEEDED = 2
21
+
22
+
23
+ @dataclass(frozen=True)
24
+ class Bucket:
25
+ """One outcome category of a report: JSON key + log line prefix + exit semantic."""
26
+ key: str
27
+ prefix: str = " "
28
+ causes_failure: bool = False # items here → exit 1
29
+ causes_action: bool = False # items here → exit 2
30
+
31
+
32
+ class Buckets:
33
+ """Bucket catalog: the ONLY place bucket keys are defined. Consumers
34
+ (tests, CI jq, notify) reference these, never bare strings."""
35
+ APPLIED = Bucket("applied", "✓")
36
+ UNCHANGED = Bucket("unchanged", "=")
37
+ SKIPPED = Bucket("skipped", "-")
38
+ DEPLOYED = Bucket("deployed", "→")
39
+ FAILED = Bucket("failed", "!", causes_failure=True)
40
+
41
+ class Drift:
42
+ DETECTED = Bucket("drifted", "Δ", causes_action=True)
43
+ MISSING_IN_GIT = Bucket("missing_in_git", "+", causes_action=True)
44
+ MISSING_IN_FAAS = Bucket("missing_in_faas", "-", causes_action=True)
45
+
46
+
47
+ @dataclass
48
+ class Report:
49
+ """Data model for subcommand results: buckets + summary headline + exit code."""
50
+ headline: str
51
+ buckets: list[Bucket] = field(default_factory=list)
52
+ rows: dict[str, list[Any]] = field(default_factory=dict)
53
+
54
+ def add(self, bucket: Bucket, items: list[Any]) -> None:
55
+ self.buckets.append(bucket)
56
+ self.rows[bucket.key] = sorted(items, key=str)
57
+
58
+ def bucket(self, key: str) -> list[Any]:
59
+ return self.rows.get(key, [])
60
+
61
+ @property
62
+ def payload(self) -> dict:
63
+ return {b.key: self.bucket(b.key) for b in self.buckets}
64
+
65
+ @property
66
+ def exit_code(self) -> int:
67
+ if any(self.bucket(b.key) for b in self.buckets if b.causes_failure):
68
+ return EXIT_FAILED
69
+ if any(self.bucket(b.key) for b in self.buckets if b.causes_action):
70
+ return EXIT_ACTION_NEEDED
71
+ return EXIT_OK
72
+
73
+ def log(self) -> None:
74
+ parts = " ".join(f"{b.key}={len(self.bucket(b.key))}" for b in self.buckets)
75
+ logger.info(f"{self.headline} {parts}".rstrip())
76
+ for b in self.buckets:
77
+ for item in self.bucket(b.key):
78
+ if isinstance(item, dict): # failed rows: {id, error}
79
+ logger.warning(f" {b.prefix} {item['id']}: {item['error']}")
80
+ else:
81
+ logger.info(f" {b.prefix} {item}")
82
+
83
+ def write_json(self, path: str | None) -> None:
84
+ if not path:
85
+ return
86
+ p = Path(path)
87
+ p.parent.mkdir(parents=True, exist_ok=True)
88
+ p.write_text(json.dumps({k: v for k, v in self.payload.items()},
89
+ ensure_ascii=False, indent=2, sort_keys=True) + "\n")
90
+
91
+ def emit(self, output: str | None = None, extra_log: str | None = None) -> int:
92
+ self.log()
93
+ if extra_log:
94
+ logger.info(extra_log)
95
+ self.write_json(output)
96
+ return self.exit_code
97
+
98
+
99
+ def _fail_rows(failed: list[tuple[str, str]]) -> list[dict]:
100
+ return [{"id": n, "error": e} for n, e in sorted(failed)]
101
+
102
+
103
+ def drift_report(result: DriftResult, diff_text: str | None = None) -> Report:
104
+ r = Report(headline="drift report")
105
+ r.add(Buckets.Drift.DETECTED, result.drifted)
106
+ r.add(Buckets.Drift.MISSING_IN_GIT, result.missing_in_git)
107
+ r.add(Buckets.Drift.MISSING_IN_FAAS, result.missing_in_faas)
108
+ r.add(Buckets.FAILED, [])
109
+ return r
110
+
111
+
112
+ def apply_report(result: ApplyResult, dry_run: bool = False, domain: str | None = None) -> Report:
113
+ d = f"{domain} " if domain else ""
114
+ r = Report(headline=f"apply {d}report{' (dry-run)' if dry_run else ''}")
115
+ r.add(Buckets.APPLIED, result.applied)
116
+ r.add(Buckets.UNCHANGED, result.unchanged)
117
+ r.add(Buckets.SKIPPED, result.skipped)
118
+ r.add(Buckets.FAILED, _fail_rows(result.failed))
119
+ return r
120
+
121
+
122
+ def deploy_report(result: DeployResult) -> Report:
123
+ r = Report(headline="deploy report")
124
+ r.add(Buckets.DEPLOYED, result.deployed)
125
+ r.add(Buckets.SKIPPED, result.skipped)
126
+ r.add(Buckets.FAILED, _fail_rows(result.failed))
127
+ return r
128
+
129
+
130
+ def emit_drift(result: DriftResult, output: str | None = None) -> int:
131
+ return drift_report(result).emit(output, extra_log=result.diff_text or None)
132
+
133
+
134
+ def emit_apply(result: ApplyResult, output: str | None = None, dry_run: bool = False, domain: str | None = None) -> int:
135
+ return apply_report(result, dry_run, domain).emit(output)
136
+
137
+
138
+ def emit_deploy(result: DeployResult, output: str | None = None) -> int:
139
+ return deploy_report(result).emit(output)
140
+
141
+
142
+ def print_drift_report(result: DriftResult, output: str | None = None) -> int:
143
+ return emit_drift(result, output)
144
+
145
+
146
+ def print_apply_report(result: ApplyResult, dry_run: bool = False, output: str | None = None, domain: str | None = None) -> int:
147
+ return emit_apply(result, output, dry_run, domain)
148
+
149
+
150
+ def print_deploy_report(result: DeployResult, output: str | None = None) -> int:
151
+ return emit_deploy(result, output)