kilar 0.2.4

A powerful CLI tool for managing port processes - quickly find and terminate processes using specific ports
Documentation
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
use clap::{Parser, Subcommand};

#[derive(Parser)]
#[command(
    name = "kilar",
    about = "Port process management CLI tool",
    version,
    author,
    args_conflicts_with_subcommands = true,
    subcommand_help_heading = "Commands",
    help_template = "{before-help}{name} {version}\n{author-with-newline}{about-with-newline}\n{usage-heading} {usage}\n\n{all-args}{after-help}"
)]
pub struct Cli {
    #[command(subcommand)]
    pub command: Commands,

    #[arg(short, long, global = true, help = "Suppress output")]
    pub quiet: bool,

    #[arg(short, long, global = true, help = "Output in JSON format")]
    pub json: bool,

    #[arg(short = 'v', long, global = true, help = "Enable verbose output")]
    pub verbose: bool,
}

#[derive(Subcommand)]
pub enum Commands {
    #[command(about = "Check port usage status")]
    Check {
        #[arg(help = "Port number to check")]
        port: u16,

        #[arg(short, long, default_value = "tcp", help = "Protocol (tcp/udp)")]
        protocol: String,

        #[arg(short, long, help = "Enable interactive mode with kill option")]
        interactive: bool,
    },

    #[command(about = "Kill process using specified port")]
    Kill {
        #[arg(help = "Port number used by the process to kill")]
        port: u16,

        #[arg(short, long, help = "Force kill without confirmation")]
        force: bool,

        #[arg(short, long, default_value = "tcp", help = "Protocol (tcp/udp)")]
        protocol: String,
    },

    #[command(about = "List ports in use")]
    List {
        #[arg(short = 'r', long, help = "Port range to filter (e.g., 3000-4000)")]
        ports: Option<String>,

        #[arg(short, long, help = "Filter by process name")]
        filter: Option<String>,

        #[arg(
            short,
            long,
            default_value = "port",
            help = "Sort order (port/pid/name)"
        )]
        sort: String,

        #[arg(short, long, default_value = "tcp", help = "Protocol (tcp/udp/all)")]
        protocol: String,

        #[arg(long, help = "View only (no kill feature)")]
        view_only: bool,

        #[arg(long, help = "Watch mode - continuously monitor port changes")]
        watch: bool,
    },
}

impl Cli {
    pub fn parse_args() -> Self {
        Self::parse()
    }
}

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

    #[test]
    fn test_cli_structure() {
        // CLI構造体の基本構造をテスト
        let cli =
            Cli::try_parse_from(["kilar", "check", "3000"]).expect("Failed to parse check command");

        assert!(!cli.quiet);
        assert!(!cli.json);
        assert!(!cli.verbose);

        match cli.command {
            Commands::Check {
                port,
                protocol,
                interactive,
            } => {
                assert_eq!(port, 3000);
                assert_eq!(protocol, "tcp");
                assert!(!interactive);
            }
            _ => panic!("Expected Check command"),
        }
    }

    #[test]
    fn test_check_command_parsing() {
        // Check コマンドのパースをテスト
        let test_cases = vec![
            (vec!["kilar", "check", "8080"], 8080, "tcp", false),
            (
                vec!["kilar", "check", "3000", "--protocol", "udp"],
                3000,
                "udp",
                false,
            ),
            (
                vec!["kilar", "check", "5000", "--interactive"],
                5000,
                "tcp",
                true,
            ),
            (
                vec!["kilar", "check", "9000", "-p", "tcp", "-i"],
                9000,
                "tcp",
                true,
            ),
        ];

        for (args, expected_port, expected_protocol, expected_interactive) in test_cases {
            let cli = Cli::try_parse_from(&args)
                .unwrap_or_else(|_| panic!("Failed to parse: {:?}", args));

            match cli.command {
                Commands::Check {
                    port,
                    protocol,
                    interactive,
                } => {
                    assert_eq!(port, expected_port, "Port mismatch for args: {:?}", args);
                    assert_eq!(
                        protocol, expected_protocol,
                        "Protocol mismatch for args: {:?}",
                        args
                    );
                    assert_eq!(
                        interactive, expected_interactive,
                        "Interactive mismatch for args: {:?}",
                        args
                    );
                }
                _ => panic!("Expected Check command for args: {:?}", args),
            }
        }
    }

    #[test]
    fn test_kill_command_parsing() {
        // Kill コマンドのパースをテスト
        let test_cases = vec![
            (vec!["kilar", "kill", "8080"], 8080, "tcp", false),
            (
                vec!["kilar", "kill", "3000", "--protocol", "udp"],
                3000,
                "udp",
                false,
            ),
            (vec!["kilar", "kill", "5000", "--force"], 5000, "tcp", true),
            (
                vec!["kilar", "kill", "9000", "-p", "tcp", "-f"],
                9000,
                "tcp",
                true,
            ),
        ];

        for (args, expected_port, expected_protocol, expected_force) in test_cases {
            let cli = Cli::try_parse_from(&args)
                .unwrap_or_else(|_| panic!("Failed to parse: {:?}", args));

            match cli.command {
                Commands::Kill {
                    port,
                    protocol,
                    force,
                } => {
                    assert_eq!(port, expected_port, "Port mismatch for args: {:?}", args);
                    assert_eq!(
                        protocol, expected_protocol,
                        "Protocol mismatch for args: {:?}",
                        args
                    );
                    assert_eq!(force, expected_force, "Force mismatch for args: {:?}", args);
                }
                _ => panic!("Expected Kill command for args: {:?}", args),
            }
        }
    }

    #[test]
    fn test_list_command_parsing() {
        // List コマンドのパースをテスト
        let cli = Cli::try_parse_from(["kilar", "list"]).expect("Failed to parse list command");

        match cli.command {
            Commands::List {
                ports,
                filter,
                sort,
                protocol,
                view_only,
                watch,
            } => {
                assert_eq!(ports, None);
                assert_eq!(filter, None);
                assert_eq!(sort, "port");
                assert_eq!(protocol, "tcp");
                assert!(!view_only);
                assert!(!watch);
            }
            _ => panic!("Expected List command"),
        }
    }

    #[test]
    fn test_list_command_with_options() {
        let cli = Cli::try_parse_from([
            "kilar",
            "list",
            "--ports",
            "3000-4000",
            "--filter",
            "node",
            "--sort",
            "pid",
            "--protocol",
            "udp",
            "--view-only",
            "--watch",
        ])
        .expect("Failed to parse list command with options");

        match cli.command {
            Commands::List {
                ports,
                filter,
                sort,
                protocol,
                view_only,
                watch,
            } => {
                assert_eq!(ports, Some("3000-4000".to_string()));
                assert_eq!(filter, Some("node".to_string()));
                assert_eq!(sort, "pid");
                assert_eq!(protocol, "udp");
                assert!(view_only);
                assert!(watch);
            }
            _ => panic!("Expected List command"),
        }
    }

    #[test]
    fn test_global_flags() {
        // グローバルフラグのテスト
        let test_cases = vec![
            (
                vec!["kilar", "check", "3000", "--quiet"],
                true,
                false,
                false,
            ),
            (vec!["kilar", "check", "3000", "--json"], false, true, false),
            (
                vec!["kilar", "check", "3000", "--verbose"],
                false,
                false,
                true,
            ),
            (vec!["kilar", "check", "3000", "-q"], true, false, false),
            (vec!["kilar", "check", "3000", "-j"], false, true, false),
            (vec!["kilar", "check", "3000", "-v"], false, false, true),
            (
                vec!["kilar", "check", "3000", "--quiet", "--json", "--verbose"],
                true,
                true,
                true,
            ),
        ];

        for (args, expected_quiet, expected_json, expected_verbose) in test_cases {
            let cli = Cli::try_parse_from(&args)
                .unwrap_or_else(|_| panic!("Failed to parse: {:?}", args));

            assert_eq!(
                cli.quiet, expected_quiet,
                "Quiet flag mismatch for args: {:?}",
                args
            );
            assert_eq!(
                cli.json, expected_json,
                "JSON flag mismatch for args: {:?}",
                args
            );
            assert_eq!(
                cli.verbose, expected_verbose,
                "Verbose flag mismatch for args: {:?}",
                args
            );
        }
    }

    #[test]
    fn test_port_range_validation() {
        // 有効なポート番号の範囲をテスト
        let valid_ports = [1, 80, 443, 3000, 8080, 65535];

        for port in valid_ports {
            let port_str = port.to_string();
            let args = vec!["kilar", "check", &port_str];
            let result = Cli::try_parse_from(&args);
            assert!(result.is_ok(), "Port {} should be valid", port);

            if let Ok(cli) = result {
                match cli.command {
                    Commands::Check {
                        port: parsed_port, ..
                    } => {
                        assert_eq!(parsed_port, port);
                    }
                    _ => panic!("Expected Check command"),
                }
            }
        }
    }

    #[test]
    fn test_invalid_port_numbers() {
        // 無効なポート番号のテスト(u16の範囲外や文字列)
        let invalid_ports = ["65536", "-1", "abc", ""];

        for invalid_port in invalid_ports {
            let args = vec!["kilar", "check", invalid_port];
            let result = Cli::try_parse_from(&args);
            assert!(result.is_err(), "Port '{}' should be invalid", invalid_port);
        }
    }

    #[test]
    fn test_protocol_values() {
        // プロトコル値のテスト(バリデーションは後で行われるので、文字列として受け入れられる)
        let protocols = ["tcp", "udp", "all", "invalid"];

        for protocol in protocols {
            let args = vec!["kilar", "check", "3000", "--protocol", protocol];
            let cli = Cli::try_parse_from(&args)
                .unwrap_or_else(|_| panic!("Failed to parse protocol: {}", protocol));

            match cli.command {
                Commands::Check {
                    protocol: parsed_protocol,
                    ..
                } => {
                    assert_eq!(parsed_protocol, protocol);
                }
                _ => panic!("Expected Check command"),
            }
        }
    }

    #[test]
    fn test_sort_values() {
        // ソートオプションのテスト(バリデーションは後で行われるので、文字列として受け入れられる)
        let sorts = ["port", "pid", "name", "invalid"];

        for sort in sorts {
            let args = vec!["kilar", "list", "--sort", sort];
            let cli = Cli::try_parse_from(&args)
                .unwrap_or_else(|_| panic!("Failed to parse sort: {}", sort));

            match cli.command {
                Commands::List {
                    sort: parsed_sort, ..
                } => {
                    assert_eq!(parsed_sort, sort);
                }
                _ => panic!("Expected List command"),
            }
        }
    }

    #[test]
    fn test_help_output() {
        // ヘルプ出力が正常に生成されることを確認
        let mut cmd = Cli::command();
        let help = cmd.render_help();
        let help_str = help.to_string();

        // 基本的な内容が含まれていることを確認
        assert!(help_str.contains("kilar"));
        assert!(help_str.contains("Port process management CLI tool"));
        assert!(help_str.contains("check"));
        assert!(help_str.contains("kill"));
        assert!(help_str.contains("list"));
    }

    #[test]
    fn test_version_output() {
        // バージョン情報のテスト
        let cmd = Cli::command();
        let version = cmd.get_version();

        // バージョンが設定されていることを確認
        assert!(version.is_some());
    }

    #[test]
    fn test_missing_required_arguments() {
        // 必須引数が不足している場合のテスト
        let invalid_args = vec![
            vec!["kilar", "check"], // ポート番号が不足
            vec!["kilar", "kill"],  // ポート番号が不足
            vec!["kilar"],          // サブコマンドが不足
        ];

        for args in invalid_args {
            let result = Cli::try_parse_from(&args);
            assert!(result.is_err(), "Args {:?} should be invalid", args);
        }
    }

    #[test]
    fn test_command_aliases() {
        // 短縮オプションのテスト
        let cli = Cli::try_parse_from(["kilar", "check", "3000", "-p", "udp", "-i"])
            .expect("Failed to parse with short options");

        match cli.command {
            Commands::Check {
                port,
                protocol,
                interactive,
            } => {
                assert_eq!(port, 3000);
                assert_eq!(protocol, "udp");
                assert!(interactive);
            }
            _ => panic!("Expected Check command"),
        }
    }

    #[test]
    fn test_default_values() {
        // デフォルト値のテスト
        let cli = Cli::try_parse_from(["kilar", "check", "3000"]).expect("Failed to parse");

        match cli.command {
            Commands::Check {
                protocol,
                interactive,
                ..
            } => {
                assert_eq!(protocol, "tcp"); // デフォルトプロトコル
                assert!(!interactive); // デフォルトでインタラクティブでない
            }
            _ => panic!("Expected Check command"),
        }

        let cli = Cli::try_parse_from(["kilar", "list"]).expect("Failed to parse");

        match cli.command {
            Commands::List { sort, protocol, .. } => {
                assert_eq!(sort, "port"); // デフォルトソート
                assert_eq!(protocol, "tcp"); // デフォルトプロトコル
            }
            _ => panic!("Expected List command"),
        }
    }

    #[test]
    fn test_cli_parse_args_method() {
        // parse_args メソッドのテスト(実際のコマンドライン引数をテストできないため、構造テスト)
        // この関数は実際のstd::env::args()を使用するため、単体テストでは直接テストできない
        // しかし、メソッドが存在することは確認できる

        // メソッドが存在し、正しいシグネチャを持つことを確認
        fn _test_parse_args_signature(_: fn() -> Cli) {}
        _test_parse_args_signature(Cli::parse_args);
    }
}