1use std::process::ExitCode;
2
3use super::{load_draft, save_draft};
4use crate::api::Output;
5use crate::auth;
6
7pub struct HatchArgs {
8 pub id: Option<String>,
9 pub version: Option<String>,
10 pub name: Option<String>,
11 pub domain: Option<String>,
12 pub synopsis: Option<String>,
13 pub intent: Vec<String>,
14 pub mutations: Vec<String>,
15 pub license: Option<String>,
16}
17
18pub fn handle_hatch(out: &Output, args: HatchArgs) -> ExitCode {
19 let (spore_core_path, mut draft) = match load_draft() {
20 Ok(v) => v,
21 Err((code, msg)) => return out.error(&code, &msg),
22 };
23
24 if let Some(i) = args.id {
25 draft.id = i;
26 }
27 if let Some(v) = args.version {
28 draft.version = v;
29 }
30 if let Some(n) = args.name {
31 draft.name = n;
32 }
33 if let Some(d) = args.domain {
34 let site = crate::site::SiteDir::new(&d);
36 match auth::get_identity_with_site(&d, &site) {
37 Ok(info) => draft.key = info.public_key,
38 Err(e) => {
39 return out.error(
40 "identity_error",
41 &format!("Cannot resolve key for domain '{}': {}", d, e),
42 )
43 }
44 }
45 draft.domain = d;
46 }
47 if let Some(s) = args.synopsis {
48 draft.synopsis = s;
49 }
50 if !args.intent.is_empty() {
51 draft.intent = args.intent;
52 }
53 if !args.mutations.is_empty() {
54 draft.mutations = args.mutations;
55 }
56 if let Some(l) = args.license {
57 draft.license = l;
58 }
59
60 if let Err(e) = save_draft(&spore_core_path, &draft) {
61 return out.error_hypha(&e);
62 }
63
64 out.ok(&draft)
65}
66
67pub fn handle_tree_set(
68 out: &Output,
69 algorithm: Option<String>,
70 exclude_names: Option<Vec<String>>,
71 follow_rules: Option<Vec<String>>,
72) -> ExitCode {
73 let (spore_core_path, mut draft) = match load_draft() {
74 Ok(v) => v,
75 Err((code, msg)) => return out.error(&code, &msg),
76 };
77
78 if let Some(a) = algorithm {
79 draft.tree.algorithm = a;
80 }
81 if let Some(e) = exclude_names {
82 draft.tree.exclude_names = e;
83 }
84 if let Some(f) = follow_rules {
85 draft.tree.follow_rules = f;
86 }
87
88 if let Err(e) = save_draft(&spore_core_path, &draft) {
89 return out.error_hypha(&e);
90 }
91
92 out.ok(&draft.tree)
93}
94
95pub fn handle_tree_show(out: &Output) -> ExitCode {
96 let (_spore_core_path, draft) = match load_draft() {
97 Ok(v) => v,
98 Err((code, msg)) => return out.error(&code, &msg),
99 };
100
101 out.ok(&draft.tree)
102}