Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

265 changes: 204 additions & 61 deletions crates/openshell-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use clap_complete::env::CompleteEnv;
use miette::Result;
use owo_colors::OwoColorize;
use std::io::Write;
use std::path::PathBuf;

use openshell_bootstrap::{
edge_token::load_edge_token, get_gateway_metadata, list_gateways, load_active_gateway,
Expand Down Expand Up @@ -633,18 +634,20 @@ fn normalize_completion_script(output: Vec<u8>, executable: &std::path::Path) ->
}

#[derive(Clone, Debug, ValueEnum)]
enum CliProviderType {
Claude,
Opencode,
Codex,
Copilot,
Generic,
Openai,
Anthropic,
Nvidia,
Gitlab,
Github,
Outlook,
enum ProviderProfileOutput {
Table,
Yaml,
Json,
}

impl ProviderProfileOutput {
fn as_str(&self) -> &'static str {
match self {
Self::Table => "table",
Self::Yaml => "yaml",
Self::Json => "json",
}
}
}
Comment thread
johntmyers marked this conversation as resolved.

#[derive(Clone, Debug, ValueEnum)]
Expand All @@ -662,24 +665,6 @@ impl From<CliEditor> for openshell_cli::ssh::Editor {
}
}

impl CliProviderType {
fn as_str(&self) -> &'static str {
match self {
Self::Claude => "claude",
Self::Opencode => "opencode",
Self::Codex => "codex",
Self::Copilot => "copilot",
Self::Generic => "generic",
Self::Openai => "openai",
Self::Anthropic => "anthropic",
Self::Nvidia => "nvidia",
Self::Gitlab => "gitlab",
Self::Github => "github",
Self::Outlook => "outlook",
}
}
}

#[derive(Subcommand, Debug)]
enum ProviderCommands {
/// Create a provider config.
Expand All @@ -690,8 +675,8 @@ enum ProviderCommands {
name: String,

/// Provider type.
#[arg(long = "type", value_enum)]
provider_type: CliProviderType,
#[arg(long = "type")]
provider_type: String,

/// Load provider credentials/config from existing local state.
#[arg(long, conflicts_with = "credentials")]
Expand Down Expand Up @@ -736,7 +721,15 @@ enum ProviderCommands {

/// List available provider profiles.
#[command(help_template = LEAF_HELP_TEMPLATE, next_help_heading = "FLAGS")]
ListProfiles,
ListProfiles {
/// Output format.
#[arg(short = 'o', long = "output", value_enum, default_value_t = ProviderProfileOutput::Table)]
output: ProviderProfileOutput,
},

/// Manage provider profiles.
#[command(subcommand, help_template = SUBCOMMAND_HELP_TEMPLATE)]
Profile(ProviderProfileCommands),

/// Update an existing provider's credentials or config.
#[command(help_template = LEAF_HELP_TEMPLATE, next_help_heading = "FLAGS")]
Expand Down Expand Up @@ -771,6 +764,51 @@ enum ProviderCommands {
},
}

#[derive(Subcommand, Debug)]
enum ProviderProfileCommands {
/// Export a provider profile.
#[command(help_template = LEAF_HELP_TEMPLATE, next_help_heading = "FLAGS")]
Export {
/// Provider profile id.
id: String,

/// Output format.
#[arg(short = 'o', long = "output", value_enum, default_value_t = ProviderProfileOutput::Yaml)]
output: ProviderProfileOutput,
},

/// Import provider profiles from a file or directory.
#[command(group = clap::ArgGroup::new("source").required(true).args(["file", "from"]), help_template = LEAF_HELP_TEMPLATE, next_help_heading = "FLAGS")]
Import {
/// Profile file to import.
#[arg(short = 'f', long = "file", value_hint = ValueHint::FilePath)]
file: Option<PathBuf>,

/// Directory containing profile files to import.
#[arg(long = "from", value_hint = ValueHint::DirPath)]
from: Option<PathBuf>,
},

/// Validate provider profile files without registering them.
#[command(group = clap::ArgGroup::new("source").required(true).args(["file", "from"]), help_template = LEAF_HELP_TEMPLATE, next_help_heading = "FLAGS")]
Lint {
/// Profile file to lint.
#[arg(short = 'f', long = "file", value_hint = ValueHint::FilePath)]
file: Option<PathBuf>,

/// Directory containing profile files to lint.
#[arg(long = "from", value_hint = ValueHint::DirPath)]
from: Option<PathBuf>,
},

/// Delete a custom provider profile.
#[command(help_template = LEAF_HELP_TEMPLATE, next_help_heading = "FLAGS")]
Delete {
/// Provider profile id.
id: String,
},
}

// -----------------------------------------------------------------------
// Gateway commands (replaces the old `cluster` / `cluster admin` groups)
// -----------------------------------------------------------------------
Expand Down Expand Up @@ -2777,9 +2815,35 @@ async fn main() -> Result<()> {
} => {
run::provider_list(endpoint, limit, offset, names, &tls).await?;
}
ProviderCommands::ListProfiles => {
run::provider_list_profiles(endpoint, &tls).await?;
ProviderCommands::ListProfiles { output } => {
run::provider_list_profiles(endpoint, output.as_str(), &tls).await?;
}
ProviderCommands::Profile(command) => match command {
ProviderProfileCommands::Export { id, output } => {
run::provider_profile_export(endpoint, &id, output.as_str(), &tls).await?;
}
ProviderProfileCommands::Import { file, from } => {
run::provider_profile_import(
endpoint,
file.as_deref(),
from.as_deref(),
&tls,
)
.await?;
}
ProviderProfileCommands::Lint { file, from } => {
run::provider_profile_lint(
endpoint,
file.as_deref(),
from.as_deref(),
&tls,
)
.await?;
}
ProviderProfileCommands::Delete { id } => {
run::provider_profile_delete(endpoint, &id, &tls).await?;
}
},
ProviderCommands::Update {
name,
from_existing,
Expand Down Expand Up @@ -3479,9 +3543,113 @@ mod tests {
assert!(matches!(
cli.command,
Some(Commands::Provider {
command: Some(ProviderCommands::ListProfiles)
command: Some(ProviderCommands::ListProfiles {
output: ProviderProfileOutput::Table
})
})
));
}

#[test]
fn provider_list_profiles_accepts_output_format() {
let cli = Cli::try_parse_from(["openshell", "provider", "list-profiles", "-o", "json"])
.expect("provider list-profiles -o json should parse");

assert!(matches!(
cli.command,
Some(Commands::Provider {
command: Some(ProviderCommands::ListProfiles {
output: ProviderProfileOutput::Json
})
})
));
}

#[test]
fn provider_profile_commands_parse() {
let export = Cli::try_parse_from([
"openshell",
"provider",
"profile",
"export",
"custom-api",
"-o",
"yaml",
])
.expect("provider profile export should parse");
assert!(matches!(
export.command,
Some(Commands::Provider {
command: Some(ProviderCommands::Profile(ProviderProfileCommands::Export {
id,
output: ProviderProfileOutput::Yaml
}))
}) if id == "custom-api"
));

let import = Cli::try_parse_from([
"openshell",
"provider",
"profile",
"import",
"--from",
"./profiles",
])
.expect("provider profile import should parse");
assert!(matches!(
import.command,
Some(Commands::Provider {
command: Some(ProviderCommands::Profile(ProviderProfileCommands::Import {
from: Some(_),
..
}))
})
));

let delete =
Cli::try_parse_from(["openshell", "provider", "profile", "delete", "custom-api"])
.expect("provider profile delete should parse");
assert!(matches!(
delete.command,
Some(Commands::Provider {
command: Some(ProviderCommands::Profile(ProviderProfileCommands::Delete {
id
}))
}) if id == "custom-api"
));
}

#[test]
fn provider_create_accepts_custom_profile_type_ids() {
let cli = Cli::try_parse_from([
"openshell",
"provider",
"create",
"--name",
"work-github",
"--type",
"github-readonly",
"--credential",
"GITHUB_TOKEN=token",
])
.expect("provider create should parse custom profile ids");

match cli.command {
Some(Commands::Provider {
command:
Some(ProviderCommands::Create {
name,
provider_type,
credentials,
..
}),
}) => {
assert_eq!(name, "work-github");
assert_eq!(provider_type, "github-readonly");
assert_eq!(credentials, vec!["GITHUB_TOKEN=token"]);
}
other => panic!("expected provider create command, got: {other:?}"),
}
}

#[test]
Expand Down Expand Up @@ -3630,29 +3798,4 @@ mod tests {
}
}
}

/// Ensure every provider registered in `ProviderRegistry` has a
/// corresponding `CliProviderType` variant (and vice-versa).
/// This test would have caught the missing `Copilot` variant from #707.
#[test]
fn cli_provider_types_match_registry() {
let registry = openshell_providers::ProviderRegistry::new();
let registry_types: std::collections::BTreeSet<&str> =
registry.known_types().into_iter().collect();

let cli_types: std::collections::BTreeSet<&str> =
<CliProviderType as ValueEnum>::value_variants()
.iter()
.map(CliProviderType::as_str)
.collect();

assert_eq!(
cli_types,
registry_types,
"CliProviderType variants must match ProviderRegistry.known_types(). \
CLI-only: {:?}, Registry-only: {:?}",
cli_types.difference(&registry_types).collect::<Vec<_>>(),
registry_types.difference(&cli_types).collect::<Vec<_>>(),
);
}
}
Loading
Loading