forked from fajarhide/omni
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopencode.rs
More file actions
108 lines (94 loc) · 3.24 KB
/
opencode.rs
File metadata and controls
108 lines (94 loc) · 3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
use crate::agents::AgentIntegration;
use colored::*;
use serde_json::json;
use std::fs;
use std::path::PathBuf;
pub struct OpenCodeIntegration;
impl AgentIntegration for OpenCodeIntegration {
fn id(&self) -> &'static str {
"opencode"
}
fn name(&self) -> &'static str {
"OpenCode"
}
fn install(&self, exe_path: &str) -> anyhow::Result<()> {
let opencode_dir = dirs::home_dir()
.unwrap_or_else(|| PathBuf::from("."))
.join(".config/opencode");
let config_path = opencode_dir.join("opencode.json");
let mut val = if config_path.exists() {
let content = fs::read_to_string(&config_path)?;
serde_json::from_str(&content).unwrap_or_else(|_| json!({}))
} else {
fs::create_dir_all(&opencode_dir)?;
json!({})
};
if let Some(obj) = val.as_object_mut() {
let mcp_servers = obj.entry("mcpServers").or_insert_with(|| json!({}));
if let Some(servers_obj) = mcp_servers.as_object_mut() {
servers_obj.insert(
"omni".to_string(),
json!({
"command": exe_path,
"args": ["--mcp"]
}),
);
}
}
fs::write(&config_path, serde_json::to_string_pretty(&val)?)?;
println!(
" {} Configured MCP Server in ~/.config/opencode/opencode.json",
"✓".green()
);
Ok(())
}
fn uninstall(&self) -> anyhow::Result<()> {
let config_path = dirs::home_dir()
.unwrap_or_else(|| PathBuf::from("."))
.join(".config/opencode/opencode.json");
if !config_path.exists() {
return Ok(());
}
let content = fs::read_to_string(&config_path)?;
let Ok(mut val) = serde_json::from_str::<serde_json::Value>(&content) else {
return Ok(());
};
if let Some(obj) = val.as_object_mut()
&& let Some(servers) = obj.get_mut("mcpServers").and_then(|v| v.as_object_mut())
{
servers.remove("omni");
}
fs::write(&config_path, serde_json::to_string_pretty(&val)?)?;
println!(
" {} Removed MCP Server from ~/.config/opencode/opencode.json",
"✓".yellow()
);
Ok(())
}
fn doctor_check(&self, _fix_mode: bool, _warnings: &mut Vec<String>) -> bool {
let opencode_config = dirs::home_dir()
.unwrap_or_else(|| PathBuf::from("."))
.join(".config/opencode/opencode.json");
println!("\n {}", "OpenCode:".cyan());
if opencode_config.exists()
&& fs::read_to_string(&opencode_config)
.unwrap_or_default()
.contains("omni")
{
println!(
" {:<15} {} {}",
"Config:".bright_black(),
"~/.config/opencode/opencode.json".bright_black(),
"[OK]".green().bold()
);
true
} else {
println!(
" {:<15} {}",
"Config:".bright_black(),
"not configured".bright_black()
);
true
}
}
}