Skip to content

Commit

Permalink
feat(bin/write-rockspec): add some CLI flags
Browse files Browse the repository at this point in the history
  • Loading branch information
vhyrro committed Feb 12, 2024
1 parent f24e196 commit 14a054a
Showing 1 changed file with 73 additions and 38 deletions.
111 changes: 73 additions & 38 deletions rocks-bin/src/rockspec/write_rockspec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::str::FromStr;

use clap::Args;
use eyre::{eyre, Result};
use itertools::Itertools;
use itertools::{Either, Itertools};
use rustyline::error::ReadlineError;
use spdx::LicenseId;
use spinners::{Spinner, Spinners};
Expand Down Expand Up @@ -46,7 +46,26 @@ macro_rules! parse {
// there something that tells us it's a lua project?).

#[derive(Args)]
pub struct WriteRockspec {}
pub struct WriteRockspec {
/// The name to give to the rock.
#[arg(long)]
name: Option<String>,

/// The description of the rock.
#[arg(long)]
description: Option<String>,

/// The license of the rock. Generic license names will try to be inferred.
#[arg(long, value_parser = parse_license_wrapper)]
license: Option<Either<LicenseId, ()>>,
}

fn parse_license_wrapper(s: &str) -> std::result::Result<Either<LicenseId, ()>, String> {
match parse_license(s.to_string()) {
Ok(val) => Ok(val.map(Either::Left).unwrap_or(Either::Right(()))),
Err(err) => Err(err.to_string()),
}
}

fn identity(input: String) -> Result<String> {
Ok(input)
Expand Down Expand Up @@ -81,7 +100,7 @@ fn parse_version(input: String) -> Result<LuaDependency> {
LuaDependency::from_str(format!("lua {}", input).as_str())
}

pub async fn write_rockspec(_data: WriteRockspec) -> Result<()> {
pub async fn write_rockspec(data: WriteRockspec) -> Result<()> {
let mut spinner = Spinner::new(Spinners::Dots, "Fetching repository metadata...".into());

let repo_metadata = github_metadata::get_metadata_for(None)
Expand Down Expand Up @@ -111,43 +130,59 @@ pub async fn write_rockspec(_data: WriteRockspec) -> Result<()> {
// let mut stdout = BufferedStandardStream::stdout(ColorChoice::Always);
// stdout.set_color(ColorSpec::new().set_fg(Some(Color::Green)))?;

let package_name = parse!(
editor.readline(format!("Package Name (empty for '{}'): ", repo_metadata.name).as_str()),
identity,
repo_metadata.name
)?;
let package_name = data.name.map(Ok).unwrap_or_else(|| {
parse!(
editor
.readline(format!("Package Name (empty for '{}'): ", repo_metadata.name).as_str()),
identity,
repo_metadata.name
)
})?;

let description = parse!(
editor.readline(
format!(
"Description (empty for '{}'): ",
repo_metadata
.description
.as_ref()
.unwrap_or(&"*** enter a description ***".to_string())
)
.as_str()
),
identity,
repo_metadata.description.unwrap_or_default()
)?;
let description = data.description.map(Ok).unwrap_or_else(|| {
parse!(
editor.readline(
format!(
"Description (empty for '{}'): ",
repo_metadata
.description
.as_ref()
.unwrap_or(&"*** enter a description ***".to_string())
)
.as_str()
),
identity,
repo_metadata.description.unwrap_or_default()
)
})?;

let license = parse!(
editor.readline(
format!(
"License (empty for '{}', 'none' for no license): ",
repo_metadata
.license
.as_ref()
.unwrap_or(&"none".to_string())
)
.as_str()
),
parse_license,
parse_license(repo_metadata.license.unwrap())?
)?
.map(|license| license.name)
.unwrap_or("*** enter license here ***");
let license = data
.license // First validate the `--license` option
.and_then(|license| match license {
Either::Left(license) => Some(license),
Either::Right(_) => None,
})
.map_or_else(
|| { // If there was no `--license` then prompt the user
parse!(
editor.readline(
format!(
"License (empty for '{}', 'none' for no license): ",
repo_metadata
.license
.as_ref()
.unwrap_or(&"none".to_string())
)
.as_str()
),
parse_license,
parse_license(repo_metadata.license.unwrap())?
)
},
|val| Ok(Some(val)),
)?
.map(|license| license.name)
.unwrap_or("*** enter a license ***");

let maintainer = parse!(
editor.readline(
Expand Down

0 comments on commit 14a054a

Please sign in to comment.