Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --local-registry and --tag flags #35

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cli/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ pub enum SrtoolError {

#[error("UTF8 error: {0}")]
UTF8(std::string::FromUtf8Error),

#[error("EnvVar error: {0}")]
Var(#[from] std::env::VarError),
}

impl From<std::io::Error> for SrtoolError {
Expand Down
28 changes: 25 additions & 3 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use clap::{crate_version, Parser};
use log::{debug, info};
use opts::*;
use srtool_lib::*;
use std::path::PathBuf;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::{env, fs};

Expand Down Expand Up @@ -41,7 +41,14 @@ fn main() -> Result<(), SrtoolError> {
debug!("Checking what is the latest available tag...");
const ONE_HOUR: u64 = 60 * 60;

let tag = get_image_tag(Some(ONE_HOUR)).expect("Issue getting the image tag");
let tag = if let Some(tag) = opts.tag {
tag
} else if let Ok(tag) = env::var("SRTOOL_TAG") {
info!("using tag from ENV: {:?}", tag);
tag
} else {
get_image_tag(Some(ONE_HOUR)).expect("Issue getting the image tag")
};

info!("Using {image}:{tag}");

Expand All @@ -64,10 +71,24 @@ fn main() -> Result<(), SrtoolError> {
let no_cache = if opts.engine == ContainerEngine::Podman { true } else { build_opts.no_cache };
let cache_mount =
if !no_cache { format!("-v {tmpdir}:/cargo-home", tmpdir = tmpdir.display()) } else { String::new() };
let root_opts = if build_opts.root { "-u root" } else { "" };

let root_opts = if build_opts.root { "-u root" } else { "-u builder" };
let verbose_opts = if build_opts.verbose { "-e VERBOSE=1" } else { "" };
let no_wasm_std = if build_opts.no_wasm_std { "-e WASM_BUILD_STD=0" } else { "" };

let local_registry = if build_opts.use_local_registry {
let user = if build_opts.root { "root" } else { "builder" };
let cargo_home = std::env::var("CARGO_HOME")?;
let cargo_home = Path::new(&cargo_home).canonicalize()?;
let cargo_home = cargo_home.display();
format!(
"-v {cargo_home}/git:/home/{user}/.cargo/git \
-v {cargo_home}/registry:/home/{user}/.cargo/registry"
)
} else {
"".to_string()
};

debug!("engine: '{engine}'");
debug!("app: '{app}'");
debug!("json: '{json}'");
Expand Down Expand Up @@ -99,6 +120,7 @@ fn main() -> Result<(), SrtoolError> {
{verbose_opts} \
{no_wasm_std} \
-v {dir}:/build \
{local_registry} \
{cache_mount} \
{root_opts} \
{image}:{tag} build{app}{json}"
Expand Down
6 changes: 6 additions & 0 deletions cli/src/opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ pub struct Opts {
#[clap(short, long, default_value = "docker.io/paritytech/srtool", global = true)]
pub image: String,

#[clap(long, global = true)]
pub tag: Option<String>,

/// This option is DEPRECATED and has no effect
#[clap(short, long)]
pub json: bool,
Expand Down Expand Up @@ -131,6 +134,9 @@ pub struct BuildOpts {
/// runtime compilation.
#[clap(long)]
pub no_wasm_std: bool,

#[clap(long)]
pub use_local_registry: bool,
}

/// Info opts
Expand Down
7 changes: 0 additions & 7 deletions lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ mod error;
use error::*;
use log::{debug, info};
use std::{
env,
fs::{self, File},
io::Write,
process::Command,
Expand All @@ -25,12 +24,6 @@ pub fn fetch_image_tag() -> Result<String> {
/// Get the latest image. it is fetched from cache we have a version that is younger than `cache_validity` in seconds.
#[allow(clippy::result_large_err)]
pub fn get_image_tag(cache_validity: Option<u64>) -> Result<String> {
let env_tag = env::var("SRTOOL_TAG");
if let Ok(tag) = env_tag {
info!("using tag from ENV: {:?}", tag);
return Ok(tag);
}

let cache_location = std::env::temp_dir().join(CACHE_FILE);
debug!("cache_location = {:?}", cache_location);

Expand Down
Loading