openjd-cli 0.2.0

Open Job Description CLI — validate, summarize, and run job templates
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// Copyright by contributors to this project.
// SPDX-License-Identifier: (Apache-2.0 OR MIT)

//! `openjd check` command — validate a template file.

use clap::Args;
use openjd_model::template::parse;
use std::path::PathBuf;

#[derive(Args)]
pub struct CheckArgs {
    /// Path to the template file
    pub path: PathBuf,

    /// Extensions to support (comma-separated or repeated). Empty string disables all.
    #[arg(long = "extensions")]
    pub extensions: Option<String>,
}

pub fn execute(args: CheckArgs) -> Result<(), Box<dyn std::error::Error>> {
    let path = &args.path;
    let content = crate::common::read_input_file(path)?;
    let template_value = parse::document_string_to_object(
        &content,
        crate::common::document_type(path),
        &crate::common::caller_limits(),
    )?;

    let version_str = template_value
        .get("specificationVersion")
        .and_then(|v| v.as_str())
        .ok_or("Missing field 'specificationVersion'")?;

    let exts = crate::common::parse_extensions(&args.extensions)?;
    let supported: Vec<&str> = exts.iter().map(|s| s.as_str()).collect();

    let version = version_str.parse::<openjd_model::TemplateSpecificationVersion>();
    match version {
        Ok(v) if v.is_job_template() => {
            parse::decode_job_template(
                template_value.clone(),
                Some(&supported),
                &crate::common::caller_limits(),
            )?;
        }
        Ok(v) if v.is_environment_template() => {
            parse::decode_environment_template(
                template_value.clone(),
                Some(&supported),
                &crate::common::caller_limits(),
            )?;
        }
        Ok(_) | Err(_) => {
            return Err(format!("Unknown template 'specificationVersion' ({version_str}).").into());
        }
    }

    println!("Template at '{}' passes validation checks.", path.display());
    Ok(())
}