interplane.Get Omega
Read in your language

aien-sovereign-core-mv

AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination.

Brought by drakestapleton

branch main Branches Tags History

aien-sovereign-core-mv / crates / aien-switchboard / src / cli.rs

136 lines · 4398 bytes Raw Blame History
1//! The `switchboard` command line: `accounts`, `launch <alias>`, `doctor`.
2//!
3//! Everything the command touches comes in as arguments (environment, working
4//! folder, spawner, version probe, output streams) so tests drive it with
5//! fakes and a temp HOME.
6
7use crate::account::Credential;
8use crate::doctor::{doctor, VersionProbe};
9use crate::launch::{claude_profiles, launch, Env, Spawner};
10use crate::rules::RuleTable;
11use std::ffi::OsStr;
12use std::io::Write;
13use std::path::{Path, PathBuf};
14
15const USAGE: &str = "usage: switchboard [--rules FILE] <command>
16
17commands:
18 accounts list the accounts in the rule file
19 launch <alias> open claude in that profile's own settings folder
20 doctor check the setup (never reads a secret)
21
22The rule file defaults to $HOME/.switchboard/rules.toml.";
23
24/// Exit codes: 0 done, 1 the command failed, 2 usage error. `launch` returns
25/// the exit code of `claude`.
26pub fn run(
27 args: &[String],
28 env: &Env,
29 cwd: &Path,
30 spawner: &mut dyn Spawner,
31 probe: &mut dyn VersionProbe,
32 out: &mut dyn Write,
33 err: &mut dyn Write,
34) -> i32 {
35 let mut rules_path: Option<PathBuf> = None;
36 let mut rest: Vec<&str> = Vec::new();
37 let mut it = args.iter();
38 while let Some(a) = it.next() {
39 match a.as_str() {
40 "--rules" => match it.next() {
41 Some(p) => rules_path = Some(PathBuf::from(p)),
42 None => return usage(err, "--rules needs a file"),
43 },
44 "-h" | "--help" | "help" => {
45 let _ = writeln!(out, "{USAGE}");
46 return 0;
47 }
48 other => rest.push(other),
49 }
50 }
51 let (cmd, alias) = match rest.as_slice() {
52 ["accounts"] => ("accounts", None),
53 ["doctor"] => ("doctor", None),
54 ["launch", alias] => ("launch", Some(*alias)),
55 [] => return usage(err, "no command given"),
56 ["launch"] => return usage(err, "launch needs a profile alias"),
57 _ => return usage(err, &format!("unexpected arguments: {}", rest.join(" "))),
58 };
59
60 let path = match rules_path.or_else(|| default_rules_path(env)) {
61 Some(p) => p,
62 None => return usage(err, "HOME is not set; pass --rules FILE"),
63 };
64 let rules = match RuleTable::from_path(&path) {
65 Ok(r) => r,
66 Err(e) => {
67 let _ = writeln!(err, "switchboard: {e}");
68 return 1;
69 }
70 };
71
72 match (cmd, alias) {
73 ("accounts", _) => {
74 accounts(&rules, out);
75 0
76 }
77 ("doctor", _) => {
78 let report = doctor(&rules, env, cwd, probe);
79 let _ = writeln!(out, "rule file: {}\n{report}", path.display());
80 i32::from(report.failed())
81 }
82 (_, Some(alias)) => {
83 let _ = writeln!(err, "switchboard: opening profile {alias}");
84 match launch(&rules, alias, env, spawner) {
85 Ok(code) => code,
86 Err(e) => {
87 let _ = writeln!(err, "switchboard: {e}");
88 1
89 }
90 }
91 }
92 _ => usage(err, "unknown command"),
93 }
94}
95
96fn usage(err: &mut dyn Write, why: &str) -> i32 {
97 let _ = writeln!(err, "switchboard: {why}\n\n{USAGE}");
98 2
99}
100
101fn default_rules_path(env: &Env) -> Option<PathBuf> {
102 let home = env.get(OsStr::new("HOME"))?;
103 Some(Path::new(home).join(".switchboard").join("rules.toml"))
104}
105
106fn accounts(rules: &RuleTable, out: &mut dyn Write) {
107 let launchable: Vec<&str> = claude_profiles(rules)
108 .iter()
109 .map(|(a, _)| a.id.as_str())
110 .collect();
111 let _ = writeln!(
112 out,
113 "{:<16} {:<11} {:<8} {:<7} WHERE",
114 "ID", "PROVIDER", "PROGRAM", "LAUNCH"
115 );
116 for a in rules.accounts() {
117 let where_ = match &a.credential {
118 Credential::SettingsFolder(p) => format!("folder {}", p.display()),
119 Credential::KeyRef(k) => format!("vault key name {k}"),
120 Credential::None => "no login needed".to_string(),
121 };
122 let can = if launchable.contains(&a.id.as_str()) {
123 "yes"
124 } else {
125 "-"
126 };
127 let _ = writeln!(
128 out,
129 "{:<16} {:<11} {:<8} {:<7} {where_}",
130 a.id.as_str(),
131 a.provider.as_str(),
132 a.program,
133 can
134 );
135 }
136}