opcda-bridge-client 0.2.6

OPC DA cross-platform client
Documentation
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
use crate::output::OutputFormat;
use clap::{Parser, Subcommand};
use std::path::PathBuf;

#[derive(Parser)]
#[command(name = "opcda-bridge", about = "OPC DA bridge client", version)]
pub struct Cli {
    // `global = true` on these four lets them be passed either before or
    // after the subcommand (e.g. both `--json read ...` and `read ... --json`
    // work), instead of clap's default of requiring them before it.
    #[arg(long, env = "OPC_BRIDGE_HOST", global = true)]
    pub host: Option<String>,

    /// Path to a TOML config file (default: platform config dir, see README)
    #[arg(long, value_name = "PATH", global = true)]
    pub config: Option<PathBuf>,

    /// Output format: `table` (default) or `json`
    #[arg(
        long,
        value_enum,
        value_name = "FORMAT",
        env = "OPC_BRIDGE_OUTPUT",
        global = true
    )]
    pub output: Option<OutputFormat>,

    /// Shorthand for `--output json`. If both are set, `--json` wins.
    #[arg(long, global = true)]
    pub json: bool,

    #[command(subcommand)]
    pub command: Commands,
}

#[derive(Subcommand)]
pub enum Commands {
    /// List available OPC DA servers
    Servers,
    /// Browse tags on a server
    Browse {
        /// OPC DA server ProgID (falls back to the config file's `server` key)
        #[arg(long)]
        server: Option<String>,
        /// Flat list (skip tree structure)
        #[arg(long)]
        flat: bool,
        /// Path to browse (default: root). Pass a `Branch` tag from a prior
        /// browse to drill down one level further.
        #[arg(long, default_value = "")]
        path: String,
        /// Cap on the number of tags streamed back (default: 1000)
        #[arg(long)]
        max_tags: Option<u32>,
    },
    /// Read tag values
    Read {
        /// OPC DA server ProgID (falls back to the config file's `server` key)
        #[arg(long)]
        server: Option<String>,
        /// Tag IDs to read
        tags: Vec<String>,
    },
    /// Write a value to a tag
    Write {
        /// OPC DA server ProgID (falls back to the config file's `server` key)
        #[arg(long)]
        server: Option<String>,
        /// Tag ID to write
        tag: String,
        /// Value to write (parsed as bool, int, float, or string)
        value: String,
    },
}

/// Dispatch a parsed `Cli` to the requested subcommand.
///
/// Takes an already-loaded `config` and already-resolved `format` rather
/// than loading the config itself, so a config-load failure (handled by
/// the caller, `lib::run`) can be reported in the right format even before
/// this function would otherwise learn the config file's `output` key.
pub async fn run_command(
    cli: Cli,
    config: &crate::config::ClientConfig,
    format: OutputFormat,
) -> anyhow::Result<()> {
    let host = crate::config::resolve_host(cli.host, config);

    match cli.command {
        Commands::Servers => crate::commands::cmd_servers(host, format).await?,
        Commands::Browse {
            server,
            flat,
            path,
            max_tags,
        } => {
            let server = crate::config::resolve_server(server, config)?;
            let max_tags = crate::config::resolve_max_tags(max_tags, config);
            crate::commands::cmd_browse(host, server, flat, path, max_tags, format).await?
        }
        Commands::Read { server, tags } => {
            let server = crate::config::resolve_server(server, config)?;
            crate::commands::cmd_read(host, server, tags, format).await?
        }
        Commands::Write { server, tag, value } => {
            let server = crate::config::resolve_server(server, config)?;
            crate::commands::cmd_write(host, server, tag, value, format).await?
        }
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::test_support::{MockBridgeService, start_mock_server};
    use clap::Parser;
    use opcda_bridge_proto::bridge::{BrowseResponse, WriteResponse};
    use std::sync::Mutex;

    // std::env::set_var/remove_var mutate process-global state, but `cargo
    // test` runs tests in parallel threads by default, so the tests below
    // that touch OPC_BRIDGE_HOST race with each other unless serialized.
    static ENV_MUTEX: Mutex<()> = Mutex::new(());

    #[tokio::test]
    async fn test_run_command_servers() {
        let host = start_mock_server(MockBridgeService::default()).await;
        let cli = Cli {
            host: Some(host),
            config: None,
            output: None,
            json: false,
            command: Commands::Servers,
        };
        run_command(
            cli,
            &crate::config::ClientConfig::default(),
            OutputFormat::Table,
        )
        .await
        .unwrap();
    }

    #[tokio::test]
    async fn test_run_command_browse() {
        let host = start_mock_server(MockBridgeService::default()).await;
        let cli = Cli {
            host: Some(host),
            config: None,
            output: None,
            json: false,
            command: Commands::Browse {
                server: Some("S".into()),
                flat: false,
                path: String::new(),
                max_tags: None,
            },
        };
        run_command(
            cli,
            &crate::config::ClientConfig::default(),
            OutputFormat::Table,
        )
        .await
        .unwrap();
    }

    #[tokio::test]
    async fn test_run_command_browse_no_server_errors() {
        let host = start_mock_server(MockBridgeService::default()).await;
        let cli = Cli {
            host: Some(host),
            config: None,
            output: None,
            json: false,
            command: Commands::Browse {
                server: None,
                flat: false,
                path: String::new(),
                max_tags: None,
            },
        };
        let err = run_command(
            cli,
            &crate::config::ClientConfig::default(),
            OutputFormat::Table,
        )
        .await
        .unwrap_err();
        assert!(err.to_string().contains("no OPC server specified"));
    }

    #[tokio::test]
    async fn test_run_command_read() {
        let host = start_mock_server(MockBridgeService::default()).await;
        let cli = Cli {
            host: Some(host),
            config: None,
            output: None,
            json: false,
            command: Commands::Read {
                server: Some("S".into()),
                tags: vec![],
            },
        };
        run_command(
            cli,
            &crate::config::ClientConfig::default(),
            OutputFormat::Table,
        )
        .await
        .unwrap();
    }

    #[tokio::test]
    async fn test_run_command_write() {
        let host = start_mock_server(MockBridgeService {
            write_response: WriteResponse {
                tag_id: "t".into(),
                success: true,
                error: None,
            },
            ..Default::default()
        })
        .await;
        let cli = Cli {
            host: Some(host),
            config: None,
            output: None,
            json: false,
            command: Commands::Write {
                server: Some("S".into()),
                tag: "t".into(),
                value: "hello".into(),
            },
        };
        run_command(
            cli,
            &crate::config::ClientConfig::default(),
            OutputFormat::Table,
        )
        .await
        .unwrap();
    }

    #[tokio::test]
    async fn test_browse_drop_triggers_break() {
        use opcda_bridge_proto::bridge::BrowseRequest;
        use opcda_bridge_proto::bridge::bridge_client::BridgeClient;
        let host = start_mock_server(MockBridgeService {
            browse_responses: (0..300)
                .map(|i| BrowseResponse {
                    tag_id: format!("tag{i}"),
                    node_type: "Leaf".into(),
                })
                .collect(),
            ..Default::default()
        })
        .await;
        let mut client = BridgeClient::connect(format!("http://{host}"))
            .await
            .unwrap();
        use tokio_stream::StreamExt;
        let mut stream = client
            .browse(BrowseRequest {
                server: "S".into(),
                flat: false,
                path: String::new(),
                max_tags: 1000,
            })
            .await
            .unwrap()
            .into_inner();
        let _first = stream.next().await;
        drop(stream);
        tokio::task::yield_now().await;
        tokio::task::yield_now().await;
    }

    #[test]
    fn test_cli_default_host() {
        let _guard = ENV_MUTEX.lock().unwrap_or_else(|e| e.into_inner());
        // The mutex serializes these Rust 2024 unsafe environment mutations.
        // nosemgrep: rust.lang.security.unsafe-usage.unsafe-usage
        unsafe { std::env::remove_var("OPC_BRIDGE_HOST") };
        let args = Cli::try_parse_from(["opcda-bridge", "servers"]).unwrap();
        assert_eq!(args.host, None);
    }

    #[test]
    fn test_cli_version_flag() {
        let err = Cli::try_parse_from(["opcda-bridge", "--version"])
            .err()
            .unwrap();
        assert_eq!(err.kind(), clap::error::ErrorKind::DisplayVersion);
        assert!(err.to_string().contains(env!("CARGO_PKG_VERSION")));
    }

    #[test]
    fn test_cli_custom_host() {
        let args =
            Cli::try_parse_from(["opcda-bridge", "--host", "192.168.1.1:9999", "servers"]).unwrap();
        assert_eq!(args.host, Some("192.168.1.1:9999".to_string()));
    }

    #[test]
    fn test_cli_global_flags_after_subcommand() {
        // host/config/output/json are `global = true` so they can be placed
        // after the subcommand too, not just before it.
        let args = Cli::try_parse_from([
            "opcda-bridge",
            "read",
            "--server",
            "MyServer",
            "tag1",
            "--host",
            "192.168.1.1:9999",
            "--json",
        ])
        .unwrap();
        assert_eq!(args.host, Some("192.168.1.1:9999".to_string()));
        assert!(args.json);
    }

    #[test]
    fn test_cli_config_flag() {
        let args =
            Cli::try_parse_from(["opcda-bridge", "--config", "custom.toml", "servers"]).unwrap();
        assert_eq!(args.config, Some(PathBuf::from("custom.toml")));
    }

    #[test]
    fn test_cli_servers_command() {
        let args = Cli::try_parse_from(["opcda-bridge", "servers"]).unwrap();
        assert!(matches!(args.command, Commands::Servers));
    }

    #[test]
    fn test_cli_browse_command() {
        let args =
            Cli::try_parse_from(["opcda-bridge", "browse", "--server", "MyServer", "--flat"])
                .unwrap();
        assert!(matches!(
            args.command,
            Commands::Browse { ref server, flat, .. } if server.as_deref() == Some("MyServer") && flat
        ));
    }

    #[test]
    fn test_cli_browse_no_flat() {
        let args = Cli::try_parse_from(["opcda-bridge", "browse", "--server", "MyServer"]).unwrap();
        assert!(matches!(
            args.command,
            Commands::Browse { ref server, flat: false, .. } if server.as_deref() == Some("MyServer")
        ));
    }

    #[test]
    fn test_cli_browse_no_server() {
        let args = Cli::try_parse_from(["opcda-bridge", "browse"]).unwrap();
        assert!(matches!(
            args.command,
            Commands::Browse { server: None, .. }
        ));
    }

    #[test]
    fn test_cli_browse_max_tags() {
        let args = Cli::try_parse_from([
            "opcda-bridge",
            "browse",
            "--server",
            "MyServer",
            "--max-tags",
            "50",
        ])
        .unwrap();
        assert!(matches!(
            args.command,
            Commands::Browse {
                max_tags: Some(50),
                ..
            }
        ));
    }

    #[test]
    fn test_cli_browse_default_path_is_root() {
        let args = Cli::try_parse_from(["opcda-bridge", "browse", "--server", "MyServer"]).unwrap();
        assert!(matches!(
            args.command,
            Commands::Browse { ref path, .. } if path.is_empty()
        ));
    }

    #[test]
    fn test_cli_browse_path_flag() {
        let args = Cli::try_parse_from([
            "opcda-bridge",
            "browse",
            "--server",
            "MyServer",
            "--path",
            "Simulink.Device1",
        ])
        .unwrap();
        assert!(matches!(
            args.command,
            Commands::Browse { ref path, .. } if path == "Simulink.Device1"
        ));
    }

    #[test]
    fn test_cli_read_command() {
        let args = Cli::try_parse_from([
            "opcda-bridge",
            "read",
            "--server",
            "MyServer",
            "tag1",
            "tag2",
            "tag3",
        ])
        .unwrap();
        assert!(matches!(
            args.command,
            Commands::Read { ref server, ref tags }
                if server.as_deref() == Some("MyServer")
                    && tags == &vec!["tag1".to_string(), "tag2".to_string(), "tag3".to_string()]
        ));
    }

    #[test]
    fn test_cli_read_no_tags() {
        let args = Cli::try_parse_from(["opcda-bridge", "read", "--server", "MyServer"]).unwrap();
        assert!(matches!(
            args.command,
            Commands::Read { ref server, ref tags } if server.as_deref() == Some("MyServer") && tags.is_empty()
        ));
    }

    #[test]
    fn test_cli_write_command() {
        let args = Cli::try_parse_from([
            "opcda-bridge",
            "write",
            "--server",
            "MyServer",
            "Tag1",
            "42",
        ])
        .unwrap();
        assert!(matches!(
            args.command,
            Commands::Write { ref server, ref tag, ref value }
                if server.as_deref() == Some("MyServer") && tag == "Tag1" && value == "42"
        ));
    }

    #[test]
    fn test_cli_host_from_env() {
        let _guard = ENV_MUTEX.lock().unwrap_or_else(|e| e.into_inner());
        // The mutex serializes these Rust 2024 unsafe environment mutations.
        // nosemgrep: rust.lang.security.unsafe-usage.unsafe-usage
        unsafe { std::env::set_var("OPC_BRIDGE_HOST", "envhost:8888") };
        let args = Cli::try_parse_from(["opcda-bridge", "servers"]).unwrap();
        assert_eq!(args.host, Some("envhost:8888".to_string()));
        // nosemgrep: rust.lang.security.unsafe-usage.unsafe-usage
        unsafe { std::env::remove_var("OPC_BRIDGE_HOST") };
    }

    #[test]
    fn test_cli_arg_overrides_env() {
        let _guard = ENV_MUTEX.lock().unwrap_or_else(|e| e.into_inner());
        // The mutex serializes these Rust 2024 unsafe environment mutations.
        // nosemgrep: rust.lang.security.unsafe-usage.unsafe-usage
        unsafe { std::env::set_var("OPC_BRIDGE_HOST", "envhost:8888") };
        let args =
            Cli::try_parse_from(["opcda-bridge", "--host", "arghost:7777", "servers"]).unwrap();
        assert_eq!(args.host, Some("arghost:7777".to_string()));
        // nosemgrep: rust.lang.security.unsafe-usage.unsafe-usage
        unsafe { std::env::remove_var("OPC_BRIDGE_HOST") };
    }

    #[test]
    fn test_cli_default_output_is_none() {
        // OPC_BRIDGE_OUTPUT is read by every Cli::try_parse_from call, so
        // this must be guarded/cleared just like test_cli_default_host,
        // or a concurrently-running env-setting test in another thread
        // could leak a value in here.
        let _guard = ENV_MUTEX.lock().unwrap_or_else(|e| e.into_inner());
        // The mutex serializes this Rust 2024 unsafe environment mutation.
        // nosemgrep: rust.lang.security.unsafe-usage.unsafe-usage
        unsafe { std::env::remove_var("OPC_BRIDGE_OUTPUT") };
        let args = Cli::try_parse_from(["opcda-bridge", "servers"]).unwrap();
        assert_eq!(args.output, None);
        assert!(!args.json);
    }

    #[test]
    fn test_cli_output_table_flag() {
        let args = Cli::try_parse_from(["opcda-bridge", "--output", "table", "servers"]).unwrap();
        assert_eq!(args.output, Some(OutputFormat::Table));
    }

    #[test]
    fn test_cli_output_json_flag() {
        let args = Cli::try_parse_from(["opcda-bridge", "--output", "json", "servers"]).unwrap();
        assert_eq!(args.output, Some(OutputFormat::Json));
    }

    #[test]
    fn test_cli_json_shorthand_flag() {
        // See test_cli_default_output_is_none: args.output is asserted here
        // too, so this needs the same guard/clear.
        let _guard = ENV_MUTEX.lock().unwrap_or_else(|e| e.into_inner());
        // The mutex serializes this Rust 2024 unsafe environment mutation.
        // nosemgrep: rust.lang.security.unsafe-usage.unsafe-usage
        unsafe { std::env::remove_var("OPC_BRIDGE_OUTPUT") };
        let args = Cli::try_parse_from(["opcda-bridge", "--json", "servers"]).unwrap();
        assert!(args.json);
        assert_eq!(args.output, None);
    }

    #[test]
    fn test_cli_output_from_env() {
        let _guard = ENV_MUTEX.lock().unwrap_or_else(|e| e.into_inner());
        // The mutex serializes these Rust 2024 unsafe environment mutations.
        // nosemgrep: rust.lang.security.unsafe-usage.unsafe-usage
        unsafe { std::env::set_var("OPC_BRIDGE_OUTPUT", "json") };
        let args = Cli::try_parse_from(["opcda-bridge", "servers"]).unwrap();
        assert_eq!(args.output, Some(OutputFormat::Json));
        // nosemgrep: rust.lang.security.unsafe-usage.unsafe-usage
        unsafe { std::env::remove_var("OPC_BRIDGE_OUTPUT") };
    }

    #[test]
    fn test_cli_json_flag_with_output_env_set_both_parse() {
        // `--json` and `--output` (even env-sourced) are not declared as
        // clap conflicts: resolve_from_cli resolves the precedence in code
        // (--json always wins) instead, so both can be present here.
        let _guard = ENV_MUTEX.lock().unwrap_or_else(|e| e.into_inner());
        // The mutex serializes these Rust 2024 unsafe environment mutations.
        // nosemgrep: rust.lang.security.unsafe-usage.unsafe-usage
        unsafe { std::env::set_var("OPC_BRIDGE_OUTPUT", "table") };
        let args = Cli::try_parse_from(["opcda-bridge", "--json", "servers"]).unwrap();
        assert!(args.json);
        assert_eq!(args.output, Some(OutputFormat::Table));
        assert_eq!(
            crate::output::resolve_from_cli(&args),
            Some(OutputFormat::Json)
        );
        // nosemgrep: rust.lang.security.unsafe-usage.unsafe-usage
        unsafe { std::env::remove_var("OPC_BRIDGE_OUTPUT") };
    }

    #[test]
    fn test_resolve_from_cli_json_wins_over_output() {
        let args = Cli::try_parse_from(["opcda-bridge", "--json", "--output", "table", "servers"])
            .unwrap();
        assert_eq!(
            crate::output::resolve_from_cli(&args),
            Some(OutputFormat::Json)
        );
    }

    #[test]
    fn test_resolve_from_cli_output_only() {
        let args = Cli::try_parse_from(["opcda-bridge", "--output", "json", "servers"]).unwrap();
        assert_eq!(
            crate::output::resolve_from_cli(&args),
            Some(OutputFormat::Json)
        );
    }

    #[test]
    fn test_resolve_from_cli_neither_set() {
        // args.output is env-sensitive when neither --output nor --json is
        // passed, so this needs the same guard/clear as
        // test_cli_default_output_is_none.
        let _guard = ENV_MUTEX.lock().unwrap_or_else(|e| e.into_inner());
        // The mutex serializes this Rust 2024 unsafe environment mutation.
        // nosemgrep: rust.lang.security.unsafe-usage.unsafe-usage
        unsafe { std::env::remove_var("OPC_BRIDGE_OUTPUT") };
        let args = Cli::try_parse_from(["opcda-bridge", "servers"]).unwrap();
        assert_eq!(crate::output::resolve_from_cli(&args), None);
    }
}