alpm_pkginfo/cli.rs
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
use std::{
fmt::{Display, Formatter},
path::PathBuf,
str::FromStr,
};
use alpm_types::{
Architecture,
Backup,
BuildDate,
ExtraData,
Group,
InstalledSize,
License,
Name,
OptionalDependency,
PackageDescription,
PackageRelation,
Packager,
Url,
Version,
};
use clap::{Args, Parser, Subcommand, ValueEnum};
use strum::Display;
use crate::{Error, RelationOrSoname};
/// A type wrapping a PathBuf with a default value
///
/// This type is used in circumstances where an output file is required that defaults to
/// ".PKGINFO"
#[derive(Clone, Debug)]
pub struct OutputFile(pub PathBuf);
impl Default for OutputFile {
fn default() -> Self {
OutputFile(PathBuf::from(".PKGINFO"))
}
}
impl Display for OutputFile {
fn fmt(&self, fmt: &mut Formatter) -> std::fmt::Result {
write!(fmt, "{}", self.0.display())
}
}
impl FromStr for OutputFile {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(OutputFile(PathBuf::from(s)))
}
}
#[derive(Clone, Debug, Parser)]
#[command(about, author, name = "alpm-pkginfo", version)]
pub struct Cli {
#[command(subcommand)]
pub command: Command,
}
#[allow(clippy::large_enum_variant)]
#[derive(Clone, Debug, Subcommand)]
pub enum Command {
#[command()]
/// Create a PKGINFO file according to a schema
///
/// If the input can be validated according to the schema, the program writes a valid PKGINFO
/// file and exits with no output and a return code of 0. If the input can not be validated
/// according to the schema, an error is emitted on stderr and the program exits with a
/// non-zero exit code.
Create {
#[command(subcommand)]
command: CreateCommand,
},
#[command()]
/// Validate a PKGINFO file
///
/// Validate a PKGINFO file according to a schema.
/// If the file can be validated, the program exits with no output and a return code of 0.
/// If the file can not be validated, an error is emitted on stderr and the program exits with
/// a non-zero exit code.
Validate {
/// An optional input file path to read from
///
/// If no file is specified, stdin is read from and expected to contain PKGINFO data to
/// validate.
#[arg(value_name = "FILE")]
file: Option<PathBuf>,
},
/// Parse a PKGINFO file and output it in a different file format
///
/// If the input can be validated according to a known schema, the program writes the PKGINFO
/// data to stdout in a different file format (optionally, a file path to write to may be
/// provided) and exits with a return code of 0. Currently only JSON is supported as output
/// format. If the input can not be validated according to a known schema, an error is
/// emitted on stderr and the program exits with a non-zero exit code.
#[command()]
Format {
/// An optional input file path to read from
///
/// If no file path is specified, stdin is read from and expected to contain PKGINFO data
/// to format.
#[arg(value_name = "FILE")]
file: Option<PathBuf>,
/// The output format to use
///
/// Currently only "json" (the default) is supported
#[arg(
short,
long,
value_name = "OUTPUT_FORMAT",
default_value_t = OutputFormat::Json
)]
output_format: OutputFormat,
/// Pretty-print the output
///
/// Has no effect if the output format can not be pretty printed.
#[arg(short, long)]
pretty: bool,
},
}
/// Arguments for creating a PKGINFO file according to the format version 1 schema
///
/// This struct is defined separately for reusing it for v1 and v2 because both share
/// a set of overlapping fields.
#[derive(Clone, Debug, Args)]
pub struct V1CreateArgs {
/// The pkgname to use in the PKGINFO
///
/// The pkgname must follow the alpm-package-name format (see `man 7 alpm-package-name`).
#[arg(env = "PKGINFO_PKGNAME", long, value_name = "PKGNAME")]
pub pkgname: Name,
/// The pkgbase to use in the PKGINFO
///
/// The pkgbase must follow the alpm-package-name format (see `man 7 alpm-package-name`).
#[arg(env = "PKGINFO_PKGBASE", long, value_name = "PKGBASE")]
pub pkgbase: Name,
/// The pkgver to use in the PKGINFO
///
/// The pkgver value must follow the alpm-pkgver format (see `man 7 alpm-pkgver`).
#[arg(env = "PKGINFO_PKGVER", long, value_name = "PKGVER")]
pub pkgver: Version,
/// The package description to use in the PKGINFO
///
/// The value must follow the format described in the PKGINFO format (see `man 5 PKGINFO`).
#[arg(env = "PKGINFO_PKGDESC", long, value_name = "PKGDESC")]
pub pkgdesc: PackageDescription,
/// Provide a url
#[arg(env = "PKGINFO_URL", long, value_name = "URL")]
pub url: Url,
/// Provide a builddate
#[arg(env = "PKGINFO_BUILDDATE", long, value_name = "BUILDDATE")]
pub builddate: BuildDate,
/// Provide a packager
#[arg(env = "PKGINFO_PACKAGER", long, value_name = "PACKAGER")]
pub packager: Packager,
/// Provide a size
#[arg(env = "PKGINFO_SIZE", long, value_name = "SIZE")]
pub size: InstalledSize,
/// Provide a architecture
#[arg(env = "PKGINFO_ARCH", long, value_name = "ARCH")]
pub arch: Architecture,
/// Provide one or more licenses
#[arg(
env = "PKGINFO_LICENSE",
value_delimiter = ' ',
long,
value_name = "LICENSE"
)]
pub license: Vec<License>,
/// Provide one or more replaces
#[arg(
env = "PKGINFO_REPLACES",
value_delimiter = ' ',
long,
value_name = "REPLACES"
)]
pub replaces: Vec<PackageRelation>,
/// Provide one or more groups
#[arg(
env = "PKGINFO_GROUP",
value_delimiter = ' ',
long,
value_name = "GROUP"
)]
pub group: Vec<Group>,
/// Provide one or more conflicts
#[arg(
env = "PKGINFO_CONFLICT",
value_delimiter = ' ',
long,
value_name = "CONFLICT"
)]
pub conflict: Vec<PackageRelation>,
/// Provide one or more provides
#[arg(
env = "PKGINFO_PROVIDES",
value_delimiter = ' ',
long,
value_name = "PROVIDES"
)]
pub provides: Vec<RelationOrSoname>,
/// Provide one or more backups
#[arg(
env = "PKGINFO_BACKUP",
value_delimiter = ' ',
long,
value_name = "BACKUP"
)]
pub backup: Vec<Backup>,
/// Provide one or more depends
#[arg(
env = "PKGINFO_DEPEND",
value_delimiter = ' ',
long,
value_name = "DEPEND"
)]
pub depend: Vec<RelationOrSoname>,
/// Provide one or more optdepend
#[arg(
env = "PKGINFO_OPTDEPEND",
value_delimiter = ',',
long,
value_name = "OPTDEPEND"
)]
pub optdepend: Vec<OptionalDependency>,
/// Provide one or more makedepend
#[arg(
env = "PKGINFO_MAKEDEPEND",
value_delimiter = ' ',
long,
value_name = "MAKEDEPEND"
)]
pub makedepend: Vec<PackageRelation>,
/// Provide one or more checkdepend
#[arg(
env = "PKGINFO_CHECKDEPEND",
value_delimiter = ' ',
long,
value_name = "CHECKDEPEND"
)]
pub checkdepend: Vec<PackageRelation>,
/// An optional custom file to write to
#[arg(default_value_t = OutputFile::default(), env = "PKGINFO_OUTPUT_FILE", value_name = "FILE")]
pub output: OutputFile,
}
/// Create an PKGINFO file according to a schema
///
/// If the input can be validated according to the schema, the program exits with no output and
/// a return code of 0. If the input can not be validated according to the schema, an error
/// is emitted on stderr and the program exits with a non-zero exit code.
#[derive(Clone, Debug, Subcommand)]
pub enum CreateCommand {
/// Create a PKGINFO version 1 file
V1 {
#[command(flatten)]
args: V1CreateArgs,
},
/// Create a PKGINFO version 2 file
V2 {
#[command(flatten)]
args: V1CreateArgs,
/// Provide one or more Xdata
#[arg(env = "PKGINFO_XDATA", long, value_name = "XDATA")]
xdata: Vec<ExtraData>,
},
}
/// Output format for the format command
#[derive(Clone, Debug, Default, Display, ValueEnum)]
#[non_exhaustive]
pub enum OutputFormat {
#[default]
#[strum(to_string = "json")]
Json,
}