vstask 0.3.0

Run VS Code task from the terminal
Documentation
use std::fs;
use tracing::info;
use tracing_subscriber::{EnvFilter, fmt, prelude::*};

use vstask::cli;
use vstask::tasks;

const TASKS_JSON_FILE_PATH: &str = ".vscode/tasks.json";

fn main() -> Result<(), Box<dyn std::error::Error>> {
    tracing_subscriber::registry()
        .with(fmt::layer())
        .with(EnvFilter::from_default_env())
        .init();

    let args = cli::parse();

    // Check if command is invoked from a vscode based project
    // Find tasks.json inside .vscode or vscode folder
    info!("Attempting to read task file: {}", TASKS_JSON_FILE_PATH);

    // Read the JSON file content
    let file_content = fs::read_to_string(TASKS_JSON_FILE_PATH)
        .map_err(|e| format!("Could not read file {}: {}", TASKS_JSON_FILE_PATH, e))?;

    // Parse the JSON content into the defined structures
    let tasks_file: tasks::TasksFile = json5::from_str(&file_content)
        .map_err(|e| format!("Failed to parse JSON with comments: {}", e))?;

    info!(
        "Successfully parsed tasks.json (Version: {})",
        tasks_file.version
    );

    if args.list {
        tasks::list_tasks(&tasks_file.tasks, args.json);
        return Ok(());
    }

    // Handle task execution
    if let Some(task_name) = args.task_name {
        if let Some(task) = tasks_file.tasks.iter().find(|t| t.label == task_name) {
            tasks::execute_task(task)?;
        } else {
            return Err(format!("Could not find a task labeled '{}'.", task_name).into());
        }
    } else {
        // No task name provided, show available tasks
        tasks::list_tasks(&tasks_file.tasks, args.json);
    }

    Ok(())
}