Skip to main content

qualia_cli/handlers/
solid.rs

1use std::path::PathBuf;
2
3use crate::cli::SolidAction;
4
5/// Handles the subcommand action.
6pub async fn handle(action: SolidAction) {
7    match action {
8        SolidAction::Serve {
9            host,
10            port,
11            data_root,
12            public_base,
13            demo_oidc,
14            no_demo_oidc,
15        } => {
16            let data_root = data_root.unwrap_or_else(|| {
17                std::env::var("QUALIA_SOLID_POD_ROOT")
18                    .map(PathBuf::from)
19                    .unwrap_or_else(|_| std::env::temp_dir().join("qualia-solid-pod"))
20            });
21            let public_base = public_base.unwrap_or_else(|| format!("http://{host}:{port}"));
22            let cfg = qualia_solid_bridge::BridgeConfig {
23                listen: format!("{host}:{port}").parse().expect("invalid host:port"),
24                data_root,
25                public_base,
26                demo_oidc: demo_oidc && !no_demo_oidc,
27            };
28            qualia_solid_bridge::run_bridge(cfg).await;
29        }
30        SolidAction::Fetch { url, token, out } => {
31            match qualia_solid_bridge::fetch_resource(&url, token.as_deref()).await {
32                Ok(r) => {
33                    println!("status        : {}", r.status);
34                    println!("content-type  : {}", r.content_type);
35                    println!("quin_count    : {}", r.quin_count);
36                    println!("url           : {}", r.url);
37                    if let Some(path) = out {
38                        if let Err(e) = std::fs::write(&path, r.body.as_bytes()) {
39                            eprintln!("write {}: {e}", path.display());
40                        } else {
41                            println!("wrote         : {}", path.display());
42                        }
43                    } else {
44                        let preview: String = r.body.chars().take(800).collect();
45                        println!("--- body (preview) ---\n{preview}");
46                    }
47                }
48                Err(e) => eprintln!("solid fetch failed: {e}"),
49            }
50        }
51        SolidAction::Put {
52            url,
53            file,
54            content_type,
55            token,
56        } => match std::fs::read(&file) {
57            Ok(body) => {
58                match qualia_solid_bridge::put_resource(
59                    &url,
60                    &body,
61                    &content_type,
62                    token.as_deref(),
63                )
64                .await
65                {
66                    Ok(status) => println!("PUT ok status={status} url={url}"),
67                    Err(e) => eprintln!("solid put failed: {e}"),
68                }
69            }
70            Err(e) => eprintln!("read {}: {e}", file.display()),
71        },
72        SolidAction::Post {
73            container,
74            file,
75            content_type,
76            slug,
77            token,
78        } => match std::fs::read(&file) {
79            Ok(body) => {
80                match qualia_solid_bridge::post_to_container(
81                    &container,
82                    &body,
83                    &content_type,
84                    slug.as_deref(),
85                    token.as_deref(),
86                )
87                .await
88                {
89                    Ok((status, loc)) => {
90                        println!("POST ok status={status}");
91                        if let Some(l) = loc {
92                            println!("location={l}");
93                        }
94                    }
95                    Err(e) => eprintln!("solid post failed: {e}"),
96                }
97            }
98            Err(e) => eprintln!("read {}: {e}", file.display()),
99        },
100    }
101}