Skip to content

Commit d8b3657

Browse files
committed
Merge branch 'add_version' into 'main'
feat(cli): runtime version override + ws auth-hint fix See merge request mobilex/web3/OKOutcomes!27
2 parents 3fcf040 + 5d83f0e commit d8b3657

4 files changed

Lines changed: 55 additions & 11 deletions

File tree

src/app.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
//! cargo run --bin okx-outcomes -- search "world cup" # search
99
//! ```
1010
11-
use super::{APP_NAME, APP_VERSION};
11+
use super::APP_NAME;
1212

1313
/// Run the predictions CLI application.
1414
pub async fn run() {
@@ -31,7 +31,7 @@ pub async fn run() {
3131
pub(super) async fn dispatch(raw_args: &[String]) {
3232
// Version flag — print and return before any command parsing.
3333
if raw_args.iter().any(|a| a == "--version" || a == "-V") {
34-
println!("{APP_NAME} v{APP_VERSION}");
34+
println!("{APP_NAME} v{}", super::version());
3535
return;
3636
}
3737

@@ -145,7 +145,7 @@ fn filter_outer_flags(args: &[String]) -> Vec<&str> {
145145
}
146146

147147
fn print_help() {
148-
println!("OKX Outcomes CLI v{APP_VERSION}");
148+
println!("OKX Outcomes CLI v{}", super::version());
149149
println!();
150150
println!("USAGE:");
151151
println!(" {APP_NAME} [COMMAND] [OPTIONS]");
@@ -237,7 +237,7 @@ fn print_help() {
237237
println!(" {APP_NAME} account positions -j JSON positions");
238238
println!(" {APP_NAME} clob create-order Place order");
239239
println!(" {APP_NAME} ctf split --market 1 --amount 100 Split position");
240-
println!(" {APP_NAME} ws prices 100170100 Stream prices");
240+
println!(" {APP_NAME} ws prices 101663000 Stream prices");
241241
println!(" {APP_NAME} status Check API connectivity");
242242
println!(" {APP_NAME} shell Enter REPL mode");
243243
}

src/lib.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,28 @@
1919
pub const APP_NAME: &str = "okx-outcomes";
2020

2121
/// CLI version, sourced from `[package].version` in `Cargo.toml` at compile
22-
/// time. Surfaced via `--version`/`-V` and in the help header.
22+
/// time. The default surfaced via `--version`/`-V` and the help header,
23+
/// unless an embedding binary overrides it via [`set_version`].
2324
pub const APP_VERSION: &str = env!("CARGO_PKG_VERSION");
2425

26+
/// Override the version string shown by `--version`/`-V` and the help
27+
/// header. Lets an embedding binary report its own / effective version
28+
/// instead of this crate's. One-shot, like [`set_http_client`].
29+
pub fn set_version(version: String) -> Result<(), String> {
30+
CUSTOM_VERSION.set(version)
31+
}
32+
33+
pub(crate) static CUSTOM_VERSION: std::sync::OnceLock<String> = std::sync::OnceLock::new();
34+
35+
/// Effective version: the injected override if any, else the compile-time
36+
/// [`APP_VERSION`].
37+
pub(crate) fn version() -> &'static str {
38+
CUSTOM_VERSION
39+
.get()
40+
.map(String::as_str)
41+
.unwrap_or(APP_VERSION)
42+
}
43+
2544
/// Override the HTTP client used by the SDK for every REST request.
2645
///
2746
/// Must be called before any command dispatches (typically at the top of

src/setup_cmds.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,31 @@ pub fn maybe_print_setup_hint(positional: &[&str]) {
7272
if clob_public_read {
7373
return;
7474
}
75+
// `ws` is likewise mixed, but its public channels are an open set (any
76+
// `candle*` bar, plus future additions), so we identify the *private*
77+
// subcommands instead and treat everything else as public. This keeps a
78+
// single source of truth with the private dispatch arms in `ws/cmds.rs`:
79+
// terminal/orders/positions/balance/user-trades/pnl/private all need an
80+
// account; prices/books/trades/tickers/event-status/candle* don't.
81+
let ws_public_read = positional.first().map(|s| s.to_lowercase()).as_deref() == Some("ws")
82+
&& positional
83+
.get(1)
84+
.map(|s| s.to_lowercase())
85+
.is_some_and(|s| {
86+
!matches!(
87+
s.as_str(),
88+
"terminal"
89+
| "orders"
90+
| "positions"
91+
| "balance"
92+
| "user-trades"
93+
| "pnl"
94+
| "private"
95+
)
96+
});
97+
if ws_public_read {
98+
return;
99+
}
75100
// Only nag when the user has no way to authenticate at all — neither a live
76101
// API key in `~/.okx/config.toml` nor an OAuth session. Check the cheap file
77102
// read first so the broker subprocess is skipped whenever a key is present.

src/ws/cmds.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ pub async fn run_ws_command(args: &[&str], json_mode: bool) {
127127
if inst_ids.is_empty() && !no_id_ok {
128128
crate::util::mark_failed();
129129
raw_println!("Error: at least one assetId is required.");
130-
raw_println!("Example: {APP_NAME} ws prices 100888000");
130+
raw_println!("Example: {APP_NAME} ws prices 101663000");
131131
return;
132132
}
133133

@@ -532,11 +532,11 @@ Output modes:
532532
--json / -j NDJSON: one compact JSON object per push, suitable for piping
533533
534534
Examples:
535-
{APP_NAME} ws prices 100170100
536-
{APP_NAME} ws books 100170100 100888000
537-
{APP_NAME} ws trades 100170100 --json
538-
{APP_NAME} ws trades 100170100 --json | jq '.px'
539-
{APP_NAME} ws candle15m 100170100
535+
{APP_NAME} ws prices 101663000
536+
{APP_NAME} ws books 101663000
537+
{APP_NAME} ws trades 101663000 --json
538+
{APP_NAME} ws trades 101663000 --json | jq '.px'
539+
{APP_NAME} ws candle15m 101663000
540540
{APP_NAME} ws orders # private; needs credentials from `setup`
541541
"#
542542
);

0 commit comments

Comments
 (0)