mirror of
https://github.com/nix-community/nix-init.git
synced 2024-09-11 08:05:42 +03:00
add commit option to commit the changes if it follows RFC 140
This commit is contained in:
parent
f521b7ad65
commit
7bb16d411d
@ -50,6 +50,7 @@ Arguments:
|
||||
Options:
|
||||
-u, --url <URL> Specify the URL
|
||||
-n, --nixpkgs <NIXPKGS> Path to nixpkgs (in nix)
|
||||
-C, --commit[=<COMMIT>] Commit the changes if the output path is name-based (RFC 140) [possible values: true, false]
|
||||
-c, --config <CONFIG> Specify the config file
|
||||
-h, --help Print help
|
||||
-V, --version Print version
|
||||
@ -85,6 +86,10 @@ maintainers = ["figsoda"]
|
||||
nixpkgs = "<nixpkgs>" # use the nixpkgs from channels (default)
|
||||
# nixpkgs = 'builtins.getFlake "nixpkgs"' # use the nixpkgs from the flake registry
|
||||
|
||||
# commit the changes if the output path is name-based (RFC 140)
|
||||
# see https://github.com/NixOS/nixpkgs/tree/master/pkgs/by-name for more information
|
||||
commit = true
|
||||
|
||||
# access tokens to access private repositories and avoid rate limits
|
||||
[access-tokens]
|
||||
"github.com" = "ghp_blahblahblah..."
|
||||
|
@ -13,6 +13,7 @@ use crate::utils::{CommandExt, ResultExt};
|
||||
#[derive(Default, Deserialize)]
|
||||
#[serde(default, rename_all = "kebab-case")]
|
||||
pub struct Config {
|
||||
pub commit: bool,
|
||||
pub maintainers: Vec<String>,
|
||||
pub nixpkgs: Option<String>,
|
||||
pub access_tokens: AccessTokens,
|
||||
|
@ -23,6 +23,12 @@ pub struct Opts {
|
||||
#[arg(short, long)]
|
||||
pub nixpkgs: Option<String>,
|
||||
|
||||
/// Commit the changes if the output path is name-based (RFC 140)
|
||||
///
|
||||
/// see https://github.com/NixOS/nixpkgs/tree/master/pkgs/by-name for more information
|
||||
#[arg(short = 'C', long, num_args=0..=1, require_equals = true, default_missing_value = "true")]
|
||||
pub commit: Option<bool>,
|
||||
|
||||
/// Specify the config file
|
||||
#[arg(short, long)]
|
||||
pub config: Option<PathBuf>,
|
||||
|
48
src/main.rs
48
src/main.rs
@ -16,7 +16,7 @@ use std::{
|
||||
fmt::Write as _,
|
||||
fs::{create_dir_all, metadata, read_dir, read_to_string, File},
|
||||
io::{stderr, BufRead, BufReader, Write as _},
|
||||
path::{Path, PathBuf},
|
||||
path::{Component, Path, PathBuf},
|
||||
};
|
||||
|
||||
use anyhow::{Context, Result};
|
||||
@ -1022,9 +1022,53 @@ async fn run() -> Result<()> {
|
||||
|
||||
writeln!(out, " }};\n}}")?;
|
||||
|
||||
let mut out_file = File::create(out_path).context("failed to create output file")?;
|
||||
let mut out_file = File::create(&out_path).context("failed to create output file")?;
|
||||
write!(out_file, "{out}")?;
|
||||
|
||||
if !opts.commit.unwrap_or(cfg.commit) || !Path::new(".git").is_dir() {
|
||||
return Ok(());
|
||||
}
|
||||
let Some(out_dir) = out_dir else {
|
||||
return Ok(());
|
||||
};
|
||||
|
||||
let mut xs = out_path.components();
|
||||
let attr: &str = match (
|
||||
xs.next(),
|
||||
xs.next(),
|
||||
xs.next(),
|
||||
xs.next(),
|
||||
xs.next(),
|
||||
xs.next(),
|
||||
) {
|
||||
(
|
||||
Some(Component::Normal(pkgs)),
|
||||
Some(Component::Normal(by_name)),
|
||||
Some(Component::Normal(_)),
|
||||
Some(Component::Normal(attr)),
|
||||
Some(Component::Normal(package_nix)),
|
||||
None,
|
||||
) if pkgs == "pkgs" && by_name == "by-name" && package_nix == "package.nix" => {
|
||||
attr.try_into()?
|
||||
}
|
||||
_ => return Ok(()),
|
||||
};
|
||||
|
||||
Command::new("git")
|
||||
.arg("add")
|
||||
.arg("-N")
|
||||
.arg(out_dir)
|
||||
.run()
|
||||
.await?;
|
||||
|
||||
Command::new("git")
|
||||
.arg("commit")
|
||||
.arg(out_dir)
|
||||
.arg("-om")
|
||||
.arg(format!("{attr}: init at {version}\n\n{url}"))
|
||||
.run()
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
21
src/utils.rs
21
src/utils.rs
@ -33,6 +33,7 @@ pub trait CommandExt {
|
||||
Self: 'a;
|
||||
|
||||
fn get_stdout(&mut self) -> Self::Output<'_, Result<Vec<u8>>>;
|
||||
fn run(&mut self) -> Self::Output<'_, Result<()>>;
|
||||
}
|
||||
|
||||
impl CommandExt for Command {
|
||||
@ -44,6 +45,17 @@ impl CommandExt for Command {
|
||||
into_stdout(self.output().await?)
|
||||
})
|
||||
}
|
||||
|
||||
fn run(&mut self) -> Self::Output<'_, Result<()>> {
|
||||
Box::pin(async move {
|
||||
info!("{:?}", &self);
|
||||
let status = self.status().await?;
|
||||
if !status.success() {
|
||||
bail!("command failed with {status}");
|
||||
}
|
||||
Ok(())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl CommandExt for std::process::Command {
|
||||
@ -53,6 +65,15 @@ impl CommandExt for std::process::Command {
|
||||
info!("{:?}", &self);
|
||||
into_stdout(self.output()?)
|
||||
}
|
||||
|
||||
fn run(&mut self) -> Self::Output<'_, Result<()>> {
|
||||
info!("{:?}", &self);
|
||||
let status = self.status()?;
|
||||
if !status.success() {
|
||||
bail!("command failed with {status}");
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn into_stdout(output: Output) -> Result<Vec<u8>> {
|
||||
|
Loading…
Reference in New Issue
Block a user