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

Improvement: add --raw flag to run, deploy command #1653

Merged
merged 7 commits into from Mar 11, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 22 additions & 13 deletions cargo-shuttle/src/args.rs
Expand Up @@ -108,19 +108,7 @@ pub enum Command {
/// Stop this Shuttle service
Stop,
/// View the logs of a deployment in this Shuttle service
Logs {
/// Deployment ID to get logs for. Defaults to currently running deployment
id: Option<Uuid>,
#[arg(short, long)]
/// View logs from the most recent deployment (which is not always the latest running one)
latest: bool,
#[arg(short, long)]
/// Follow log output
follow: bool,
#[arg(long)]
/// Don't display timestamps and log origin tags
raw: bool,
},
Logs(LogsArgs),
/// List or manage projects on Shuttle
#[command(subcommand)]
Project(ProjectCommand),
Expand Down Expand Up @@ -271,6 +259,9 @@ pub struct DeployArgs {
/// Don't run pre-deploy tests
#[arg(long, visible_alias = "nt")]
pub no_test: bool,
/// Don't display timestamps and log origin tags
#[arg(long)]
pub raw: bool,

#[command(flatten)]
pub secret_args: SecretsArgs,
Expand All @@ -287,6 +278,9 @@ pub struct RunArgs {
/// Use release mode for building the project
#[arg(long, short = 'r')]
pub release: bool,
/// Don't display timestamps and log origin tags
#[arg(long)]
pub raw: bool,

#[command(flatten)]
pub secret_args: SecretsArgs,
Expand Down Expand Up @@ -407,6 +401,21 @@ impl InitTemplateArg {
}
}

#[derive(Parser, Clone, Debug, Default)]
pub struct LogsArgs {
/// Deployment ID to get logs for. Defaults to currently running deployment
pub id: Option<Uuid>,
#[arg(short, long)]
/// View logs from the most recent deployment (which is not always the latest running one)
pub latest: bool,
#[arg(short, long)]
/// Follow log output
pub follow: bool,
/// Don't display timestamps and log origin tags
#[arg(long)]
pub raw: bool,
}

/// Helper function to parse and return the absolute path
fn parse_path(path: OsString) -> Result<PathBuf, io::Error> {
dunce::canonicalize(&path).map_err(|e| {
Expand Down
49 changes: 24 additions & 25 deletions cargo-shuttle/src/lib.rs
Expand Up @@ -72,7 +72,7 @@ use uuid::Uuid;

pub use crate::args::{Command, ProjectArgs, RunArgs, ShuttleArgs};
use crate::args::{
DeployArgs, DeploymentCommand, InitArgs, LoginArgs, LogoutArgs, ProjectCommand,
DeployArgs, DeploymentCommand, InitArgs, LoginArgs, LogoutArgs, LogsArgs, ProjectCommand,
ProjectStartArgs, ResourceCommand, TemplateLocation,
};
use crate::client::Client;
Expand Down Expand Up @@ -222,12 +222,7 @@ impl Shuttle {
Command::Run(run_args) => self.local_run(run_args).await,
Command::Deploy(deploy_args) => self.deploy(deploy_args).await,
Command::Status => self.status().await,
Command::Logs {
id,
latest,
follow,
raw,
} => self.logs(id, latest, follow, raw).await,
Command::Logs(logs_args) => self.logs(logs_args).await,
Command::Deployment(DeploymentCommand::List { page, limit, raw }) => {
self.deployments_list(page, limit, raw).await
}
Expand Down Expand Up @@ -843,20 +838,14 @@ impl Shuttle {
Ok(CommandOutcome::Ok)
}

async fn logs(
&self,
id: Option<Uuid>,
latest: bool,
follow: bool,
raw: bool,
) -> Result<CommandOutcome> {
async fn logs(&self, args: LogsArgs) -> Result<CommandOutcome> {
let client = self.client.as_ref().unwrap();
let id = if let Some(id) = id {
let id = if let Some(id) = args.id {
id
} else {
let proj_name = self.ctx.project_name();

if latest {
if args.latest {
// Find latest deployment (not always an active one)
let deployments = client
.get_deployments(proj_name, 0, 1)
Expand All @@ -883,7 +872,7 @@ impl Shuttle {
}
};

if follow {
if args.follow {
let mut stream = client
.get_logs_ws(self.ctx.project_name(), &id)
.await
Expand All @@ -895,10 +884,10 @@ impl Shuttle {
if let tokio_tungstenite::tungstenite::Message::Text(line) = msg {
match serde_json::from_str::<shuttle_common::LogItem>(&line) {
Ok(log) => {
if raw {
println!("{}", log.get_raw_line());
if args.raw {
println!("{}", log.get_raw_line())
} else {
println!("{log}");
println!("{log}")
}
}
Err(err) => {
Expand All @@ -924,10 +913,10 @@ impl Shuttle {
})?;

for log in logs.into_iter() {
if raw {
println!("{}", log.get_raw_line());
if args.raw {
println!("{}", log.get_raw_line())
} else {
println!("{log}");
println!("{log}")
}
}
}
Expand Down Expand Up @@ -1117,14 +1106,20 @@ impl Shuttle {
.context("child process did not have a handle to stdout")?;
let mut reader = BufReader::new(child_stdout).lines();
let service_name_clone = service_name.clone();
let raw = run_args.raw;
tokio::spawn(async move {
while let Some(line) = reader.next_line().await.unwrap() {
let log_item = LogItem::new(
deployment_id,
shuttle_common::log::Backend::Runtime(service_name_clone.clone()),
line,
);
println!("{log_item}");

if raw {
println!("{}", log_item.get_raw_line())
} else {
println!("{log_item}")
}
}
});

Expand Down Expand Up @@ -1717,7 +1712,11 @@ impl Shuttle {
}
};

println!("{log_item}");
if args.raw {
println!("{}", log_item.get_raw_line())
} else {
println!("{log_item}")
}

// Detect versions of deployer and runtime, and print warnings of outdated.
if !deployer_version_checked
Expand Down
1 change: 1 addition & 0 deletions common-tests/src/cargo_shuttle.rs
Expand Up @@ -30,6 +30,7 @@ pub async fn cargo_shuttle_run(working_directory: &str, external: bool) -> Strin
port,
external,
release: false,
raw: false,
secret_args: Default::default(),
};

Expand Down