voirs-cli 0.1.0-rc.1

Command-line interface for VoiRS speech synthesis
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
501
502
503
504
505
506
507
508
509
510
511
512
//! Workflow automation command implementations.
//!
//! This module provides commands for managing and executing declarative workflows
//! for complex multi-step synthesis pipelines.

use crate::error::Result;
use crate::workflow::{
    ExecutionState, StateManager, Workflow, WorkflowEngine, WorkflowRegistry, WorkflowValidator,
};
use clap::Subcommand;
use std::path::PathBuf;

/// Workflow automation commands
#[derive(Subcommand)]
pub enum WorkflowCommands {
    /// Execute a workflow from a definition file
    Execute {
        /// Path to workflow definition file (YAML or JSON)
        workflow_file: PathBuf,

        /// Override workflow variables (key=value format)
        #[arg(short = 'v', long = "var", value_parser = parse_key_val)]
        variables: Vec<(String, String)>,

        /// Maximum number of parallel steps
        #[arg(long, default_value = "4")]
        max_parallel: usize,

        /// Resume from previous execution if available
        #[arg(long)]
        resume: bool,

        /// Storage directory for workflow state
        #[arg(long)]
        state_dir: Option<PathBuf>,
    },

    /// Validate a workflow definition without executing
    Validate {
        /// Path to workflow definition file (YAML or JSON)
        workflow_file: PathBuf,

        /// Show detailed validation results
        #[arg(long)]
        detailed: bool,

        /// Output format (text, json, yaml)
        #[arg(long, default_value = "text")]
        format: String,
    },

    /// List all registered workflows
    List {
        /// Workflow registry directory
        #[arg(long)]
        registry_dir: Option<PathBuf>,

        /// Show detailed information
        #[arg(long)]
        detailed: bool,
    },

    /// Show status of a workflow execution
    Status {
        /// Workflow name
        workflow_name: String,

        /// Storage directory for workflow state
        #[arg(long)]
        state_dir: Option<PathBuf>,

        /// Output format (text, json, yaml)
        #[arg(long, default_value = "text")]
        format: String,
    },

    /// Resume a failed or stopped workflow
    Resume {
        /// Workflow name to resume
        workflow_name: String,

        /// Storage directory for workflow state
        #[arg(long)]
        state_dir: Option<PathBuf>,

        /// Maximum number of parallel steps
        #[arg(long, default_value = "4")]
        max_parallel: usize,
    },

    /// Stop a running workflow
    Stop {
        /// Workflow name to stop
        workflow_name: String,

        /// Storage directory for workflow state
        #[arg(long)]
        state_dir: Option<PathBuf>,

        /// Force stop without saving state
        #[arg(long)]
        force: bool,
    },
}

/// Parse a key-value pair for variable overrides
fn parse_key_val(s: &str) -> std::result::Result<(String, String), String> {
    let pos = s
        .find('=')
        .ok_or_else(|| format!("Invalid KEY=value: no `=` found in `{}`", s))?;
    Ok((s[..pos].to_string(), s[pos + 1..].to_string()))
}

/// Execute a workflow from a definition file
pub async fn run_workflow_execute(
    workflow_file: PathBuf,
    variables: Vec<(String, String)>,
    max_parallel: usize,
    resume: bool,
    state_dir: Option<PathBuf>,
) -> Result<()> {
    println!("Loading workflow from: {}", workflow_file.display());

    // Load workflow definition
    let mut workflow = Workflow::load_from_file(&workflow_file).await?;

    // Apply variable overrides
    for (key, value) in variables {
        workflow
            .variables
            .insert(key.clone(), crate::workflow::Variable::String(value));
    }

    println!("Workflow: {}", workflow.metadata.name);
    println!("Version: {}", workflow.metadata.version);
    if !workflow.metadata.description.is_empty() {
        println!("Description: {}", workflow.metadata.description);
    }
    println!("Steps: {}", workflow.steps.len());
    println!();

    // Determine state directory
    let state_dir = state_dir.unwrap_or_else(|| {
        std::env::current_dir()
            .expect("current dir should be accessible")
            .join(".voirs")
            .join("workflow_state")
    });

    // Check if we should resume
    if resume {
        let state_manager = StateManager::new(state_dir.clone());
        if state_manager.exists(&workflow.metadata.name).await {
            println!("Resuming workflow from previous state...");
            println!();
        }
    }

    // Create workflow engine
    let engine = WorkflowEngine::new(state_dir, max_parallel);

    // Execute workflow
    println!("Executing workflow...");
    println!("─────────────────────────────────────────────────");

    let result = engine.execute(workflow).await?;

    println!("─────────────────────────────────────────────────");
    println!();
    println!("Workflow execution completed!");
    println!();
    println!("Statistics:");
    println!("  Total steps: {}", result.stats.total_steps);
    println!("  Successful: {}", result.stats.successful_steps);
    println!("  Failed: {}", result.stats.failed_steps);
    println!("  Skipped: {}", result.stats.skipped_steps);
    println!("  Total duration: {}ms", result.stats.total_duration_ms);
    println!(
        "  Average step duration: {}ms",
        result.stats.avg_step_duration_ms
    );
    println!("  Total retries: {}", result.stats.total_retries);
    println!(
        "  Success rate: {:.1}%",
        result.stats.success_rate() * 100.0
    );

    if result.stats.is_successful() {
        println!();
        println!("✓ All steps completed successfully");
    } else {
        println!();
        println!("✗ Some steps failed");
        return Err(crate::error::CliError::Workflow(
            "Workflow execution had failures".to_string(),
        ));
    }

    Ok(())
}

/// Validate a workflow definition
pub async fn run_workflow_validate(
    workflow_file: PathBuf,
    detailed: bool,
    format: String,
) -> Result<()> {
    println!("Validating workflow: {}", workflow_file.display());
    println!();

    // Load workflow definition
    let workflow = Workflow::load_from_file(&workflow_file).await?;

    // Create validator
    let validator = WorkflowValidator::new();

    // Validate workflow
    let result = validator.validate(&workflow)?;

    // Output results based on format
    match format.as_str() {
        "json" => {
            let json = serde_json::to_string_pretty(&result)?;
            println!("{}", json);
        }
        "yaml" => {
            let yaml = serde_yaml::to_string(&result).map_err(|e| {
                crate::error::CliError::SerializationError(format!(
                    "Failed to serialize to YAML: {}",
                    e
                ))
            })?;
            println!("{}", yaml);
        }
        _ => {
            // Text format
            if result.valid {
                println!("✓ Workflow validation passed");
            } else {
                println!("✗ Workflow validation failed");
            }
            println!();

            if result.has_errors() {
                println!("Errors:");
                for error in &result.errors {
                    println!("  - {}", error);
                }
                println!();
            }

            if result.has_warnings() {
                println!("Warnings:");
                for warning in &result.warnings {
                    println!("  - {}", warning);
                }
                println!();
            }

            if detailed && result.valid {
                println!("Workflow Details:");
                println!("  Name: {}", workflow.metadata.name);
                println!("  Version: {}", workflow.metadata.version);
                if !workflow.metadata.description.is_empty() {
                    println!("  Description: {}", workflow.metadata.description);
                }
                println!("  Steps: {}", workflow.steps.len());
                println!("  Variables: {}", workflow.variables.len());
                println!("  Max parallel: {}", workflow.config.max_parallel);
            }
        }
    }

    if !result.valid {
        return Err(crate::error::CliError::Workflow(
            "Workflow validation failed".to_string(),
        ));
    }

    Ok(())
}

/// List all registered workflows
pub async fn run_workflow_list(registry_dir: Option<PathBuf>, detailed: bool) -> Result<()> {
    // Determine registry directory
    let registry_dir = registry_dir.unwrap_or_else(|| {
        std::env::current_dir()
            .expect("current dir should be accessible")
            .join(".voirs")
            .join("workflows")
    });

    let registry = WorkflowRegistry::new(registry_dir.clone());

    // Load workflows from directory
    let count = registry.load_from_directory().await?;

    if count == 0 {
        println!("No workflows found in: {}", registry_dir.display());
        println!();
        println!("Create workflow definitions in this directory or specify a different path with --registry-dir");
        return Ok(());
    }

    println!("Registered workflows ({} found):", count);
    println!();

    let workflow_names = registry.list().await;

    for name in workflow_names {
        if let Some(workflow) = registry.get(&name).await {
            println!("{}", workflow.metadata.name);
            if detailed {
                println!("    Version: {}", workflow.metadata.version);
                if !workflow.metadata.description.is_empty() {
                    println!("    Description: {}", workflow.metadata.description);
                }
                println!("    Steps: {}", workflow.steps.len());
                println!("    Variables: {}", workflow.variables.len());
                println!();
            }
        }
    }

    Ok(())
}

/// Show status of a workflow execution
pub async fn run_workflow_status(
    workflow_name: String,
    state_dir: Option<PathBuf>,
    format: String,
) -> Result<()> {
    // Determine state directory
    let state_dir = state_dir.unwrap_or_else(|| {
        std::env::current_dir()
            .expect("current dir should be accessible")
            .join(".voirs")
            .join("workflow_state")
    });

    let state_manager = StateManager::new(state_dir);

    // Check if state exists
    if !state_manager.exists(&workflow_name).await {
        return Err(crate::error::CliError::Workflow(format!(
            "No state found for workflow: {}",
            workflow_name
        )));
    }

    // Load state
    let state = state_manager.load(&workflow_name).await?;

    // Output based on format
    match format.as_str() {
        "json" => {
            let json = serde_json::to_string_pretty(&state)?;
            println!("{}", json);
        }
        "yaml" => {
            let yaml = serde_yaml::to_string(&state).map_err(|e| {
                crate::error::CliError::SerializationError(format!(
                    "Failed to serialize to YAML: {}",
                    e
                ))
            })?;
            println!("{}", yaml);
        }
        _ => {
            // Text format
            println!("Workflow: {}", state.workflow_name);
            println!("State: {:?}", state.state);
            println!();

            println!("Progress:");
            println!("  Completed steps: {}", state.completed_steps.len());
            println!("  Skipped steps: {}", state.skipped_steps.len());
            if let Some(ref current) = state.current_step {
                println!("  Current step: {}", current);
            }
            println!("  Total retries: {}", state.total_retries);
            println!();

            println!("Variables: {}", state.variables.len());
            for (key, value) in &state.variables {
                println!("  {}: {:?}", key, value);
            }
            println!();

            println!(
                "Last updated: {}",
                state.last_updated.format("%Y-%m-%d %H:%M:%S UTC")
            );
            println!();

            if state.can_resume() {
                println!("✓ This workflow can be resumed");
            } else {
                println!(
                    "✗ This workflow cannot be resumed (state: {:?})",
                    state.state
                );
            }
        }
    }

    Ok(())
}

/// Resume a failed or stopped workflow
pub async fn run_workflow_resume(
    workflow_name: String,
    state_dir: Option<PathBuf>,
    max_parallel: usize,
) -> Result<()> {
    // Determine state directory
    let state_dir = state_dir.unwrap_or_else(|| {
        std::env::current_dir()
            .expect("current dir should be accessible")
            .join(".voirs")
            .join("workflow_state")
    });

    let state_manager = StateManager::new(state_dir.clone());

    // Check if state exists
    if !state_manager.exists(&workflow_name).await {
        return Err(crate::error::CliError::Workflow(format!(
            "No state found for workflow: {}",
            workflow_name
        )));
    }

    // Load state
    let state = state_manager.load(&workflow_name).await?;

    // Check if workflow can be resumed
    if !state.can_resume() {
        return Err(crate::error::CliError::Workflow(format!(
            "Workflow '{}' cannot be resumed (current state: {:?})",
            workflow_name, state.state
        )));
    }

    println!("Resuming workflow: {}", workflow_name);
    println!("Current state: {:?}", state.state);
    println!("Completed steps: {}", state.completed_steps.len());
    println!();

    // Note: Resume functionality would require loading the original workflow definition
    // and passing the existing state to the engine. This is a simplified version.
    println!("⚠ Resume functionality requires the original workflow definition file");
    println!("  Use: voirs workflow execute <workflow-file> --resume");

    Ok(())
}

/// Stop a running workflow
pub async fn run_workflow_stop(
    workflow_name: String,
    state_dir: Option<PathBuf>,
    force: bool,
) -> Result<()> {
    // Determine state directory
    let state_dir = state_dir.unwrap_or_else(|| {
        std::env::current_dir()
            .expect("current dir should be accessible")
            .join(".voirs")
            .join("workflow_state")
    });

    let state_manager = StateManager::new(state_dir);

    // Check if state exists
    if !state_manager.exists(&workflow_name).await {
        return Err(crate::error::CliError::Workflow(format!(
            "No state found for workflow: {}",
            workflow_name
        )));
    }

    // Load state
    let mut state = state_manager.load(&workflow_name).await?;

    // Check if workflow is running
    if state.state != ExecutionState::Running {
        println!(
            "Workflow '{}' is not running (state: {:?})",
            workflow_name, state.state
        );
        return Ok(());
    }

    if force {
        // Force stop - delete state
        state_manager.delete(&workflow_name).await?;
        println!(
            "✓ Workflow '{}' forcefully stopped (state deleted)",
            workflow_name
        );
    } else {
        // Graceful stop - mark as stopped
        state.state = ExecutionState::Stopped;
        state.last_updated = chrono::Utc::now();
        state_manager.save(&workflow_name, &state).await?;
        println!("✓ Workflow '{}' stopped gracefully", workflow_name);
        println!("  State saved for potential resume");
    }

    Ok(())
}