parse config file to find default remote

If no --remote is specified the .cargo-remote.toml config file will be consulted to find a remote build server. If none is found an error is thrown.
pull/1/head
Sebastian Geisler 6 years ago
parent b09ee0d93e
commit 834cce64bc

5
.gitignore vendored

@ -58,4 +58,7 @@ crashlytics-build.properties
fabric.properties
# project file
cargo-remote.iml
cargo-remote.iml
# test config file
.cargo-remote.toml

@ -8,4 +8,5 @@ structopt = "0.1.6"
structopt-derive = "0.1.6"
cargo_metadata = "0.4.0"
log = "0.4.1"
simple_logger = "0.4.0"
simple_logger = "0.4.0"
toml = "0.4.5"

@ -7,27 +7,26 @@ extern crate cargo_metadata;
#[macro_use] extern crate log;
extern crate simple_logger;
extern crate toml;
use std::process::{exit, Command, Stdio};
use std::ffi::OsString;
use std::path::Path;
use std::fs::File;
use std::io::{BufReader, Read};
use structopt::StructOpt;
use toml::Value;
#[derive(StructOpt, Debug)]
#[structopt(name = "cargo remote")]
struct Opts {
// workaround for "remote" argument when calling "cargo remote"
_unused: String,
#[structopt(subcommand)]
command: Cmd,
command: String,
#[structopt(short = "r", long = "remote", help = "remote ssh build server")]
remote: String
}
#[derive(StructOpt, Debug)]
enum Cmd {
#[structopt(name="build", help = "Build cargo project remotely and copy back target folder")]
Build,
remote: Option<String>
}
fn main() {
@ -46,12 +45,26 @@ fn main() {
exit(-2);
}, |project| {
(
Path::new(&project.manifest_path).parent().expect("Cargo.toml seems to have no parent directory?"),
Path::new(&project.manifest_path)
.parent()
.expect("Cargo.toml seems to have no parent directory?"),
&project.name
)
});
let build_server = options.remote;
let build_server = options.remote.unwrap_or_else(|| {
let config_path = project_dir.join(".cargo-remote.toml");
File::open(config_path).ok().and_then(|mut file| {
let mut config_file_string = "".to_owned();
file.read_to_string(&mut config_file_string);
config_file_string.parse::<Value>().ok()
}).and_then(|value| {
value["remote"].as_str().map(str::to_owned)
}).unwrap_or_else(|| {
error!("No remote build server was defined (use config file or --remote flag)");
exit(-3);
})
});
match options.command {
Build => {

Loading…
Cancel
Save