wasmer_cli/commands/domain/
zonefile.rs

1use crate::{commands::AsyncCliCommand, config::WasmerEnv, opts::ItemFormatOpts};
2use anyhow::Context;
3
4#[derive(clap::Parser, Debug)]
5/// Show a zone file
6pub struct CmdZoneFileGet {
7    #[clap(flatten)]
8    fmt: ItemFormatOpts,
9
10    #[clap(flatten)]
11    env: WasmerEnv,
12
13    /// Name of the domain.
14    domain_name: String,
15
16    /// output file name to store zone file
17    #[clap(short = 'o', long = "output", required = false)]
18    zone_file_path: Option<String>,
19}
20
21#[derive(clap::Parser, Debug)]
22/// Show a zone file
23pub struct CmdZoneFileSync {
24    #[clap(flatten)]
25    env: WasmerEnv,
26
27    /// filename of  zone-file to sync
28    zone_file_path: String,
29
30    /// Do not delete records that are not present in the zone file
31    #[clap(short = 'n', long = "no-delete-missing-records")]
32    no_delete_missing_records: bool,
33}
34
35#[async_trait::async_trait]
36impl AsyncCliCommand for CmdZoneFileGet {
37    type Output = ();
38
39    async fn run_async(self) -> Result<(), anyhow::Error> {
40        let client = self.env.client()?;
41        if let Some(domain) =
42            wasmer_backend_api::query::get_domain_zone_file(&client, self.domain_name).await?
43        {
44            let zone_file_contents = domain.zone_file;
45            if let Some(zone_file_path) = self.zone_file_path {
46                std::fs::write(zone_file_path, zone_file_contents)
47                    .context("Unable to write file")?;
48            } else {
49                println!("{zone_file_contents}");
50            }
51        } else {
52            anyhow::bail!("Domain not found");
53        }
54        Ok(())
55    }
56}
57
58#[async_trait::async_trait]
59impl AsyncCliCommand for CmdZoneFileSync {
60    type Output = ();
61
62    async fn run_async(self) -> Result<(), anyhow::Error> {
63        let data = std::fs::read(&self.zone_file_path).context("Unable to read file")?;
64        let zone_file_contents = String::from_utf8(data).context("Not a valid UTF-8 sequence")?;
65        let domain = wasmer_backend_api::query::upsert_domain_from_zone_file(
66            &self.env.client()?,
67            zone_file_contents,
68            !self.no_delete_missing_records,
69        )
70        .await?;
71        println!("Successfully synced domain: {}", domain.name);
72        Ok(())
73    }
74}