summaryrefslogtreecommitdiff
path: root/src/cli.rs
blob: 1b62cd977385764fa94f14dbf44584db99c128e4 (plain)
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
use std::path::PathBuf;

use anyhow::{bail, Result};
use clap::{Parser, Subcommand};

use crate::compare::compare;
use crate::manifest::ArtifactManifest;
use crate::{BuildPlan, ImageSpec};

#[derive(Debug, Parser)]
#[command(name = "alt-controller-image", about = "Native ALT image builder")]
pub struct Cli {
    #[command(subcommand)]
    pub command: Command,
}

#[derive(Debug, Subcommand)]
pub enum Command {
    /// Validate an image specification and render its build intent.
    Plan {
        #[arg(long)]
        spec: PathBuf,
    },
    /// Build an image from a specification.
    Build {
        #[arg(long)]
        spec: PathBuf,
        #[arg(long)]
        workspace: PathBuf,
        #[arg(long)]
        output: PathBuf,
    },
    /// Inspect a built artifact.
    Inspect {
        #[arg(long)]
        artifact: PathBuf,
    },
    /// Compare two built artifacts semantically.
    Compare {
        #[arg(long)]
        left: PathBuf,
        #[arg(long)]
        right: PathBuf,
    },
}

pub fn run(cli: Cli) -> Result<()> {
    match cli.command {
        Command::Plan { spec } => {
            let plan = BuildPlan::compile(ImageSpec::load(&spec)?)?;
            println!("target: {}", plan.spec().target.name);
            println!("architecture: {}", plan.spec().target.architecture.as_str());
            println!("format: tar");
            println!("stages:");
            for (index, stage) in plan.stages().iter().enumerate() {
                println!("  {}. {stage}", index + 1);
            }
            Ok(())
        }
        Command::Build { .. } => bail!("build execution is not available yet"),
        Command::Inspect { artifact } => {
            let manifest = ArtifactManifest::load(&artifact)?;
            println!("{}", toml::to_string_pretty(&manifest)?);
            Ok(())
        }
        Command::Compare { left, right } => {
            let left = ArtifactManifest::load(&left)?;
            let right = ArtifactManifest::load(&right)?;
            let report = compare(&left, &right);
            print!("{}", report.render());
            if report.is_equivalent() {
                Ok(())
            } else {
                bail!("artifacts differ semantically")
            }
        }
    }
}