Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// specific language governing permissions and limitations
// under the License.

use crate::commands::binary_consumer_offsets::consumer_info;
use crate::commands::cli_command::{CliCommand, PRINT_TARGET};
use anyhow::Context;
use async_trait::async_trait;
Expand Down Expand Up @@ -50,13 +51,7 @@ impl GetConsumerOffsetCmd {
}

pub fn get_consumer_info(&self) -> String {
match self.consumer.kind {
ConsumerKind::Consumer => {
format!("consumer with ID: {}", self.consumer.id)
}
ConsumerKind::ConsumerGroup => format!("consumer group with ID: {}", self.consumer.id),
ConsumerKind::ExternalGroup => format!("external group with ID: {}", self.consumer.id),
}
consumer_info(&self.consumer)
}
}

Expand Down
10 changes: 10 additions & 0 deletions core/cli/src/commands/binary_consumer_offsets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,13 @@

pub mod get_consumer_offset;
pub mod set_consumer_offset;

use iggy_common::{Consumer, ConsumerKind};

pub(crate) fn consumer_info(consumer: &Consumer) -> String {
match consumer.kind {
ConsumerKind::Consumer => format!("consumer with ID: {}", consumer.id),
ConsumerKind::ConsumerGroup => format!("consumer group with ID: {}", consumer.id),
ConsumerKind::ExternalGroup => format!("external group with ID: {}", consumer.id),
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// specific language governing permissions and limitations
// under the License.

use crate::commands::binary_consumer_offsets::consumer_info;
use crate::commands::cli_command::{CliCommand, PRINT_TARGET};
use anyhow::Context;
use async_trait::async_trait;
Expand Down Expand Up @@ -51,14 +52,22 @@ impl SetConsumerOffsetCmd {
offset,
}
}

pub fn get_consumer_info(&self) -> String {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

simplification: SetConsumerOffsetCmd::get_consumer_info copies the three-arm match in GetConsumerOffsetCmd::get_consumer_info (core/cli/src/commands/binary_consumer_offsets/get_consumer_offset.rs:52) word for word. Move one pub(crate) fn consumer_info(consumer: &Consumer) into binary_consumer_offsets/mod.rs and call it from both commands.

consumer_info(&self.consumer)
}
}

#[async_trait]
impl CliCommand for SetConsumerOffsetCmd {
fn explain(&self) -> String {
format!(
"set consumer offset for consumer with ID: {} for stream with ID: {} and topic with ID: {} and partition with ID: {} to {}",
self.consumer.id, self.stream_id, self.topic_id, self.partition_id, self.offset,
"set consumer offset for {} for stream with ID: {} and topic with ID: {} and partition with ID: {} to {}",
self.get_consumer_info(),
self.stream_id,
self.topic_id,
self.partition_id,
self.offset,
)
}

Expand All @@ -68,14 +77,14 @@ impl CliCommand for SetConsumerOffsetCmd {
.await
.with_context(|| {
format!(
"Problem setting consumer offset for consumer with ID: {} for stream with ID: {} and topic with ID: {} and partition with ID: {}",
self.consumer.id, self.stream_id, self.topic_id, self.partition_id
"Problem setting consumer offset for {} for stream with ID: {} and topic with ID: {} and partition with ID: {}",
self.get_consumer_info(), self.stream_id, self.topic_id, self.partition_id
)
})?;

event!(target: PRINT_TARGET, Level::INFO,
"Consumer offset for consumer with ID: {} for stream with ID: {} and topic with ID: {} and partition with ID: {} set to {}",
self.consumer.id,
"Consumer offset for {} for stream with ID: {} and topic with ID: {} and partition with ID: {} set to {}",
self.get_consumer_info(),
self.stream_id,
self.topic_id,
self.partition_id,
Expand All @@ -85,3 +94,43 @@ impl CliCommand for SetConsumerOffsetCmd {
Ok(())
}
}

#[cfg(test)]
mod tests {
use super::*;

fn cmd(kind: ConsumerKind) -> SetConsumerOffsetCmd {
SetConsumerOffsetCmd::new(
Identifier::numeric(1).unwrap(),
Identifier::numeric(2).unwrap(),
Identifier::numeric(3).unwrap(),
0,
100,
kind,
)
}

#[test]
fn should_name_consumer_kind_in_explain_message() {
assert_eq!(
cmd(ConsumerKind::Consumer).explain(),
"set consumer offset for consumer with ID: 1 for stream with ID: 2 and topic with ID: 3 and partition with ID: 0 to 100"
);
}

#[test]
fn should_name_consumer_group_kind_in_explain_message() {
assert_eq!(
cmd(ConsumerKind::ConsumerGroup).explain(),
"set consumer offset for consumer group with ID: 1 for stream with ID: 2 and topic with ID: 3 and partition with ID: 0 to 100"
);
}

#[test]
fn should_name_external_group_kind_in_explain_message() {
assert_eq!(
cmd(ConsumerKind::ExternalGroup).explain(),
"set consumer offset for external group with ID: 1 for stream with ID: 2 and topic with ID: 3 and partition with ID: 0 to 100"
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,14 @@ impl IggyCmdTestCase for TestExternalGroupOffsetCmd {
fn verify_command(&self, command_state: Assert) {
match self.step {
Step::Set => {
command_state.success();
command_state
.success()
.stdout(starts_with(format!(
"Executing set consumer offset for external group with ID: {GROUP_NAME}"
)))
.stdout(contains(format!(
"Consumer offset for external group with ID: {GROUP_NAME}"
)));
}
Step::Get => {
command_state
Expand Down