composerize-np 0.2.0

Convert docker run commands to docker-compose files (YAML/JSON) and convert between formats
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
use crate::mappings::{get_mappings, strip_quotes, parse_key_value_list, is_boolean_flag, ArgType};
use indexmap::IndexMap;
use serde_yaml::Value;

pub fn parse_docker_command(input: &str) -> Result<(String, Vec<String>, IndexMap<String, Vec<String>>), String> {
    // Handle bash-style line continuation (backslash + newline)
    let cleaned = input
        .trim()
        .replace("\\\n", " ")  // Bash-style: \ + newline
        .replace("\\\r\n", " ") // Windows-style: \ + CRLF
        .replace('\n', " ")
        .split_whitespace()
        .collect::<Vec<_>>()
        .join(" ");

    // Remove docker/podman run/create
    let re = regex::Regex::new(r"^(?:docker|podman)\s+(?:run|create|container\s+run|service\s+create)\s+")
        .map_err(|e| format!("Regex error: {}", e))?;
    
    let cleaned = re.replace(&cleaned, "").to_string();
    
    let mut args: IndexMap<String, Vec<String>> = IndexMap::new();
    let mut positional = Vec::new();
    let mut tokens: Vec<String> = Vec::new();
    
    // Improved tokenizer with escaping support
    let mut current = String::new();
    let mut in_quotes = false;
    let mut quote_char = ' ';
    let mut chars = cleaned.chars().peekable();
    
    while let Some(ch) = chars.next() {
        match ch {
            '\\' => {
                // Escaping - add next character as is
                if let Some(next_ch) = chars.next() {
                    current.push(next_ch);
                }
            }
            '"' | '\'' if !in_quotes => {
                in_quotes = true;
                quote_char = ch;
                current.push(ch);
            }
            c if c == quote_char && in_quotes => {
                in_quotes = false;
                current.push(ch);
            }
            c if c.is_whitespace() && !in_quotes => {
                if !current.is_empty() {
                    tokens.push(current.clone());
                    current.clear();
                }
            }
            _ => {
                current.push(ch);
            }
        }
    }
    
    if !current.is_empty() {
        tokens.push(current);
    }
    
    let mut i = 0;
    
    while i < tokens.len() {
        let token = &tokens[i];
        
        if token.starts_with("--") {
            let flag_part = token.trim_start_matches("--");
            
            // Check for --flag=value format
            if flag_part.contains('=') {
                let parts: Vec<&str> = flag_part.splitn(2, '=').collect();
                if parts.len() == 2 {
                    args.entry(parts[0].to_string())
                        .or_insert_with(Vec::new)
                        .push(strip_quotes(parts[1]));
                }
                i += 1;
            } else if is_boolean_flag(flag_part) {
                // Boolean flag
                args.entry(flag_part.to_string())
                    .or_insert_with(Vec::new)
                    .push("true".to_string());
                i += 1;
            } else if i + 1 < tokens.len() && !tokens[i + 1].starts_with('-') {
                // Flag with value via space
                args.entry(flag_part.to_string())
                    .or_insert_with(Vec::new)
                    .push(strip_quotes(&tokens[i + 1]));
                i += 2;
            } else {
                // Unknown flag without value - treat as boolean
                args.entry(flag_part.to_string())
                    .or_insert_with(Vec::new)
                    .push("true".to_string());
                i += 1;
            }
        } else if token.starts_with('-') && token.len() > 1 && !token.chars().nth(1).unwrap().is_numeric() {
            let flags = token.trim_start_matches('-');
            
            // If it's a single character, check for value
            if flags.len() == 1 {
                if i + 1 < tokens.len() && !tokens[i + 1].starts_with('-') {
                    args.entry(flags.to_string())
                        .or_insert_with(Vec::new)
                        .push(strip_quotes(&tokens[i + 1]));
                    i += 2;
                } else {
                    args.entry(flags.to_string())
                        .or_insert_with(Vec::new)
                        .push("true".to_string());
                    i += 1;
                }
            } else {
                // Multiple boolean flags (e.g., -it)
                for flag_char in flags.chars() {
                    args.entry(flag_char.to_string())
                        .or_insert_with(Vec::new)
                        .push("true".to_string());
                }
                i += 1;
            }
        } else {
            // This is image or command
            positional.push(strip_quotes(token));
            i += 1;
            
            // Everything else is command
            while i < tokens.len() {
                positional.push(strip_quotes(&tokens[i]));
                i += 1;
            }
            break;
        }
    }
    
    let image = positional.first().ok_or("No image specified")?.clone();
    let command = positional.into_iter().skip(1).collect();
    
    Ok((image, command, args))
}

pub fn build_compose_value(
    args: &IndexMap<String, Vec<String>>,
    network: &str,
) -> Result<Value, String> {
    let mappings = get_mappings();
    let mut service = serde_yaml::Mapping::new();
    
    for (key, values) in args {
        if let Some(mapping) = mappings.get(key) {
            if mapping.path.is_empty() {
                continue; // Ignore (e.g., --rm, --detached)
            }
            
            for value in values {
                let path = mapping.path.replace("¤network¤", network);
                apply_mapping(&mut service, &path, value, &mapping.arg_type)?;
            }
        }
    }
    
    Ok(Value::Mapping(service))
}

fn apply_mapping(
    service: &mut serde_yaml::Mapping,
    path: &str,
    value: &str,
    arg_type: &ArgType,
) -> Result<(), String> {
    let parts: Vec<&str> = path.split('/').collect();
    
    match arg_type {
        ArgType::Array => {
            set_nested_array(service, &parts, value);
        }
        ArgType::Switch => {
            let bool_val = value == "true";
            set_nested_value(service, &parts, Value::Bool(bool_val));
        }
        ArgType::Value => {
            // Special handling for healthcheck test
            if parts.len() > 0 && parts[parts.len() - 1] == "test" && parts.contains(&"healthcheck") {
                // Convert to CMD-SHELL format
                let test_array = vec![
                    Value::String("CMD-SHELL".to_string()),
                    Value::String(value.to_string())
                ];
                set_nested_value(service, &parts, Value::Sequence(test_array));
            } else {
                set_nested_value(service, &parts, Value::String(value.to_string()));
            }
        }
        ArgType::IntValue => {
            let int_val = value.parse::<i64>()
                .map_err(|_| format!("Invalid integer: {}", value))?;
            set_nested_value(service, &parts, Value::Number(int_val.into()));
        }
        ArgType::FloatValue => {
            let float_val = value.parse::<f64>()
                .map_err(|_| format!("Invalid float: {}", value))?;
            set_nested_value(service, &parts, Value::Number(serde_yaml::Number::from(float_val)));
        }
        ArgType::Envs => {
            let env_value = if value.contains('=') {
                let parts: Vec<&str> = value.splitn(2, '=').collect();
                format!("{}={}", parts[0], strip_quotes(parts[1]))
            } else {
                value.to_string()
            };
            set_nested_array(service, &parts, &env_value);
        }
        ArgType::Map => {
            let map = parse_key_value_list(value, ',', '=');
            set_nested_map(service, &parts, map);
        }
        ArgType::MapArray => {
            // MapArray is an array of objects (e.g., for --mount)
            // Check mount type
            if value.starts_with("type=tmpfs") {
                // Convert mount format to docker run style for tmpfs
                let tmpfs_value = convert_mount_to_tmpfs(value);
                set_nested_array(service, &["tmpfs"], &tmpfs_value);
            } else if value.starts_with("type=bind") || value.starts_with("type=volume") {
                // Convert mount to short syntax for volumes
                let volume_value = convert_mount_to_volume(value);
                set_nested_array(service, &["volumes"], &volume_value);
            } else {
                // Regular mount (bind, volume) goes to volumes
                set_nested_array(service, &parts, value);
            }
        }
        ArgType::Networks => {
            if value.matches(|c| c == ':').count() == 0 
                && !["host", "bridge", "none"].contains(&value) 
                && !value.starts_with("container:") {
                // Named network
                let mut network_map = IndexMap::new();
                network_map.insert(
                    Value::String(value.to_string()),
                    Value::Mapping(serde_yaml::Mapping::new())
                );
                set_nested_value(service, &["networks"], Value::Mapping(network_map.into_iter().collect()));
            } else {
                // network_mode
                set_nested_value(service, &["network_mode"], Value::String(value.to_string()));
            }
        }
        ArgType::Ulimits => {
            parse_ulimit(service, &parts, value)?;
        }
        ArgType::Gpus => {
            parse_gpus(service, value)?;
        }
        ArgType::DeviceBlockIOConfigRate | ArgType::DeviceBlockIOConfigWeight => {
            // Эти типы обрабатываются в оригинале, но пока упрощаем
            set_nested_value(service, &parts, Value::String(value.to_string()));
        }
    }
    
    Ok(())
}

fn set_nested_value(map: &mut serde_yaml::Mapping, path: &[&str], value: Value) {
    if path.is_empty() {
        return;
    }
    
    if path.len() == 1 {
        map.insert(Value::String(path[0].to_string()), value);
        return;
    }
    
    let key = Value::String(path[0].to_string());
    let nested = map.entry(key.clone())
        .or_insert_with(|| Value::Mapping(serde_yaml::Mapping::new()));
    
    if let Value::Mapping(nested_map) = nested {
        set_nested_value(nested_map, &path[1..], value);
    }
}

fn set_nested_array(map: &mut serde_yaml::Mapping, path: &[&str], value: &str) {
    if path.is_empty() {
        return;
    }
    
    if path.len() == 1 {
        let key = Value::String(path[0].to_string());
        let arr = map.entry(key.clone())
            .or_insert_with(|| Value::Sequence(Vec::new()));
        
        if let Value::Sequence(seq) = arr {
            seq.push(Value::String(value.to_string()));
        }
        return;
    }
    
    let key = Value::String(path[0].to_string());
    let nested = map.entry(key.clone())
        .or_insert_with(|| Value::Mapping(serde_yaml::Mapping::new()));
    
    if let Value::Mapping(nested_map) = nested {
        set_nested_array(nested_map, &path[1..], value);
    }
}

fn set_nested_map(map: &mut serde_yaml::Mapping, path: &[&str], value: IndexMap<String, Value>) {
    if path.is_empty() {
        return;
    }
    
    if path.len() == 1 {
        let key = Value::String(path[0].to_string());
        let existing = map.entry(key.clone())
            .or_insert_with(|| Value::Mapping(serde_yaml::Mapping::new()));
        
        if let Value::Mapping(existing_map) = existing {
            for (k, v) in value {
                existing_map.insert(Value::String(k), v);
            }
        }
        return;
    }
    
    let key = Value::String(path[0].to_string());
    let nested = map.entry(key.clone())
        .or_insert_with(|| Value::Mapping(serde_yaml::Mapping::new()));
    
    if let Value::Mapping(nested_map) = nested {
        set_nested_map(nested_map, &path[1..], value);
    }
}

fn parse_ulimit(map: &mut serde_yaml::Mapping, path: &[&str], value: &str) -> Result<(), String> {
    let parts: Vec<&str> = value.splitn(2, '=').collect();
    if parts.len() != 2 {
        return Err(format!("Invalid ulimit format: {}", value));
    }
    
    let limit_name = parts[0];
    let limit_value = parts[1];
    
    let full_path = format!("{}/{}", path.join("/"), limit_name);
    let full_parts: Vec<&str> = full_path.split('/').collect();
    
    if limit_value.contains(':') {
        let limits: Vec<&str> = limit_value.split(':').collect();
        if limits.len() == 2 {
            let soft = limits[0].parse::<i64>()
                .map_err(|_| format!("Invalid soft limit: {}", limits[0]))?;
            let hard = limits[1].parse::<i64>()
                .map_err(|_| format!("Invalid hard limit: {}", limits[1]))?;
            
            let mut limit_map = IndexMap::new();
            limit_map.insert("soft".to_string(), Value::Number(soft.into()));
            limit_map.insert("hard".to_string(), Value::Number(hard.into()));
            
            set_nested_map(map, &full_parts, limit_map);
        }
    } else {
        let limit = limit_value.parse::<i64>()
            .map_err(|_| format!("Invalid limit: {}", limit_value))?;
        set_nested_value(map, &full_parts, Value::Number(limit.into()));
    }
    
    Ok(())
}

fn convert_mount_to_tmpfs(mount_str: &str) -> String {
    // Converts --mount type=tmpfs,destination=/tmp,tmpfs-size=256m,tmpfs-mode=1777
    // to format /tmp:rw,noexec,nosuid,size=256m
    
    let mut destination = String::new();
    let mut options = Vec::new();
    
    for part in mount_str.split(',') {
        let kv: Vec<&str> = part.splitn(2, '=').collect();
        if kv.len() == 2 {
            match kv[0] {
                "destination" | "target" | "dst" => destination = kv[1].to_string(),
                "tmpfs-size" => options.push(format!("size={}", kv[1])),
                "tmpfs-mode" => {}, // Ignore mode, compose doesn't support it
                "type" => {}, // Skip type
                _ => {}
            }
        }
    }
    
    // Add standard security options
    options.insert(0, "rw".to_string());
    options.insert(1, "noexec".to_string());
    options.insert(2, "nosuid".to_string());
    
    format!("{}:{}", destination, options.join(","))
}

fn convert_mount_to_volume(mount_str: &str) -> String {
    // Parse --mount format: type=bind,source=/path,target=/path,readonly
    let mut source = String::new();
    let mut target = String::new();
    let mut readonly = false;
    
    for part in mount_str.split(',') {
        if let Some(value) = part.strip_prefix("source=") {
            source = value.to_string();
        } else if let Some(value) = part.strip_prefix("target=") {
            target = value.to_string();
        } else if let Some(value) = part.strip_prefix("destination=") {
            target = value.to_string();
        } else if part == "readonly" || part == "ro" {
            readonly = true;
        }
    }
    
    // Convert to short syntax: source:target[:ro]
    if !source.is_empty() && !target.is_empty() {
        if readonly {
            format!("{}:{}:ro", source, target)
        } else {
            format!("{}:{}", source, target)
        }
    } else {
        // If parsing failed, return as is
        mount_str.to_string()
    }
}

fn parse_gpus(map: &mut serde_yaml::Mapping, value: &str) -> Result<(), String> {
    let count_value = if value == "all" {
        Value::String("all".to_string())
    } else {
        Value::Number(value.parse::<i64>()
            .map_err(|_| format!("Invalid GPU count: {}", value))?.into())
    };
    
    let mut device = IndexMap::new();
    device.insert("driver".to_string(), Value::String("nvidia".to_string()));
    device.insert("count".to_string(), count_value);
    device.insert("capabilities".to_string(), Value::Sequence(vec![Value::String("gpu".to_string())]));
    
    let mut devices = IndexMap::new();
    devices.insert("devices".to_string(), Value::Sequence(vec![
        Value::Mapping(device.into_iter().map(|(k, v)| (Value::String(k), v)).collect())
    ]));
    
    let mut reservations = IndexMap::new();
    reservations.insert("reservations".to_string(), Value::Mapping(
        devices.into_iter().map(|(k, v)| (Value::String(k), v)).collect()
    ));
    
    let mut resources = IndexMap::new();
    resources.insert("resources".to_string(), Value::Mapping(
        reservations.into_iter().map(|(k, v)| (Value::String(k), v)).collect()
    ));
    
    set_nested_map(map, &["deploy"], resources);
    
    Ok(())
}