@sigit / si Mirror · read-only
Fork 0

Mirror of github.com/getsigit/si.

Mirrored from https://github.com/getsigit/si.git

Synced Nov 24, 09:30 UTC

1 use {
2 crate::{
3 cli::SiteCommand,
4 context::Ctx,
5 ui::{self, prompt},
6 },
7 anyhow::{anyhow, Result},
8 sigit_si_api::models::Site,
9 std::time::Duration,
10 };
11
12 /// How often `publish --wait` asks, and how many times: about 90 seconds.
13 const POLL_INTERVAL: Duration = Duration::from_secs(2);
14 const POLL_ATTEMPTS: u32 = 45;
15
16 pub async fn run(ctx: &Ctx, command: SiteCommand) -> Result<()> {
17 ctx.require_token()?;
18
19 match command {
20 SiteCommand::View { repo } => view(ctx, repo).await,
21 SiteCommand::Publish { wait, repo } => publish(ctx, wait, repo).await,
22 SiteCommand::Unpublish { repo, yes } => unpublish(ctx, repo, yes).await,
23 }
24 }
25
26 async fn view(ctx: &Ctx, repo: Option<String>) -> Result<()> {
27 let slug = ctx.resolve_repo(repo)?;
28 let site = ctx.client.site(&slug.owner, &slug.name).await?;
29 ui::emit(ctx.json, &site, ui::render::site)
30 }
31
32 async fn publish(ctx: &Ctx, wait: bool, repo: Option<String>) -> Result<()> {
33 let slug = ctx.resolve_repo(repo)?;
34 let mut site = ctx.client.publish_site(&slug.owner, &slug.name).await?;
35
36 if wait {
37 ui::note("Publishing…");
38 for _ in 0..POLL_ATTEMPTS {
39 if is_settled(&site) {
40 break;
41 }
42 tokio::time::sleep(POLL_INTERVAL).await;
43 site = ctx.client.site(&slug.owner, &slug.name).await?;
44 }
45 if !is_settled(&site) {
46 return Err(anyhow!(
47 "The site is still publishing. Check on it with `si site view`."
48 ));
49 }
50 }
51
52 let failed = site.state == "failed";
53 ui::emit(ctx.json, &site, |site| {
54 if failed {
55 ui::render::site(site);
56 } else if site.state == "live" {
57 ui::success(format!("Published {}.", ui::highlight(&site.url)));
58 } else {
59 ui::success(format!("Publishing {}.", ui::highlight(&site.url)));
60 ui::note("Check on it with `si site view`.");
61 }
62 })?;
63
64 if failed {
65 return Err(anyhow!(
66 "Publishing failed: {}",
67 site.error.as_deref().unwrap_or("no reason given.")
68 ));
69 }
70 Ok(())
71 }
72
73 async fn unpublish(ctx: &Ctx, repo: Option<String>, yes: bool) -> Result<()> {
74 let slug = ctx.resolve_repo(repo)?;
75 if !yes && !prompt::confirm(&format!("Take the site for {slug} offline?"), false)? {
76 ui::note("Cancelled.");
77 return Ok(());
78 }
79
80 ctx.client.unpublish_site(&slug.owner, &slug.name).await?;
81 ui::success(format!("Taking the site for {slug} offline."));
82 Ok(())
83 }
84
85 fn is_settled(site: &Site) -> bool {
86 matches!(site.state.as_str(), "live" | "failed")
87 }
88
89 #[cfg(test)]
90 mod tests {
91 use super::*;
92
93 fn site(state: &str) -> Site {
94 serde_json::from_value(serde_json::json!({
95 "url": "https://demo.example",
96 "state": state,
97 "error": null,
98 "last_published_at": null,
99 "files": 0,
100 "bytes": 0,
101 }))
102 .unwrap()
103 }
104
105 #[test]
106 fn live_and_failed_end_the_wait() {
107 assert!(is_settled(&site("live")));
108 assert!(is_settled(&site("failed")));
109 }
110
111 #[test]
112 fn publishing_keeps_the_wait_going() {
113 assert!(!is_settled(&site("publishing")));
114 }
115 }