Skip to content

Commit db4f2e6

Browse files
authored
Improvement: add --raw flag to run, deploy command (#1653)
* add: LogsArgs struct and Logformat enum modify: logs() argument modify: output log * add: --raw flag to deploy, run * fix: common-tests * fix: tiny * fix: logs argument
1 parent 05a3765 commit db4f2e6

File tree

3 files changed

+47
-38
lines changed

3 files changed

+47
-38
lines changed

cargo-shuttle/src/args.rs

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -108,19 +108,7 @@ pub enum Command {
108108
/// Stop this Shuttle service
109109
Stop,
110110
/// View the logs of a deployment in this Shuttle service
111-
Logs {
112-
/// Deployment ID to get logs for. Defaults to currently running deployment
113-
id: Option<Uuid>,
114-
#[arg(short, long)]
115-
/// View logs from the most recent deployment (which is not always the latest running one)
116-
latest: bool,
117-
#[arg(short, long)]
118-
/// Follow log output
119-
follow: bool,
120-
#[arg(long)]
121-
/// Don't display timestamps and log origin tags
122-
raw: bool,
123-
},
111+
Logs(LogsArgs),
124112
/// List or manage projects on Shuttle
125113
#[command(subcommand)]
126114
Project(ProjectCommand),
@@ -271,6 +259,9 @@ pub struct DeployArgs {
271259
/// Don't run pre-deploy tests
272260
#[arg(long, visible_alias = "nt")]
273261
pub no_test: bool,
262+
/// Don't display timestamps and log origin tags
263+
#[arg(long)]
264+
pub raw: bool,
274265

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

291285
#[command(flatten)]
292286
pub secret_args: SecretsArgs,
@@ -407,6 +401,21 @@ impl InitTemplateArg {
407401
}
408402
}
409403

404+
#[derive(Parser, Clone, Debug, Default)]
405+
pub struct LogsArgs {
406+
/// Deployment ID to get logs for. Defaults to currently running deployment
407+
pub id: Option<Uuid>,
408+
#[arg(short, long)]
409+
/// View logs from the most recent deployment (which is not always the latest running one)
410+
pub latest: bool,
411+
#[arg(short, long)]
412+
/// Follow log output
413+
pub follow: bool,
414+
/// Don't display timestamps and log origin tags
415+
#[arg(long)]
416+
pub raw: bool,
417+
}
418+
410419
/// Helper function to parse and return the absolute path
411420
fn parse_path(path: OsString) -> Result<PathBuf, io::Error> {
412421
dunce::canonicalize(&path).map_err(|e| {

cargo-shuttle/src/lib.rs

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ use uuid::Uuid;
7272

7373
pub use crate::args::{Command, ProjectArgs, RunArgs, ShuttleArgs};
7474
use crate::args::{
75-
DeployArgs, DeploymentCommand, InitArgs, LoginArgs, LogoutArgs, ProjectCommand,
75+
DeployArgs, DeploymentCommand, InitArgs, LoginArgs, LogoutArgs, LogsArgs, ProjectCommand,
7676
ProjectStartArgs, ResourceCommand, TemplateLocation,
7777
};
7878
use crate::client::Client;
@@ -222,12 +222,7 @@ impl Shuttle {
222222
Command::Run(run_args) => self.local_run(run_args).await,
223223
Command::Deploy(deploy_args) => self.deploy(deploy_args).await,
224224
Command::Status => self.status().await,
225-
Command::Logs {
226-
id,
227-
latest,
228-
follow,
229-
raw,
230-
} => self.logs(id, latest, follow, raw).await,
225+
Command::Logs(logs_args) => self.logs(logs_args).await,
231226
Command::Deployment(DeploymentCommand::List { page, limit, raw }) => {
232227
self.deployments_list(page, limit, raw).await
233228
}
@@ -843,20 +838,14 @@ impl Shuttle {
843838
Ok(CommandOutcome::Ok)
844839
}
845840

846-
async fn logs(
847-
&self,
848-
id: Option<Uuid>,
849-
latest: bool,
850-
follow: bool,
851-
raw: bool,
852-
) -> Result<CommandOutcome> {
841+
async fn logs(&self, args: LogsArgs) -> Result<CommandOutcome> {
853842
let client = self.client.as_ref().unwrap();
854-
let id = if let Some(id) = id {
843+
let id = if let Some(id) = args.id {
855844
id
856845
} else {
857846
let proj_name = self.ctx.project_name();
858847

859-
if latest {
848+
if args.latest {
860849
// Find latest deployment (not always an active one)
861850
let deployments = client
862851
.get_deployments(proj_name, 0, 1)
@@ -883,7 +872,7 @@ impl Shuttle {
883872
}
884873
};
885874

886-
if follow {
875+
if args.follow {
887876
let mut stream = client
888877
.get_logs_ws(self.ctx.project_name(), &id)
889878
.await
@@ -895,10 +884,10 @@ impl Shuttle {
895884
if let tokio_tungstenite::tungstenite::Message::Text(line) = msg {
896885
match serde_json::from_str::<shuttle_common::LogItem>(&line) {
897886
Ok(log) => {
898-
if raw {
899-
println!("{}", log.get_raw_line());
887+
if args.raw {
888+
println!("{}", log.get_raw_line())
900889
} else {
901-
println!("{log}");
890+
println!("{log}")
902891
}
903892
}
904893
Err(err) => {
@@ -924,10 +913,10 @@ impl Shuttle {
924913
})?;
925914

926915
for log in logs.into_iter() {
927-
if raw {
928-
println!("{}", log.get_raw_line());
916+
if args.raw {
917+
println!("{}", log.get_raw_line())
929918
} else {
930-
println!("{log}");
919+
println!("{log}")
931920
}
932921
}
933922
}
@@ -1117,14 +1106,20 @@ impl Shuttle {
11171106
.context("child process did not have a handle to stdout")?;
11181107
let mut reader = BufReader::new(child_stdout).lines();
11191108
let service_name_clone = service_name.clone();
1109+
let raw = run_args.raw;
11201110
tokio::spawn(async move {
11211111
while let Some(line) = reader.next_line().await.unwrap() {
11221112
let log_item = LogItem::new(
11231113
deployment_id,
11241114
shuttle_common::log::Backend::Runtime(service_name_clone.clone()),
11251115
line,
11261116
);
1127-
println!("{log_item}");
1117+
1118+
if raw {
1119+
println!("{}", log_item.get_raw_line())
1120+
} else {
1121+
println!("{log_item}")
1122+
}
11281123
}
11291124
});
11301125

@@ -1717,7 +1712,11 @@ impl Shuttle {
17171712
}
17181713
};
17191714

1720-
println!("{log_item}");
1715+
if args.raw {
1716+
println!("{}", log_item.get_raw_line())
1717+
} else {
1718+
println!("{log_item}")
1719+
}
17211720

17221721
// Detect versions of deployer and runtime, and print warnings of outdated.
17231722
if !deployer_version_checked

common-tests/src/cargo_shuttle.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ pub async fn cargo_shuttle_run(working_directory: &str, external: bool) -> Strin
3030
port,
3131
external,
3232
release: false,
33+
raw: false,
3334
secret_args: Default::default(),
3435
};
3536

0 commit comments

Comments
 (0)