removed user + fixed Cli & moved RemoteOpts into own struct + fixed port args

pull/17/head
ZSchoen 3 years ago
parent 11019a2799
commit 3328e485ad

@ -17,5 +17,4 @@ log = "0.4.1"
simple_logger = "1.3.0"
xdg = "2.1.0"
serde = { version = "1.0", features = ["derive"] }
toml = "0.5.1"
config = "0.11"

@ -4,7 +4,6 @@ use serde::Deserialize;
pub struct Remote {
pub name: String,
pub host: String,
pub user: String,
pub ssh_port: u16,
pub temp_dir: String,
}
@ -13,7 +12,6 @@ pub struct Remote {
struct PartialRemote {
pub name: Option<String>,
pub host: String,
pub user: String,
pub ssh_port: Option<u16>,
pub temp_dir: Option<String>,
}
@ -23,7 +21,6 @@ impl Default for Remote {
Self {
name: String::new(),
host: String::new(),
user: String::new(),
ssh_port: 22,
temp_dir: "~/remote-builds".to_string(),
}
@ -39,7 +36,6 @@ impl From<PartialRemote> for Remote {
Remote {
name,
host: minimal_remote.host,
user: minimal_remote.user,
ssh_port,
temp_dir,
}
@ -55,12 +51,6 @@ impl<'de> Deserialize<'de> for Remote {
}
}
impl Remote {
pub fn user_host(&self) -> String {
format!("{}@{}", self.user, self.host)
}
}
#[derive(Debug, Default, Deserialize)]
pub struct Config {
#[serde(rename = "remote")]
@ -86,36 +76,26 @@ impl Config {
conf.try_into()
}
pub fn get_remote(&self, opts: &crate::Opts) -> Option<Remote> {
pub fn get_remote(&self, opts: &crate::RemoteOpts) -> Option<Remote> {
let remotes: Vec<_> = self.remotes.clone().unwrap_or_default();
let config_remote = match &opts.remote_name {
let config_remote = match &opts.name {
Some(remote_name) => remotes
.into_iter()
.find(|remote| remote.name == *remote_name),
None => remotes.into_iter().next(),
};
let blueprint_remote = match (
config_remote,
opts.remote_host.is_some() && opts.remote_user.is_some(),
) {
let blueprint_remote = match (config_remote, opts.host.is_some()) {
(Some(config_remote), _) => config_remote,
(None, true) => Remote::default(),
(None, false) => return None,
};
Some(Remote {
name: opts.remote_name.clone().unwrap_or(blueprint_remote.name),
host: opts.remote_host.clone().unwrap_or(blueprint_remote.host),
user: opts.remote_user.clone().unwrap_or(blueprint_remote.user),
ssh_port: opts
.remote_ssh_port
.clone()
.unwrap_or(blueprint_remote.ssh_port),
temp_dir: opts
.remote_temp_dir
.clone()
.unwrap_or(blueprint_remote.temp_dir),
name: opts.name.clone().unwrap_or(blueprint_remote.name),
host: opts.host.clone().unwrap_or(blueprint_remote.host),
ssh_port: opts.ssh_port.clone().unwrap_or(blueprint_remote.ssh_port),
temp_dir: opts.temp_dir.clone().unwrap_or(blueprint_remote.temp_dir),
})
}
}

@ -11,92 +11,109 @@ mod config;
const PROGRESS_FLAG: &str = "--info=progress2";
#[derive(StructOpt, Debug)]
#[structopt(name = "cargo-remote", bin_name = "cargo")]
pub struct Opts {
pub struct RemoteOpts {
#[structopt(short = "r", long = "remote", help = "Remote ssh build server")]
remote_name: Option<String>,
name: Option<String>,
#[structopt(short, long, help = "")]
remote_host: Option<String>,
host: Option<String>,
#[structopt(short, long, help = "")]
remote_user: Option<String>,
ssh_port: Option<u16>,
#[structopt(short, long, help = "")]
remote_ssh_port: Option<u16>,
temp_dir: Option<String>,
}
#[structopt(short, long, help = "")]
remote_temp_dir: Option<String>,
#[structopt(
short = "b",
long = "build-env",
help = "Set remote environment variables. RUST_BACKTRACE, CC, LIB, etc. ",
default_value = "RUST_BACKTRACE=1"
)]
build_env: String,
#[structopt(
short = "d",
long = "rustup-default",
help = "Rustup default (stable|beta|nightly)",
default_value = "stable"
)]
rustup_default: String,
#[structopt(
short = "e",
long = "env",
help = "Environment profile. default_value = /etc/profile",
default_value = "/etc/profile"
)]
env: String,
#[structopt(
short = "c",
long = "copy-back",
help = "Transfer the target folder or specific file from that folder back to the local machine"
)]
copy_back: Option<Option<String>>,
#[structopt(
long = "no-copy-lock",
help = "don't transfer the Cargo.lock file back to the local machine"
)]
no_copy_lock: bool,
#[structopt(
long = "manifest-path",
help = "Path to the manifest to execute",
default_value = "Cargo.toml",
parse(from_os_str)
)]
manifest_path: PathBuf,
#[structopt(
short = "h",
long = "transfer-hidden",
help = "Transfer hidden files and directories to the build server"
)]
hidden: bool,
#[structopt(help = "cargo command that will be executed remotely")]
command: String,
#[structopt(
help = "cargo options and flags that will be applied remotely",
name = "remote options"
)]
options: Vec<String>,
#[derive(StructOpt, Debug)]
#[structopt(name = "cargo-remote", bin_name = "cargo")]
enum Opts {
#[structopt(name = "remote")]
Remote {
#[structopt(flatten)]
remote_opts: RemoteOpts,
#[structopt(
short = "b",
long = "build-env",
help = "Set remote environment variables. RUST_BACKTRACE, CC, LIB, etc. ",
default_value = "RUST_BACKTRACE=1"
)]
build_env: String,
#[structopt(
short = "d",
long = "rustup-default",
help = "Rustup default (stable|beta|nightly)",
default_value = "stable"
)]
rustup_default: String,
#[structopt(
short = "e",
long = "env",
help = "Environment profile. default_value = /etc/profile",
default_value = "/etc/profile"
)]
env: String,
#[structopt(
short = "c",
long = "copy-back",
help = "Transfer the target folder or specific file from that folder back to the local machine"
)]
copy_back: Option<Option<String>>,
#[structopt(
long = "no-copy-lock",
help = "don't transfer the Cargo.lock file back to the local machine"
)]
no_copy_lock: bool,
#[structopt(
long = "manifest-path",
help = "Path to the manifest to execute",
default_value = "Cargo.toml",
parse(from_os_str)
)]
manifest_path: PathBuf,
#[structopt(
short = "h",
long = "transfer-hidden",
help = "Transfer hidden files and directories to the build server"
)]
hidden: bool,
#[structopt(help = "cargo command that will be executed remotely")]
command: String,
#[structopt(
help = "cargo options and flags that will be applied remotely",
name = "remote options"
)]
options: Vec<String>,
},
}
fn main() {
simple_logger::init().unwrap();
let opts = Opts::from_args();
let Opts::Remote {
remote_opts,
build_env,
rustup_default,
env,
copy_back,
no_copy_lock,
manifest_path,
hidden,
command,
options,
} = Opts::from_args();
let mut metadata_cmd = cargo_metadata::MetadataCommand::new();
metadata_cmd.manifest_path(&opts.manifest_path).no_deps();
metadata_cmd.manifest_path(manifest_path).no_deps();
let project_metadata = metadata_cmd.exec().unwrap();
let project_dir = project_metadata.workspace_root;
@ -110,7 +127,7 @@ fn main() {
}
};
let remote = match conf.get_remote(&opts) {
let remote = match conf.get_remote(&remote_opts) {
Some(remote) => remote,
None => {
error!("No remote build server was defined (use config file or the --remote flags)");
@ -118,19 +135,7 @@ fn main() {
}
};
let build_server = remote.user_host();
let Opts {
build_env,
rustup_default,
env,
copy_back,
no_copy_lock,
hidden,
command,
options,
..
} = opts;
let build_server = remote.host;
// generate a unique build path by using the hashed project dir as folder on the remote machine
let mut hasher = DefaultHasher::new();
@ -144,7 +149,7 @@ fn main() {
.arg("-a".to_owned())
.arg("--delete")
.arg("--compress")
.arg(format!("-e ssh -p {}", remote.ssh_port))
.args(&["-e", "ssh", "-p", &remote.ssh_port.to_string()])
.arg("--info=progress2")
.arg("--exclude")
.arg("target");
@ -181,7 +186,7 @@ fn main() {
info!("Starting build process.");
let output = Command::new("ssh")
.arg(format!("-p {}", remote.ssh_port))
.args(&["-p", &remote.ssh_port.to_string()])
.arg("-t")
.arg(&build_server)
.arg(build_command)
@ -201,7 +206,8 @@ fn main() {
.arg("-a")
.arg("--delete")
.arg("--compress")
.arg(format!("-e ssh -p {}", remote.ssh_port))
.args(&["-e", "ssh", "-p", &remote.ssh_port.to_string()])
.arg("--info=progress2")
.arg("--info=progress2")
.arg(format!(
"{}:{}/target/{}",
@ -231,7 +237,8 @@ fn main() {
.arg("-a")
.arg("--delete")
.arg("--compress")
.arg(format!("-e ssh -p {}", remote.ssh_port))
.args(&["-e", "ssh", "-p", &remote.ssh_port.to_string()])
.arg("--info=progress2")
.arg("--info=progress2")
.arg(format!("{}:{}/Cargo.lock", build_server, build_path))
.arg(format!("{}/Cargo.lock", project_dir.to_string_lossy()))

Loading…
Cancel
Save