[codex-rs] fix: exit code 1 if no api key (#697)
This commit is contained in:
13
codex-rs/Cargo.lock
generated
13
codex-rs/Cargo.lock
generated
@@ -528,6 +528,7 @@ dependencies = [
|
|||||||
"anyhow",
|
"anyhow",
|
||||||
"clap",
|
"clap",
|
||||||
"codex-core",
|
"codex-core",
|
||||||
|
"owo-colors",
|
||||||
"tokio",
|
"tokio",
|
||||||
"tracing",
|
"tracing",
|
||||||
"tracing-subscriber",
|
"tracing-subscriber",
|
||||||
@@ -560,7 +561,7 @@ dependencies = [
|
|||||||
"anyhow",
|
"anyhow",
|
||||||
"clap",
|
"clap",
|
||||||
"codex-core",
|
"codex-core",
|
||||||
"owo-colors 4.2.0",
|
"owo-colors",
|
||||||
"rand",
|
"rand",
|
||||||
"tokio",
|
"tokio",
|
||||||
"tracing",
|
"tracing",
|
||||||
@@ -598,7 +599,7 @@ dependencies = [
|
|||||||
"eyre",
|
"eyre",
|
||||||
"indenter",
|
"indenter",
|
||||||
"once_cell",
|
"once_cell",
|
||||||
"owo-colors 3.5.0",
|
"owo-colors",
|
||||||
"tracing-error",
|
"tracing-error",
|
||||||
]
|
]
|
||||||
|
|
||||||
@@ -609,7 +610,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||||||
checksum = "cd6be1b2a7e382e2b98b43b2adcca6bb0e465af0bdd38123873ae61eb17a72c2"
|
checksum = "cd6be1b2a7e382e2b98b43b2adcca6bb0e465af0bdd38123873ae61eb17a72c2"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"once_cell",
|
"once_cell",
|
||||||
"owo-colors 3.5.0",
|
"owo-colors",
|
||||||
"tracing-core",
|
"tracing-core",
|
||||||
"tracing-error",
|
"tracing-error",
|
||||||
]
|
]
|
||||||
@@ -2224,12 +2225,6 @@ version = "0.1.1"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39"
|
checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "owo-colors"
|
|
||||||
version = "3.5.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "c1b04fb49957986fdce4d6ee7a65027d55d4b6d2265e5848bbb507b58ccfdb6f"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "owo-colors"
|
name = "owo-colors"
|
||||||
version = "4.2.0"
|
version = "4.2.0"
|
||||||
|
|||||||
@@ -24,3 +24,4 @@ tokio = { version = "1", features = [
|
|||||||
] }
|
] }
|
||||||
tracing = { version = "0.1.41", features = ["log"] }
|
tracing = { version = "0.1.41", features = ["log"] }
|
||||||
tracing-subscriber = { version = "0.3.19", features = ["env-filter"] }
|
tracing-subscriber = { version = "0.3.19", features = ["env-filter"] }
|
||||||
|
owo-colors = "4.2.0"
|
||||||
|
|||||||
@@ -12,11 +12,23 @@ use codex_core::protocol::FileChange;
|
|||||||
use codex_core::protocol::InputItem;
|
use codex_core::protocol::InputItem;
|
||||||
use codex_core::protocol::Op;
|
use codex_core::protocol::Op;
|
||||||
use codex_core::util::is_inside_git_repo;
|
use codex_core::util::is_inside_git_repo;
|
||||||
|
use owo_colors::OwoColorize;
|
||||||
use tracing::debug;
|
use tracing::debug;
|
||||||
use tracing::error;
|
use tracing::error;
|
||||||
use tracing::info;
|
use tracing::info;
|
||||||
use tracing_subscriber::EnvFilter;
|
use tracing_subscriber::EnvFilter;
|
||||||
|
|
||||||
|
/// Returns `true` if a recognised API key is present in the environment.
|
||||||
|
///
|
||||||
|
/// At present we only support `OPENAI_API_KEY`, mirroring the behaviour of the
|
||||||
|
/// Node-based `codex-cli`. Additional providers can be added here when the
|
||||||
|
/// Rust implementation gains first-class support for them.
|
||||||
|
fn has_api_key() -> bool {
|
||||||
|
std::env::var("OPENAI_API_KEY")
|
||||||
|
.map(|s| !s.trim().is_empty())
|
||||||
|
.unwrap_or(false)
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn run_main(cli: Cli) -> anyhow::Result<()> {
|
pub async fn run_main(cli: Cli) -> anyhow::Result<()> {
|
||||||
// TODO(mbolin): Take a more thoughtful approach to logging.
|
// TODO(mbolin): Take a more thoughtful approach to logging.
|
||||||
let default_level = "error";
|
let default_level = "error";
|
||||||
@@ -41,6 +53,20 @@ pub async fn run_main(cli: Cli) -> anyhow::Result<()> {
|
|||||||
..
|
..
|
||||||
} = cli;
|
} = cli;
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------
|
||||||
|
// API key handling
|
||||||
|
// ---------------------------------------------------------------------
|
||||||
|
|
||||||
|
if !has_api_key() {
|
||||||
|
eprintln!(
|
||||||
|
"\n{msg}\n\nSet the environment variable {var} and re-run this command.\nYou can create a key here: {url}\n",
|
||||||
|
msg = "Missing OpenAI API key.".red(),
|
||||||
|
var = "OPENAI_API_KEY".bold(),
|
||||||
|
url = "https://platform.openai.com/account/api-keys".bold().underline(),
|
||||||
|
);
|
||||||
|
std::process::exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
if !skip_git_repo_check && !is_inside_git_repo() {
|
if !skip_git_repo_check && !is_inside_git_repo() {
|
||||||
eprintln!("Not inside a Git repo and --skip-git-repo-check was not specified.");
|
eprintln!("Not inside a Git repo and --skip-git-repo-check was not specified.");
|
||||||
std::process::exit(1);
|
std::process::exit(1);
|
||||||
|
|||||||
Reference in New Issue
Block a user