1pub mod auth;
2pub mod config_cmd;
3pub mod identity;
4pub mod intent;
5pub mod poi;
6pub mod proof;
7pub mod watch;
8pub mod rpc;
9pub mod receive;
10
11use clap::{Parser, Subcommand};
12
13use crate::errors::CairnError;
14
15#[derive(Parser, Debug)]
19#[command(name = "cairn", version, about, long_about = None)]
20pub struct Cli {
21 #[arg(long, default_value = "json", global = true, env = "CAIRN_OUTPUT")]
23 pub output: String,
24
25 #[arg(long, short, global = true)]
27 pub quiet: bool,
28
29 #[arg(long, global = true, env = "BACKPAC_API_URL")]
31 pub api_url: Option<String>,
32
33 #[arg(long, global = true, env = "BACKPAC_JWT")]
35 pub jwt: Option<String>,
36
37 #[arg(long, global = true, env = "CAIRN_CHAIN")]
39 pub chain: Option<String>,
40
41 #[arg(long, global = true, env = "CAIRN_NETWORK")]
43 pub network: Option<String>,
44
45 #[arg(long, global = true, env = "BACKPAC_WORKER_URL")]
47 pub worker_url: Option<String>,
48
49 #[command(subcommand)]
50 pub command: Commands,
51}
52
53#[derive(Subcommand, Debug)]
54pub enum Commands {
55 Auth(auth::AuthArgs),
57
58 Identity(identity::IdentityArgs),
60
61 Poi(poi::PoiArgs),
63
64 Intent(intent::IntentArgs),
66
67 Proof(proof::ProofArgs),
69
70 Config(config_cmd::ConfigArgs),
72
73 Watch(watch::WatchArgs),
75
76 #[command(flatten)]
78 Rpc(rpc::RpcCommands),
79
80 Receive(receive::ReceiveArgs),
82}
83
84impl Cli {
85 pub async fn execute(&self) -> Result<(), CairnError> {
86 match &self.command {
87 Commands::Auth(args) => args.execute(self).await,
88 Commands::Identity(args) => args.execute(self).await,
89 Commands::Poi(args) => args.execute(self).await,
90 Commands::Intent(args) => args.execute(self).await,
91 Commands::Proof(args) => args.execute(self).await,
92 Commands::Config(args) => args.execute(self).await,
93 Commands::Watch(args) => args.execute(self).await,
94 Commands::Rpc(cmd) => cmd.execute(self).await,
95 Commands::Receive(args) => args.execute(self).await,
96 }
97 }
98}
99
100pub fn output_json(value: &serde_json::Value, format: &str) {
102 if format == "text" {
103 if let Some(obj) = value.as_object() {
105 for (k, v) in obj {
106 match v {
107 serde_json::Value::String(s) => println!("{}={}", k, s),
108 serde_json::Value::Null => println!("{}=null", k),
109 _ => println!("{}={}", k, v),
110 }
111 }
112 } else {
113 println!("{}", value);
114 }
115 } else {
116 println!("{}", serde_json::to_string_pretty(value).unwrap());
117 }
118}