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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
use std::str::FromStr;

pub use clap::error::ErrorKind;
use clap::{Error, Parser, Subcommand, ValueEnum};

#[derive(Clone, Debug, ValueEnum)]
#[value(rename_all = "UPPER")]
pub enum RecordType {
    A,
    AAAA,
    CNAME,
    MX,
    NS,
    TXT,
    SRV,
}

#[derive(Clone, Debug, ValueEnum, PartialEq)]
pub enum OnError {
    Print,
    Exit,
}

#[derive(Clone, Debug, Parser)]
pub struct DnsEntry {
    pub domain: String,
    pub name: String,
    pub ttl: u32,
    pub r#type: RecordType,
    pub content: String,
}

#[cfg(feature = "propagation")]
#[derive(Debug, Subcommand)]
pub enum DnsCommand {
    AcmeValidationDelete { domain: String },
    AcmeValidationSet { domain: String, challenge: String },
    AcmeValidationCheck { domain: String, challenge: String },
    Delete(DnsEntry),
    Insert(DnsEntry),
    List { domain: String },
}

#[cfg(not(feature = "propagation"))]
#[derive(Debug, Subcommand)]
pub enum DnsCommand {
    AcmeValidationDelete { domain: String },
    AcmeValidationSet { domain: String, challenge: String },
    Delete(DnsEntry),
    Insert(DnsEntry),
    List { domain: String },
}

#[derive(Debug, Subcommand)]
pub enum DomainCommand {
    List,
    Item { domain: String },
}

#[derive(Debug, Subcommand)]
pub enum InvoiceCommand {
    List,
    Item { number: String },
    Pdf { number: String },
}

#[derive(Debug, Subcommand)]
pub enum ProductCommand {
    List,
    Elements { name: String },
}

#[derive(Debug, Subcommand)]
pub enum EmailBoxCommand {
    List {
        domain: String,
    },
    Item {
        domain: String,
        id: String,
    },
    Delete {
        domain: String,
        id: String,
    },
    Insert {
        domain: String,
        username: String,
        password: String,
    },
}

#[derive(Debug, Subcommand)]
pub enum EmailForwardCommand {
    List {
        domain: String,
    },
    Item {
        domain: String,
        id: String,
    },
    Delete {
        domain: String,
        id: String,
    },
    Insert {
        domain: String,
        local_part: String,
        forward_to: String,
    },
}

#[derive(Debug, Subcommand)]
pub enum VpsCommand {
    List,
    Item { name: String },
    Start { name: String },
    Stop { name: String },
    Reset { name: String },
    Lock { name: String },
    Unlock { name: String },
}

#[derive(Debug, Subcommand)]
pub enum SubCommand {
    AvailibilityZones,
    Comment {
        text: String,
    },
    #[command(subcommand)]
    Dns(DnsCommand),
    #[command(subcommand)]
    Domain(DomainCommand),
    #[command(subcommand)]
    EmailBox(EmailBoxCommand),
    #[command(subcommand)]
    EmailForward(EmailForwardCommand),
    #[command(subcommand)]
    Invoice(InvoiceCommand),
    Onerror {
        on_error: OnError,
    },
    Ping,
    #[command(subcommand)]
    Product(ProductCommand),
    Sleep {
        number_of_seconds: u64,
    },
    #[command(subcommand)]
    Vps(VpsCommand),
}

#[derive(Debug, Parser)]
#[command(multicall = true)]
pub struct TransipCommand {
    #[command(subcommand)]
    pub command: SubCommand,
}

fn command_line<S: AsRef<str>>(line: S) -> Result<Vec<String>, clap::Error> {
    if line.as_ref().trim_start().starts_with("#") {
        Ok(vec!["comment".to_owned(), line.as_ref().to_owned()])
    } else {
        shlex::split(line.as_ref()).ok_or(clap::Error::new(clap::error::ErrorKind::Format))
    }
}

impl FromStr for TransipCommand {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        command_line(s).and_then(TransipCommand::try_parse_from)
    }
}

#[cfg(test)]
mod tests {
    use std::io::{BufRead, BufReader};

    use super::{command_line, TransipCommand};
    use clap::Parser;

    const COMMANDS: &[u8] = include_bytes!("commands.txt");

    #[test]
    fn try_command_lines() {
        let lines = BufReader::new(COMMANDS).lines();
        for args_option in lines.map_while(Result::ok).map(command_line) {
            match args_option {
                Ok(args) => {
                    let result = TransipCommand::try_parse_from(args).unwrap();
                    println!("{:?}", &result.command);
                }
                Err(error) => eprintln!("Error parsing line: {error}"),
            }
        }
    }
}