Skip to content

Commit

Permalink
uds: cleanup error reporting to user
Browse files Browse the repository at this point in the history
  • Loading branch information
colemickens committed Aug 5, 2024
1 parent 5898cf2 commit 1780425
Showing 1 changed file with 23 additions and 10 deletions.
33 changes: 23 additions & 10 deletions src/cli/cmd/login/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ pub async fn dnixd_uds() -> color_eyre::Result<SendRequest<axum::body::Body>> {
let response = sender.send_request(request).await?;

if response.status() != StatusCode::OK {
tracing::error!("failed to connect to determinate-nixd socket");
return Err(eyre!("failed to connect to determinate-nixd socket"));
}

Expand All @@ -90,10 +89,15 @@ impl LoginSubcommand {
let dnixd_uds = match dnixd_uds().await {
Ok(socket) => Some(socket),
Err(err) => {
tracing::error!(
"failed to connect to determinate-nixd socket, will not attempt to use it: {:?}",
err
if tracing::enabled!(tracing::Level::DEBUG) {
tracing::debug!(
"failed to connect to determinate-nixd socket, will not attempt to use it: {}", err
);
} else {
tracing::warn!(
"failed to connect to determinate-nixd socket, will not attempt to use it."
);
}
None
}
};
Expand Down Expand Up @@ -194,12 +198,16 @@ impl LoginSubcommand {
.header("Content-Type", "application/json")
.body(Body::from(add_req_json))?;
let response = uds.send_request(request).await?;

let body = response.into_body();
let bytes = body.collect().await.unwrap_or_default().to_bytes();
let text: String = String::from_utf8_lossy(&bytes).into();

tracing::trace!("sent the add request: {:?}", text);

if response.status() != StatusCode::OK {
let body = response.into_body();
let bytes = body.collect().await.unwrap_or_default().to_bytes();
let text: String = String::from_utf8_lossy(&bytes).into();

tracing::warn!(
"failed to update netrc via determinatenixd, falling back to local-file approach: {}", &text
);
}

token_updated = true;
}
Expand All @@ -209,6 +217,11 @@ impl LoginSubcommand {
"failed to update netrc via determinatenixd, falling back to local-file approach"
);

// check if user is root or not
if !nix::unistd::Uid::effective().is_root() {
return Err(eyre!("`fh login` is attempting to update a file owned by root, please re-run the same command, prefixed with `sudo -i`."));
}

update_netrc_file(&netrc_file_path, &netrc_contents).await?;

// only update user_nix_config if we could not use determinatenixd
Expand Down

0 comments on commit 1780425

Please sign in to comment.