Skip to content

Commit

Permalink
adding possibility to override file for subnet topic map
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolaMilosa committed Dec 12, 2024
1 parent a57b911 commit 931c3d3
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
5 changes: 5 additions & 0 deletions rs/cli/src/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::path::PathBuf;

use crate::commands::subnet::Subnet;
use api_boundary_nodes::ApiBoundaryNodes;
use clap::Args as ClapArgs;
Expand Down Expand Up @@ -141,6 +143,9 @@ pub struct DiscourseOpts {
/// prompt user for the link
#[clap(long, env = "DISCOURSE_SKIP_POST_CREATION", global = true)]
pub(crate) discourse_skip_post_creation: bool,

#[clap(long, env = "DISCOURSE_SUBNET_TOPIC_OVERRIDE_FILE_PATH", global = true)]
pub(crate) discourse_subnet_topic_override_file_path: Option<PathBuf>,
}

#[derive(Parser, Debug)]
Expand Down
1 change: 1 addition & 0 deletions rs/cli/src/ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ impl DreContext {
// It can happen because the tool runs in offline mode, or if its a dry run.
self.store.is_offline() || self.dry_run,
self.discourse_opts.discourse_skip_post_creation,
self.discourse_opts.discourse_subnet_topic_override_file_path.clone(),
)?);
*self.discourse_client.borrow_mut() = Some(client.clone());
Ok(client)
Expand Down
28 changes: 25 additions & 3 deletions rs/cli/src/discourse_client.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
use std::{collections::BTreeMap, fmt::Display, time::Duration};
use std::{
collections::BTreeMap,
fmt::Display,
path::{Path, PathBuf},
time::Duration,
};

use futures::{future::BoxFuture, TryFutureExt};
use ic_types::PrincipalId;
Expand Down Expand Up @@ -26,10 +31,18 @@ pub struct DiscourseClientImp {
api_user: String,
offline: bool,
skip_forum_post_creation: bool,
subnet_topic_file_override: Option<PathBuf>,
}

impl DiscourseClientImp {
pub fn new(url: String, api_key: String, api_user: String, offline: bool, skip_forum_post_creation: bool) -> anyhow::Result<Self> {
pub fn new(
url: String,
api_key: String,
api_user: String,
offline: bool,
skip_forum_post_creation: bool,
subnet_topic_file_override: Option<PathBuf>,
) -> anyhow::Result<Self> {
let client = reqwest::Client::builder().timeout(Duration::from_secs(30)).build()?;

Ok(Self {
Expand All @@ -39,6 +52,7 @@ impl DiscourseClientImp {
api_user,
offline,
skip_forum_post_creation,
subnet_topic_file_override,
})
}

Expand Down Expand Up @@ -209,14 +223,22 @@ fn get_subnet_topics_map() -> BTreeMap<PrincipalId, SubnetTopicInfo> {
serde_json::from_str(SUBNET_TOPICS_AND_SLUGS).unwrap()
}

fn get_subnet_topics_from_path(path: &Path) -> anyhow::Result<BTreeMap<PrincipalId, SubnetTopicInfo>> {
let file = std::fs::File::open(path)?;
serde_json::from_reader(file).map_err(anyhow::Error::from)
}

impl DiscourseClient for DiscourseClientImp {
fn create_replace_nodes_forum_post(&self, subnet_id: PrincipalId, body: String) -> BoxFuture<'_, anyhow::Result<Option<DiscourseResponse>>> {
Box::pin(async move {
if self.offline || self.skip_forum_post_creation {
return Ok(None);
}

let subnet_topic_map = get_subnet_topics_map();
let subnet_topic_map = match &self.subnet_topic_file_override {
Some(path) => get_subnet_topics_from_path(path)?,
None => get_subnet_topics_map(),
};
let topic_info = subnet_topic_map.get(&subnet_id).ok_or(anyhow::anyhow!(
"Subnet {} not found in the subnet topic map. Don't know where to create a forum post",
subnet_id.to_string()
Expand Down

0 comments on commit 931c3d3

Please sign in to comment.