atharvalade commented on code in PR #2998:
URL: https://github.com/apache/iggy/pull/2998#discussion_r2996290458


##########
core/cli/src/commands/binary_context/common.rs:
##########
@@ -126,6 +126,69 @@ impl ContextManager {
         Ok(context_state.contexts.clone())
     }
 
+    pub async fn create_context(&mut self, name: &str, config: ContextConfig) 
-> Result<()> {
+        self.get_context_state().await?;
+        let cs = self.context_state.as_ref().unwrap();
+
+        if cs.contexts.contains_key(name) {
+            bail!("context '{name}' already exists in {CONTEXTS_FILE_NAME}")
+        }
+
+        let mut new_contexts = cs.contexts.clone();
+        new_contexts.insert(name.to_string(), config);
+
+        self.context_rw.ensure_iggy_home_exists()?;
+        self.context_rw
+            .write_contexts(new_contexts.clone())
+            .await
+            .context(format!("failed writing contexts after creating 
'{name}'"))?;
+
+        self.context_state.replace(ContextState {
+            active_context: cs.active_context.clone(),
+            contexts: new_contexts,
+        });
+
+        Ok(())
+    }
+
+    pub async fn delete_context(&mut self, name: &str) -> Result<()> {
+        if name == DEFAULT_CONTEXT_NAME {
+            bail!("cannot delete the '{DEFAULT_CONTEXT_NAME}' context")
+        }
+
+        self.get_context_state().await?;
+        let cs = self.context_state.take().unwrap();

Review Comment:
   switched `delete_context` to use `.as_ref().unwrap()` with `.clone()` to 
match `create_context` and avoid leaving `context_state` as `None` if a later 
operation bails



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to