trefoil-cli 0.2.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.2.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
@@ -41,6 +42,7 @@ from pathlib import Path
41
42
  import yaml
42
43
 
43
44
  DEFAULT_SERVER = "http://localhost:8080"
45
+ EXAMPLES_API = "https://api.github.com/repos/alto-se/trefoil/contents/examples"
44
46
 
45
47
 
46
48
  class Ref:
@@ -143,6 +145,39 @@ def cmd_package(args):
143
145
  print(out)
144
146
 
145
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
+
146
181
  def api_request(server, method, path, body=None, timeout=30, raw=False):
147
182
  """Calls the API and returns the decoded JSON, or the bytes with raw=True."""
148
183
  req = urllib.request.Request(server.rstrip("/") + path, data=body, method=method)
@@ -469,6 +504,12 @@ def main():
469
504
  return domains.add_parser(name, help=help_text).add_subparsers(
470
505
  dest="command", metavar="<command>", required=True)
471
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
+
472
513
  config = domain("config", "package and upload agent configs")
473
514
  package = config.add_parser("package", help=__doc__.splitlines()[0])
474
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.2.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.2.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