Skip to content

Commit

Permalink
fix(cargo-shuttle): prevent crash when config owned by root (#409)
Browse files Browse the repository at this point in the history
  • Loading branch information
akrantz01 authored Oct 19, 2022
1 parent ac43016 commit 37755ca
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 18 deletions.
16 changes: 10 additions & 6 deletions cargo-shuttle/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,13 @@ pub trait ConfigManager: Sized {
C: for<'de> Deserialize<'de>,
{
let path = self.path();
let config_bytes = File::open(&path).and_then(|mut f| {
let mut buf = Vec::new();
f.read_to_end(&mut buf)?;
Ok(buf)
})?;
let config_bytes = File::open(&path)
.and_then(|mut f| {
let mut buf = Vec::new();
f.read_to_end(&mut buf)?;
Ok(buf)
})
.with_context(|| anyhow!("Unable to read configuration file: {}", path.display()))?;
toml::from_slice(config_bytes.as_slice())
.with_context(|| anyhow!("Invalid global configuration file: {}", path.display()))
}
Expand Down Expand Up @@ -256,7 +258,9 @@ impl RequestContext {
if !global.exists() {
global.create()?;
}
global.open()?;
global
.open()
.context("Unable to load global configuration")?;
Ok(Self {
global,
project: None,
Expand Down
14 changes: 4 additions & 10 deletions cargo-shuttle/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,10 @@ pub struct Shuttle {
ctx: RequestContext,
}

impl Default for Shuttle {
fn default() -> Self {
Self::new()
}
}

impl Shuttle {
pub fn new() -> Self {
let ctx = RequestContext::load_global().unwrap();
Self { ctx }
pub fn new() -> Result<Self> {
let ctx = RequestContext::load_global()?;
Ok(Self { ctx })
}

pub async fn run(mut self, mut args: Args) -> Result<CommandOutcome> {
Expand Down Expand Up @@ -479,7 +473,7 @@ mod tests {
name: None,
};

let mut shuttle = Shuttle::new();
let mut shuttle = Shuttle::new().unwrap();
Shuttle::load_project(&mut shuttle, &mut project_args).unwrap();

assert_eq!(
Expand Down
2 changes: 1 addition & 1 deletion cargo-shuttle/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use clap::Parser;
async fn main() -> Result<()> {
tracing_subscriber::fmt::init();

let result = Shuttle::new().run(Args::parse()).await;
let result = Shuttle::new()?.run(Args::parse()).await;

if matches!(result, Ok(CommandOutcome::DeploymentFailure)) {
// Deployment failure results in a shell error exit code being returned (this allows
Expand Down
2 changes: 2 additions & 0 deletions cargo-shuttle/tests/integration/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ async fn cargo_shuttle_init(path: PathBuf) -> anyhow::Result<CommandOutcome> {
let working_directory = Path::new(".").to_path_buf();

Shuttle::new()
.unwrap()
.run(Args {
api_url: Some("http://shuttle.invalid:80".to_string()),
project_args: ProjectArgs {
Expand Down Expand Up @@ -39,6 +40,7 @@ async fn cargo_shuttle_init_framework(path: PathBuf) -> anyhow::Result<CommandOu
let working_directory = Path::new(".").to_path_buf();

Shuttle::new()
.unwrap()
.run(Args {
api_url: Some("http://shuttle.invalid:80".to_string()),
project_args: ProjectArgs {
Expand Down
1 change: 1 addition & 0 deletions cargo-shuttle/tests/integration/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ async fn cargo_shuttle_command(
let working_directory = Path::new(working_directory).to_path_buf();

Shuttle::new()
.unwrap()
.run(Args {
api_url: Some("http://shuttle.invalid:80".to_string()),
project_args: ProjectArgs {
Expand Down
2 changes: 1 addition & 1 deletion cargo-shuttle/tests/integration/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ async fn cargo_shuttle_run(working_directory: &str) -> u16 {
let port = pick_unused_port().unwrap();
let run_args = RunArgs { port };

let runner = Shuttle::new().run(Args {
let runner = Shuttle::new().unwrap().run(Args {
api_url: Some("http://shuttle.invalid:80".to_string()),
project_args: ProjectArgs {
working_directory: working_directory.clone(),
Expand Down

0 comments on commit 37755ca

Please sign in to comment.