trefoil-cli 0.1.0__tar.gz → 0.3.0__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: trefoil-cli
3
- Version: 0.1.0
3
+ Version: 0.3.0
4
4
  Summary: CLI for the trefoil agent server
5
5
  Requires-Python: >=3.10
6
6
  Requires-Dist: pyyaml>=6
@@ -13,6 +13,7 @@ directory of the file that references them.
13
13
 
14
14
  Usage: trefoil <domain> <command> [args]
15
15
 
16
+ trefoil init [DIR] [--example=NAME]
16
17
  trefoil config package path/to/config.yml [-o bundle.json]
17
18
  trefoil config sync [path/to/config.yml] [--server URL]
18
19
  trefoil agents list
@@ -28,6 +29,7 @@ Usage: trefoil <domain> <command> [args]
28
29
  import argparse
29
30
  import base64
30
31
  import getpass
32
+ import importlib.metadata
31
33
  import json
32
34
  import os
33
35
  import sys
@@ -40,6 +42,7 @@ from pathlib import Path
40
42
  import yaml
41
43
 
42
44
  DEFAULT_SERVER = "http://localhost:8080"
45
+ EXAMPLES_API = "https://api.github.com/repos/alto-se/trefoil/contents/examples"
43
46
 
44
47
 
45
48
  class Ref:
@@ -142,6 +145,39 @@ def cmd_package(args):
142
145
  print(out)
143
146
 
144
147
 
148
+ def example_files(url, prefix=""):
149
+ """Yields (relative path, download URL) for every file in an example folder on GitHub."""
150
+ with urllib.request.urlopen(url, timeout=30) as resp:
151
+ entries = json.loads(resp.read())
152
+ for e in entries:
153
+ if e["type"] == "dir":
154
+ yield from example_files(e["url"], prefix + e["name"] + "/")
155
+ elif e["type"] == "file":
156
+ yield prefix + e["name"], e["download_url"]
157
+
158
+
159
+ def cmd_init(args):
160
+ dest = args.dir or Path(args.example)
161
+ if dest.exists() and (not dest.is_dir() or any(dest.iterdir())):
162
+ sys.exit(f"trefoil: {dest} already exists and is not empty")
163
+ try:
164
+ files = list(example_files(f"{EXAMPLES_API}/{quote(args.example)}?ref=main"))
165
+ for rel, url in files:
166
+ with urllib.request.urlopen(url, timeout=30) as resp:
167
+ data = resp.read()
168
+ path = dest / rel
169
+ path.parent.mkdir(parents=True, exist_ok=True)
170
+ path.write_bytes(data)
171
+ except urllib.error.HTTPError as e:
172
+ if e.code == 404:
173
+ sys.exit(f"trefoil: no example named {args.example}")
174
+ sys.exit(f"trefoil: fetching example {args.example}: {e}")
175
+ except urllib.error.URLError as e:
176
+ sys.exit(f"trefoil: cannot fetch example {args.example}: {e.reason}")
177
+ print(f"created {dest} from example {args.example} ({len(files)} files)")
178
+ print(f"next: cd {dest} && trefoil config sync")
179
+
180
+
145
181
  def api_request(server, method, path, body=None, timeout=30, raw=False):
146
182
  """Calls the API and returns the decoded JSON, or the bytes with raw=True."""
147
183
  req = urllib.request.Request(server.rstrip("/") + path, data=body, method=method)
@@ -446,8 +482,16 @@ def print_table(header, rows, empty):
446
482
  print(" ".join(cell.ljust(w) for cell, w in zip(row, widths)).rstrip())
447
483
 
448
484
 
485
+ def cli_version():
486
+ try:
487
+ return importlib.metadata.version("trefoil-cli")
488
+ except importlib.metadata.PackageNotFoundError:
489
+ return "unknown"
490
+
491
+
449
492
  def main():
450
493
  parser = argparse.ArgumentParser(prog="trefoil", description="trefoil <domain> <command> [args]")
494
+ parser.add_argument("--version", action="version", version=f"%(prog)s {cli_version()}")
451
495
  domains = parser.add_subparsers(dest="domain", metavar="<domain>", required=True)
452
496
 
453
497
  server = argparse.ArgumentParser(add_help=False)
@@ -460,6 +504,12 @@ def main():
460
504
  return domains.add_parser(name, help=help_text).add_subparsers(
461
505
  dest="command", metavar="<command>", required=True)
462
506
 
507
+ init = domains.add_parser("init", help="scaffold a project from an example config")
508
+ init.add_argument("dir", type=Path, nargs="?", help="directory to create (default: the example's name)")
509
+ init.add_argument("--example", default="echo", metavar="NAME",
510
+ help="example under examples/ in github.com/alto-se/trefoil (default: echo)")
511
+ init.set_defaults(func=cmd_init)
512
+
463
513
  config = domain("config", "package and upload agent configs")
464
514
  package = config.add_parser("package", help=__doc__.splitlines()[0])
465
515
  package.add_argument("config", type=Path, help="path to config.yml")
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "trefoil-cli"
3
- version = "0.1.0"
3
+ version = "0.3.0"
4
4
  description = "CLI for the trefoil agent server"
5
5
  requires-python = ">=3.10"
6
6
  dependencies = ["pyyaml>=6"]
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: trefoil-cli
3
- Version: 0.1.0
3
+ Version: 0.3.0
4
4
  Summary: CLI for the trefoil agent server
5
5
  Requires-Python: >=3.10
6
6
  Requires-Dist: pyyaml>=6
File without changes