1use std::{
2 fmt::{Display, Formatter},
3 path::PathBuf,
4 str::FromStr,
5};
6
7use alpm_types::{
8 Architecture,
9 BuildDate,
10 BuildDirectory,
11 BuildEnvironmentOption,
12 BuildTool,
13 BuildToolVersion,
14 InstalledPackage,
15 Name,
16 PackageOption,
17 Packager,
18 StartDirectory,
19 Version,
20};
21use clap::{Args, Parser, Subcommand, ValueEnum};
22use strum::Display;
23
24use crate::{Error, schema::BuildInfoSchema};
25
26#[derive(Clone, Debug)]
31pub struct OutputFile(pub PathBuf);
32
33impl Default for OutputFile {
34 fn default() -> Self {
35 OutputFile(PathBuf::from(".BUILDINFO"))
36 }
37}
38
39impl Display for OutputFile {
40 fn fmt(&self, fmt: &mut Formatter) -> std::fmt::Result {
41 write!(fmt, "{}", self.0.display())
42 }
43}
44
45impl FromStr for OutputFile {
46 type Err = Error;
47
48 fn from_str(s: &str) -> Result<Self, Self::Err> {
49 Ok(OutputFile(PathBuf::from(s)))
50 }
51}
52
53#[derive(Clone, Debug, Parser)]
54#[command(about, author, name = "alpm-buildinfo", version)]
55pub struct Cli {
56 #[command(subcommand)]
57 pub command: Command,
58}
59
60#[allow(clippy::large_enum_variant)]
61#[derive(Clone, Debug, Subcommand)]
62pub enum Command {
63 #[command()]
69 Create {
70 #[command(subcommand)]
71 command: CreateCommand,
72 },
73
74 #[command()]
81 Validate {
82 #[command(flatten)]
83 args: ValidateArgs,
84 },
85
86 #[command()]
88 Format {
89 #[command(flatten)]
90 args: ValidateArgs,
91
92 #[arg(
94 short,
95 long,
96 value_name = "OUTPUT_FORMAT",
97 default_value_t = OutputFormat::Json
98 )]
99 output_format: OutputFormat,
100
101 #[arg(short, long)]
103 pretty: bool,
104 },
105}
106
107#[derive(Clone, Debug, Args)]
109pub struct ValidateArgs {
110 #[arg(short, long, value_name = "VERSION")]
114 pub schema: Option<BuildInfoSchema>,
115 #[arg(value_name = "FILE")]
117 pub file: Option<PathBuf>,
118}
119
120#[derive(Clone, Debug, Args)]
125pub struct V1CreateArgs {
126 #[arg(env = "BUILDINFO_BUILDDATE", long, value_name = "BUILDDATE")]
128 pub builddate: BuildDate,
129 #[arg(env = "BUILDINFO_BUILDDIR", long, value_name = "BUILDDIR")]
131 pub builddir: BuildDirectory,
132 #[arg(
134 env = "BUILDINFO_BUILDENV",
135 long,
136 value_delimiter = ' ',
137 value_name = "BUILDENV"
138 )]
139 pub buildenv: Vec<BuildEnvironmentOption>,
140 #[arg(
142 env = "BUILDINFO_INSTALLED",
143 long,
144 value_delimiter = ' ',
145 value_name = "INSTALLED"
146 )]
147 pub installed: Vec<InstalledPackage>,
148 #[arg(
150 env = "BUILDINFO_OPTIONS",
151 long,
152 value_delimiter = ' ',
153 value_name = "OPTIONS"
154 )]
155 pub options: Vec<PackageOption>,
156 #[arg(env = "BUILDINFO_PACKAGER", long, value_name = "PACKAGER")]
158 pub packager: Packager,
159 #[arg(env = "BUILDINFO_PKGARCH", long, value_name = "PKGARCH")]
161 pub pkgarch: Architecture,
162 #[arg(env = "BUILDINFO_PKGBASE", long, value_name = "PKGBASE")]
164 pub pkgbase: Name,
165 #[arg(
167 env = "BUILDINFO_PKGBUILD_SHA256SUM",
168 long,
169 value_name = "PKGBUILD_SHA256SUM"
170 )]
171 pub pkgbuild_sha256sum: String,
172 #[arg(env = "BUILDINFO_PKGNAME", long, value_name = "PKGNAME")]
174 pub pkgname: Name,
175 #[arg(env = "BUILDINFO_PKGVER", long, value_name = "PKGVER")]
177 pub pkgver: Version,
178 #[arg(default_value_t = OutputFile::default(), env = "BUILDINFO_OUTPUT_FILE", value_name = "FILE")]
180 pub output: OutputFile,
181}
182
183#[derive(Clone, Debug, Subcommand)]
189pub enum CreateCommand {
190 V1 {
192 #[command(flatten)]
193 args: V1CreateArgs,
194 },
195 V2 {
197 #[command(flatten)]
198 args: V1CreateArgs,
199
200 #[arg(env = "BUILDINFO_STARTDIR", long, value_name = "STARTDIR")]
202 startdir: StartDirectory,
203
204 #[arg(env = "BUILDINFO_BUILDTOOL", long, value_name = "BUILDTOOL")]
206 buildtool: BuildTool,
207
208 #[arg(env = "BUILDINFO_BUILDTOOLVER", long, value_name = "BUILDTOOLVER")]
210 buildtoolver: BuildToolVersion,
211 },
212}
213
214#[derive(Clone, Debug, Default, Display, ValueEnum)]
216#[non_exhaustive]
217pub enum OutputFormat {
218 #[default]
219 #[strum(to_string = "json")]
220 Json,
221}