mockforge-cli 0.3.114

CLI interface for MockForge
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
use anyhow::Result;
use clap::Subcommand;
use mockforge_core::config::FtpConfig;
use mockforge_ftp::{
    FileContent, FileMetadata, FtpServer, GenerationPattern, VirtualFile, VirtualFileSystem,
};
use std::path::PathBuf;

/// FTP server management commands
#[derive(Subcommand)]
pub enum FtpCommands {
    /// Start FTP server
    ///
    /// Examples:
    ///   mockforge ftp serve --port 2121
    ///   mockforge ftp serve --config ftp-config.yaml
    #[command(verbatim_doc_comment)]
    Serve {
        /// FTP server port
        #[arg(short, long, default_value = "2121")]
        port: u16,

        /// FTP server host
        #[arg(long, default_value = "127.0.0.1")]
        host: String,

        /// Configuration file path
        #[arg(short, long)]
        config: Option<PathBuf>,

        /// Virtual root directory
        #[arg(long, default_value = "/")]
        virtual_root: String,
    },

    /// Manage FTP fixtures
    ///
    /// Examples:
    ///   mockforge ftp fixtures list
    ///   mockforge ftp fixtures load ./fixtures/ftp/
    ///   mockforge ftp fixtures validate fixture.yaml
    #[command(verbatim_doc_comment)]
    Fixtures {
        #[command(subcommand)]
        fixtures_command: FtpFixturesCommands,
    },

    /// Manage virtual file system
    ///
    /// Examples:
    ///   mockforge ftp vfs list /
    ///   mockforge ftp vfs add /test.txt --content "Hello World"
    ///   mockforge ftp vfs remove /test.txt
    #[command(verbatim_doc_comment)]
    Vfs {
        #[command(subcommand)]
        vfs_command: FtpVfsCommands,
    },
}

#[derive(Subcommand)]
pub enum FtpFixturesCommands {
    /// List all FTP fixtures
    List,

    /// Load fixtures from directory
    ///
    /// Example:
    ///   mockforge ftp fixtures load ./fixtures/ftp/
    Load {
        /// Directory containing fixture files
        directory: PathBuf,
    },

    /// Validate fixture file
    ///
    /// Example:
    ///   mockforge ftp fixtures validate fixture.yaml
    Validate {
        /// Fixture file to validate
        file: PathBuf,
    },
}

#[derive(Subcommand)]
pub enum FtpVfsCommands {
    /// List files in virtual directory
    ///
    /// Example:
    ///   mockforge ftp vfs list /
    List {
        /// Directory path to list
        path: String,
    },

    /// Add file to virtual file system
    ///
    /// Examples:
    ///   mockforge ftp vfs add /test.txt --content "Hello World"
    ///   mockforge ftp vfs add /data.bin --generate random --size 1024
    ///   mockforge ftp vfs add /template.txt --template "{{faker.name}}"
    #[command(verbatim_doc_comment)]
    Add {
        /// File path
        path: String,

        /// Static content
        #[arg(long, conflicts_with_all = ["template", "generate"])]
        content: Option<String>,

        /// Template content (Handlebars)
        #[arg(long, conflicts_with_all = ["content", "generate"])]
        template: Option<String>,

        /// Generate content
        #[arg(long, conflicts_with_all = ["content", "template"], value_enum)]
        generate: Option<GenerationType>,

        /// Size for generated content (in bytes)
        #[arg(long, requires = "generate")]
        size: Option<usize>,
    },

    /// Remove file from virtual file system
    ///
    /// Example:
    ///   mockforge ftp vfs remove /test.txt
    Remove {
        /// File path to remove
        path: String,
    },

    /// Get file information
    ///
    /// Example:
    ///   mockforge ftp vfs info /test.txt
    Info {
        /// File path
        path: String,
    },
}

#[derive(clap::ValueEnum, Clone)]
pub enum GenerationType {
    /// Generate random bytes
    Random,
    /// Generate all zeros
    Zeros,
    /// Generate all ones
    Ones,
    /// Generate incremental bytes (0, 1, 2, ...)
    Incremental,
}

/// Handle FTP commands
pub async fn handle_ftp_command(command: FtpCommands) -> Result<()> {
    match command {
        FtpCommands::Serve {
            port,
            host,
            config,
            virtual_root,
        } => handle_ftp_serve(port, host, config, virtual_root).await,
        FtpCommands::Fixtures { fixtures_command } => handle_ftp_fixtures(fixtures_command).await,
        FtpCommands::Vfs { vfs_command } => handle_ftp_vfs(vfs_command).await,
    }
}

async fn handle_ftp_serve(
    port: u16,
    host: String,
    _config: Option<PathBuf>,
    virtual_root: String,
) -> Result<()> {
    println!("Starting FTP server on {}:{}", host, port);

    let config = FtpConfig {
        host,
        port,
        virtual_root: virtual_root.into(),
        ..Default::default()
    };

    let server = FtpServer::new(config);
    server.start().await?;

    Ok(())
}

async fn handle_ftp_fixtures(command: FtpFixturesCommands) -> Result<()> {
    match command {
        FtpFixturesCommands::List => {
            let cwd = std::env::current_dir()?;
            let fixture_dirs = ["fixtures/ftp", "fixtures", "ftp-fixtures"];
            let mut found_any = false;

            println!("FTP fixtures:");
            for dir_name in &fixture_dirs {
                let dir = cwd.join(dir_name);
                if !dir.exists() {
                    continue;
                }
                for entry in std::fs::read_dir(&dir)?.flatten() {
                    let path = entry.path();
                    let ext = path.extension().and_then(|e| e.to_str()).unwrap_or("");
                    if matches!(ext, "yaml" | "yml") {
                        if let Ok(content) = std::fs::read_to_string(&path) {
                            match serde_yaml::from_str::<mockforge_ftp::FtpFixture>(&content) {
                                Ok(fixture) => {
                                    println!(
                                        "  {} - {} ({} files, {} upload rules)",
                                        fixture.identifier,
                                        fixture.name,
                                        fixture.virtual_files.len(),
                                        fixture.upload_rules.len()
                                    );
                                    found_any = true;
                                }
                                Err(_) => {
                                    // Not a valid FTP fixture, skip
                                }
                            }
                        }
                    }
                }
            }
            if !found_any {
                println!("  No fixtures found. Place YAML fixture files in fixtures/ftp/");
            }
        }
        FtpFixturesCommands::Load { directory } => {
            println!("Loading FTP fixtures from: {}", directory.display());
            if !directory.exists() {
                anyhow::bail!("Directory does not exist: {}", directory.display());
            }

            let mut loaded = 0;
            let mut errors = 0;
            for entry in std::fs::read_dir(&directory)?.flatten() {
                let path = entry.path();
                let ext = path.extension().and_then(|e| e.to_str()).unwrap_or("");
                if !matches!(ext, "yaml" | "yml") {
                    continue;
                }
                let content = std::fs::read_to_string(&path)?;
                match serde_yaml::from_str::<mockforge_ftp::FtpFixture>(&content) {
                    Ok(fixture) => {
                        println!(
                            "  Loaded: {} ({} files, {} upload rules)",
                            fixture.name,
                            fixture.virtual_files.len(),
                            fixture.upload_rules.len()
                        );
                        loaded += 1;
                    }
                    Err(e) => {
                        println!("  Error in {}: {}", path.display(), e);
                        errors += 1;
                    }
                }
            }
            println!("Loaded {} fixture(s), {} error(s)", loaded, errors);
        }
        FtpFixturesCommands::Validate { file } => {
            println!("Validating FTP fixture: {}", file.display());
            if !file.exists() {
                anyhow::bail!("File does not exist: {}", file.display());
            }
            let content = std::fs::read_to_string(&file)?;
            match serde_yaml::from_str::<mockforge_ftp::FtpFixture>(&content) {
                Ok(fixture) => {
                    println!("  Valid fixture: {}", fixture.name);
                    println!("  Identifier: {}", fixture.identifier);
                    println!(
                        "  Description: {}",
                        fixture.description.as_deref().unwrap_or("(none)")
                    );
                    println!("  Virtual files: {}", fixture.virtual_files.len());
                    for vf in &fixture.virtual_files {
                        println!("    - {} ({})", vf.path.display(), vf.permissions);
                    }
                    println!("  Upload rules: {}", fixture.upload_rules.len());
                    for rule in &fixture.upload_rules {
                        println!(
                            "    - pattern: {} (auto_accept: {})",
                            rule.path_pattern, rule.auto_accept
                        );
                    }
                }
                Err(e) => {
                    println!("  Invalid fixture: {}", e);
                    std::process::exit(1);
                }
            }
        }
    }
    Ok(())
}

async fn handle_ftp_vfs(command: FtpVfsCommands) -> Result<()> {
    let vfs = VirtualFileSystem::new(PathBuf::from("/"));

    match command {
        FtpVfsCommands::List { path } => {
            println!("Files in {}:", path);
            let files = vfs.list_files(PathBuf::from(path).as_path());
            for file in files {
                println!("  {} ({} bytes)", file.path.display(), file.metadata.size);
            }
        }
        FtpVfsCommands::Add {
            path,
            content,
            template,
            generate,
            size,
        } => {
            let file_content = if let Some(content) = content {
                FileContent::Static(content.into_bytes())
            } else if let Some(template) = template {
                FileContent::Template(template)
            } else if let Some(gen_type) = generate {
                let size = size.unwrap_or(1024);
                let pattern = match gen_type {
                    GenerationType::Random => GenerationPattern::Random,
                    GenerationType::Zeros => GenerationPattern::Zeros,
                    GenerationType::Ones => GenerationPattern::Ones,
                    GenerationType::Incremental => GenerationPattern::Incremental,
                };
                FileContent::Generated { size, pattern }
            } else {
                FileContent::Static(b"".to_vec())
            };

            let file = VirtualFile::new(
                PathBuf::from(path.clone()),
                file_content,
                FileMetadata::default(),
            );

            vfs.add_file(PathBuf::from(path), file)?;
            println!("File added to virtual file system");
        }
        FtpVfsCommands::Remove { path } => {
            if vfs.remove_file(PathBuf::from(path).as_path()).is_ok() {
                println!("File removed from virtual file system");
            } else {
                println!("File not found");
            }
        }
        FtpVfsCommands::Info { path } => {
            if let Some(file) = vfs.get_file(PathBuf::from(path).as_path()) {
                println!("File: {}", file.path.display());
                println!("Size: {} bytes", file.metadata.size);
                println!("Permissions: {}", file.metadata.permissions);
                println!("Owner: {}", file.metadata.owner);
                println!("Modified: {}", file.modified_at);
            } else {
                println!("File not found");
            }
        }
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_ftp_commands_serve_variant() {
        let _cmd = FtpCommands::Serve {
            port: 2121,
            host: "127.0.0.1".to_string(),
            config: None,
            virtual_root: "/".to_string(),
        };
    }

    #[test]
    fn test_ftp_fixtures_list_variant() {
        let _cmd = FtpFixturesCommands::List;
    }

    #[test]
    fn test_ftp_fixtures_load_variant() {
        let _cmd = FtpFixturesCommands::Load {
            directory: PathBuf::from("./fixtures/ftp"),
        };
    }

    #[test]
    fn test_ftp_fixtures_validate_variant() {
        let _cmd = FtpFixturesCommands::Validate {
            file: PathBuf::from("fixture.yaml"),
        };
    }

    #[test]
    fn test_ftp_vfs_list_variant() {
        let _cmd = FtpVfsCommands::List {
            path: "/".to_string(),
        };
    }
}