Skip to content

Commit

Permalink
Merge c98e783 into 39e78a0
Browse files Browse the repository at this point in the history
  • Loading branch information
ethankhall authored Jul 29, 2022
2 parents 39e78a0 + c98e783 commit d965754
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 11 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ tempfile = "3.1"
tokio = { version = "1", features = ["full"] }
serde_json = { version = "1", features = ["preserve_order"] }

[features]
gh-cli = []

[dev-dependencies]
assert_cmd = "1.0"
tempdir = "0.3"
Expand Down
19 changes: 11 additions & 8 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
FROM rust:1.50-alpine3.13 as builder
FROM rust:1.61-alpine3.16 as builder
RUN apk update
RUN apk add libc-dev openssl-dev openssl
RUN apk add libc-dev openssl-dev openssl github-cli

RUN USER=root cargo new --bin crom
WORKDIR /crom
COPY ./Cargo.toml ./Cargo.toml
COPY ./Cargo.lock ./Cargo.lock
RUN cargo build --release
RUN cargo build --release --features gh-cli
RUN rm src/*.rs

ADD . ./

RUN rm ./target/release/deps/crom*
RUN cargo run --release -- write-version next-release
RUN cargo build --release
RUN cargo run --release --features gh-cli -- write-version next-release
RUN cargo run --release --features gh-cli-- gh --help
RUN cargo build --release --features gh-cli

FROM alpine:3.11
FROM alpine:3.16

RUN apk add --no-cache ca-certificates openssl tzdata
RUN apk add --no-cache ca-certificates openssl tzdata github-cli
COPY --from=builder /crom/target/release/crom /usr/bin/crom

WORKDIR /target

ENTRYPOINT ["/usr/bin/crom"]
ENTRYPOINT ["/usr/bin/crom"]

RUN crom --help && crom gh --help
17 changes: 17 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ pub enum SubCommand {
WriteVersion(WriteArgs),
#[clap(name = "util", alias = "utility", alias = "utilities")]
Utility(UtilityArgs),
#[cfg(feature = "gh-cli")]
#[clap(name = "gh")]
GitHub(GitHubCli),
}

/// Bootstrap a project
Expand Down Expand Up @@ -185,3 +188,17 @@ pub enum UtilitySubCommand {
/// with a non-zero exit code.
VerifyNoChanges,
}

#[cfg(feature = "gh-cli")]
/// Execute the official GitHub CLI
#[derive(Parser, Debug)]
#[clap(
allow_missing_positional = true,
disable_help_flag = true,
disable_help_subcommand = true,
allow_hyphen_values = true
)]
pub struct GitHubCli {
#[clap(multiple_values = true)]
pub args: Vec<String>,
}
2 changes: 1 addition & 1 deletion src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ async fn create_version(request: VersionRequest) -> CromResult<(Version, PathBuf
let default_version = matcher.build_default_version();
let latest_version = versions.last().unwrap_or(&default_version);

let mut head = git_repo::get_head_sha(&repo)?;
let mut head = git_repo::get_head_sha(location.clone(), &repo)?;
head.truncate(7);

let version = build_version(request, head, latest_version);
Expand Down
5 changes: 5 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ error_chain! {
display("Unable to determine git repo from remote: {}", t)
}

UnknownGitHead(t: ::std::path::PathBuf) {
description("Unable to decode head")
display("Unable to decode head from repo {}", t.display())
}

FileNotFound(t: ::std::path::PathBuf) {
description("File not found")
display("File not found: {:?}", t)
Expand Down
13 changes: 11 additions & 2 deletions src/git_repo.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use crate::errors::ErrorKind;
use error_chain::bail;
use git2::*;
use log::debug;
use std::path::PathBuf;
use std::vec::Vec;

use crate::errors::Error as CromError;
Expand Down Expand Up @@ -28,8 +31,14 @@ pub fn is_working_repo_clean(repo: &Repository) -> Result<bool> {
Ok(statuses.is_empty())
}

pub fn get_head_sha(repo: &Repository) -> Result<String> {
let head = repo.head()?.peel_to_commit()?;
pub fn get_head_sha(location: PathBuf, repo: &Repository) -> Result<String> {
let head = match repo.head()?.target() {
Some(head) => head,
None => bail!(ErrorKind::UnknownGitHead(location)),
};

let head = repo.find_commit(head)?;

let strs: Vec<String> = head
.id()
.as_bytes()
Expand Down
9 changes: 9 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ async fn main() {
SubCommand::Get(args) => crate::commands::run_get(args).await,
SubCommand::WriteVersion(args) => crate::commands::run_write(args).await,
SubCommand::Utility(args) => crate::commands::run_utils(args).await,
#[cfg(feature = "gh-cli")]
SubCommand::GitHub(gh) => run_gh(gh),
};

let exit_code = match result {
Expand All @@ -54,3 +56,10 @@ async fn main() {

process::exit(exit_code);
}

#[cfg(feature = "gh-cli")]
fn run_gh(gh: cli::GitHubCli) -> CromResult<i32> {
use std::process::Command;
let exit_status = Command::new("gh").args(gh.args).spawn()?.wait()?;
Ok(exit_status.code().unwrap_or(1))
}

0 comments on commit d965754

Please sign in to comment.