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

feat(cargo-shuttle): beta account command, cleanup #1859

Merged
merged 1 commit into from
Aug 20, 2024
Merged
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
2 changes: 1 addition & 1 deletion api-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ impl ShuttleApiClient {
.context("parsing name check response")
}

pub async fn get_current_user(&self) -> Result<user::Response> {
pub async fn get_current_user_beta(&self) -> Result<user::Response> {
self.get_json("/users/me".to_owned()).await
}

Expand Down
17 changes: 8 additions & 9 deletions api-client/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,26 @@ pub trait ToJson {
impl ToJson for reqwest::Response {
async fn to_json<T: DeserializeOwned>(self) -> Result<T> {
let status_code = self.status();
let full = self.bytes().await?;
let bytes = self.bytes().await?;
let string = String::from_utf8(bytes.to_vec())
.unwrap_or_else(|_| format!("[{} bytes]", bytes.len()));

#[cfg(feature = "tracing")]
tracing::trace!(
response = %String::from_utf8(full.to_vec()).unwrap_or_else(|_| format!("[{} bytes]", full.len())),
"parsing response to json"
);
tracing::trace!(response = string, "Parsing response to JSON");

if matches!(
status_code,
StatusCode::OK | StatusCode::SWITCHING_PROTOCOLS
) {
serde_json::from_slice(&full).context("failed to parse a successful response")
serde_json::from_str(&string).context("failed to parse a successful response")
} else {
#[cfg(feature = "tracing")]
tracing::trace!("parsing response to common error");
tracing::trace!("Parsing response to common error");

let res: ApiError = match serde_json::from_slice(&full) {
let res: ApiError = match serde_json::from_str(&string) {
Ok(res) => res,
_ => ApiError {
message: "Failed to parse response from the server.".to_string(),
message: format!("Failed to parse response from the server:\n{}", string),
status_code: status_code.as_u16(),
},
};
Expand Down
2 changes: 2 additions & 0 deletions cargo-shuttle/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ pub enum Command {
Resource(ResourceCommand),
/// Remove cargo build artifacts in the Shuttle environment
Clean,
/// BETA: Show info about your Shuttle account
Account,
/// Login to the Shuttle platform
Login(LoginArgs),
/// Log out of the Shuttle platform
Expand Down
27 changes: 25 additions & 2 deletions cargo-shuttle/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,10 @@ impl Shuttle {
return Ok(CommandOutcome::Ok);
}
eprintln!("INFO: Using beta platform API");
} else if matches!(args.cmd, Command::Deployment(DeploymentCommand::Stop)) {
} else if matches!(
args.cmd,
Command::Deployment(DeploymentCommand::Stop) | Command::Account
) {
eprintln!("This command is not supported on the legacy platform.");
return Ok(CommandOutcome::Ok);
}
Expand Down Expand Up @@ -194,6 +197,7 @@ impl Shuttle {
| Command::Deploy(..)
| Command::Status
| Command::Logs { .. }
| Command::Account
| Command::Login(..)
| Command::Logout(..)
| Command::Deployment(..)
Expand Down Expand Up @@ -233,6 +237,7 @@ impl Shuttle {
GenerateCommand::Manpage => self.generate_manpage(),
GenerateCommand::Shell { shell, output } => self.complete(shell, output),
},
Command::Account => self.account().await,
Command::Login(login_args) => self.login(login_args, args.offline).await,
Command::Logout(logout_args) => self.logout(logout_args).await,
Command::Feedback => self.feedback(),
Expand Down Expand Up @@ -736,6 +741,24 @@ impl Shuttle {
Ok(CommandOutcome::Ok)
}

async fn account(&self) -> Result<CommandOutcome> {
let client = self.client.as_ref().unwrap();
let user = client.get_current_user_beta().await?;
println!("{}", "Account info:".bold());
println!(" User Id: {}", user.id);
println!(" Username: {}", user.name);
println!(" Account tier: {}", user.account_tier);
println!(" Subscriptions:");
for sub in user.subscriptions {
println!(
" - {}: Type: {}, Quantity: {}, Created: {}, Updated: {}",
sub.id, sub.r#type, sub.quantity, sub.created_at, sub.updated_at,
);
}

Ok(CommandOutcome::Ok)
}

/// Log in with the given API key or after prompting the user for one.
async fn login(&mut self, login_args: LoginArgs, offline: bool) -> Result<CommandOutcome> {
let api_key_str = match login_args.api_key {
Expand Down Expand Up @@ -768,7 +791,7 @@ impl Shuttle {
eprintln!("INFO: Skipping API key verification");
} else {
let u = client
.get_current_user()
.get_current_user_beta()
.await
.context("failed to check API key validity")?;
println!("Logged in as {} ({})", u.name.bold(), u.id.bold());
Expand Down
1 change: 1 addition & 0 deletions scripts/local-admin.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
key="dh9z58jttoes3qvt" # arbitrary test key
export SHUTTLE_API_KEY=$key
export SHUTTLE_API="http://localhost:8001"
unset SHUTTLE_BETA
export PS1="(shuttle: local admin key) $(echo $PS1 | sed -e "s/(shuttle: .*) //")"

docker compose --file docker-compose.rendered.yml --project-name shuttle-dev exec auth /usr/local/bin/shuttle-auth --db-connection-uri=postgres://postgres:postgres@control-db init-admin --user-id admin --key $key
1 change: 1 addition & 0 deletions scripts/production.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@

export SHUTTLE_API="https://api.shuttle.rs"
unset SHUTTLE_API_KEY
unset SHUTTLE_BETA
export PS1="(shuttle: production) $(echo $PS1 | sed -e "s/(shuttle: .*) //")"
1 change: 1 addition & 0 deletions scripts/unstable.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@

export SHUTTLE_API="https://api.unstable.shuttle.rs"
unset SHUTTLE_API_KEY
unset SHUTTLE_BETA
export PS1="(shuttle: unstable) $(echo $PS1 | sed -e "s/(shuttle: .*) //")"