1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
use std::fs;
use std::path::{Path, PathBuf};
use std::process::exit;

use clap::{
    error::{ContextKind, ContextValue},
    ArgMatches, Command,
};
use duckscript::types::error::ScriptError;
use duckscript::types::runtime::Context;
use serde::{Deserialize, Serialize};

use crate::config::{Config, IdentityConfig};

pub fn match_subcommand_or_exit(command: Command<'static>) -> (String, ArgMatches) {
    let mut command_clone = command.clone();
    let result = command.try_get_matches();
    let args = match result {
        Ok(args) => args,
        Err(e) => match e.kind() {
            clap::ErrorKind::UnknownArgument => {
                e.exit();
            }
            clap::ErrorKind::UnrecognizedSubcommand => {
                e.exit();
            }
            clap::ErrorKind::InvalidSubcommand => {
                e.exit();
            }
            clap::ErrorKind::MissingSubcommand => {
                let cmd = e
                    .context()
                    .find_map(|c| match c {
                        (ContextKind::InvalidSubcommand, &ContextValue::String(ref cmd)) => {
                            Some(cmd.split_ascii_whitespace().last().unwrap())
                        }
                        _ => None,
                    })
                    .expect("The InvalidArg to be present in the context of UnknownArgument.");
                match command_clone.find_subcommand_mut(cmd) {
                    Some(subcmd) => subcmd.print_help().unwrap(),
                    None => command_clone.print_help().unwrap(),
                }
                exit(0);
            }
            _ => {
                e.exit();
            }
        },
    };
    let (cmd, subcommand_args) = args.subcommand().unwrap();
    (cmd.to_string(), subcommand_args.clone())
}

#[derive(Debug, Clone, Serialize, Deserialize)]
struct DNSResponse {
    address: String,
}

pub async fn spacetime_dns(config: &Config, domain_name: &str) -> Result<String, anyhow::Error> {
    let client = reqwest::Client::new();
    let url = format!("http://{}/database/dns/{}", config.host, domain_name);
    let res = client.get(url).send().await?;
    let res = res.error_for_status()?;
    let bytes = res.bytes().await.unwrap();

    let dns: DNSResponse = serde_json::from_slice(&bytes[..]).unwrap();
    Ok(dns.address)
}

pub fn find_wasm_file(project_path: &Path) -> Result<PathBuf, anyhow::Error> {
    let module_output_directory_path = project_path
        .join("target")
        .join("wasm32-unknown-unknown")
        .join("release");
    if !module_output_directory_path.exists() || !module_output_directory_path.is_dir() {
        return Err(anyhow::anyhow!(
            "Module output directory does not exist: {}",
            module_output_directory_path.to_str().unwrap()
        ));
    }

    for file in fs::read_dir(module_output_directory_path.to_str().unwrap()).unwrap() {
        match file {
            Ok(f) => {
                if f.file_name().to_str().unwrap().ends_with(".wasm") && f.path().is_file() {
                    return Ok(f.path());
                }
            }
            Err(_) => {}
        }
    }

    Err(anyhow::anyhow!(format!(
        "Unable to find wasm file in project path: {}",
        project_path.to_str().unwrap()
    )))
}

#[derive(Deserialize)]
pub struct IdentityTokenJson {
    pub identity: String,
    pub token: String,
}

pub enum InitDefaultResultType {
    Existing,
    SavedNew,
}

pub struct InitDefaultResult {
    pub identity_config: IdentityConfig,
    pub result_type: InitDefaultResultType,
}

pub async fn init_default(config: &mut Config, nickname: Option<String>) -> Result<InitDefaultResult, anyhow::Error> {
    if config.name_exists(nickname.as_ref().unwrap_or(&"".to_string())) {
        println!("An identity with that name already exists.");
        std::process::exit(0);
    }

    let client = reqwest::Client::new();
    let builder = client.post(format!("http://{}/identity", config.host));

    if let Some(identity_config) = config.get_default_identity_config() {
        return Ok(InitDefaultResult {
            identity_config: identity_config.clone(),
            result_type: InitDefaultResultType::Existing,
        });
    }

    let res = builder.send().await?;
    let res = res.error_for_status()?;

    let body = res.bytes().await?;
    let body = String::from_utf8(body.to_vec())?;

    let identity_token: IdentityTokenJson = serde_json::from_str(&body)?;

    let identity = identity_token.identity.clone();

    let identity_config = IdentityConfig {
        identity: identity_token.identity,
        token: identity_token.token,
        nickname: nickname.clone(),
        email: None,
    };
    config.identity_configs.push(identity_config.clone());
    if config.default_identity.is_none() {
        config.default_identity = Some(identity.clone());
    }
    config.save();
    Ok(InitDefaultResult {
        identity_config,
        result_type: InitDefaultResultType::SavedNew,
    })
}

pub fn invoke_duckscript(text: &str, context: Context) -> Result<Context, ScriptError> {
    let current_working_directory = std::env::current_dir();
    let result = duckscript::runner::run_script(text, context);
    std::env::set_current_dir(current_working_directory.unwrap()).unwrap();
    return result;
}