iggy_cli/commands/binary_consumer_offsets/
set_consumer_offset.rs1use crate::commands::cli_command::{CliCommand, PRINT_TARGET};
20use anyhow::Context;
21use async_trait::async_trait;
22use iggy_common::Client;
23use iggy_common::Identifier;
24use iggy_common::{Consumer, ConsumerKind};
25use tracing::{Level, event};
26
27pub struct SetConsumerOffsetCmd {
28 consumer: Consumer,
29 stream_id: Identifier,
30 topic_id: Identifier,
31 partition_id: u32,
32 offset: u64,
33}
34
35impl SetConsumerOffsetCmd {
36 pub fn new(
37 consumer_id: Identifier,
38 stream_id: Identifier,
39 topic_id: Identifier,
40 partition_id: u32,
41 offset: u64,
42 kind: ConsumerKind,
43 ) -> Self {
44 Self {
45 consumer: Consumer {
46 kind,
47 id: consumer_id,
48 },
49 stream_id,
50 topic_id,
51 partition_id,
52 offset,
53 }
54 }
55}
56
57#[async_trait]
58impl CliCommand for SetConsumerOffsetCmd {
59 fn explain(&self) -> String {
60 format!(
61 "set consumer offset for consumer with ID: {} for stream with ID: {} and topic with ID: {} and partition with ID: {} to {}",
62 self.consumer.id, self.stream_id, self.topic_id, self.partition_id, self.offset,
63 )
64 }
65
66 async fn execute_cmd(&mut self, client: &dyn Client) -> anyhow::Result<(), anyhow::Error> {
67 client
68 .store_consumer_offset(&self.consumer, &self.stream_id, &self.topic_id, Some(self.partition_id), self.offset)
69 .await
70 .with_context(|| {
71 format!(
72 "Problem setting consumer offset for consumer with ID: {} for stream with ID: {} and topic with ID: {} and partition with ID: {}",
73 self.consumer.id, self.stream_id, self.topic_id, self.partition_id
74 )
75 })?;
76
77 event!(target: PRINT_TARGET, Level::INFO,
78 "Consumer offset for consumer with ID: {} for stream with ID: {} and topic with ID: {} and partition with ID: {} set to {}",
79 self.consumer.id,
80 self.stream_id,
81 self.topic_id,
82 self.partition_id,
83 self.offset,
84 );
85
86 Ok(())
87 }
88}