1pub mod auth;
2pub mod config_cmd;
3pub mod identity;
4pub mod intent;
5pub mod poi;
6pub mod proof;
7pub mod watch;
8
9use clap::{Parser, Subcommand};
10
11use crate::errors::CairnError;
12
13#[derive(Parser, Debug)]
17#[command(name = "cairn", version, about, long_about = None)]
18pub struct Cli {
19 #[arg(long, default_value = "json", global = true, env = "CAIRN_OUTPUT")]
21 pub output: String,
22
23 #[arg(long, short, global = true)]
25 pub quiet: bool,
26
27 #[arg(long, global = true, env = "BACKPAC_API_URL")]
29 pub api_url: Option<String>,
30
31 #[arg(long, global = true, env = "BACKPAC_JWT")]
33 pub jwt: Option<String>,
34
35 #[arg(long, global = true, env = "CAIRN_CHAIN")]
37 pub chain: Option<String>,
38
39 #[arg(long, global = true, env = "CAIRN_NETWORK")]
41 pub network: Option<String>,
42
43 #[command(subcommand)]
44 pub command: Commands,
45}
46
47#[derive(Subcommand, Debug)]
48pub enum Commands {
49 Auth(auth::AuthArgs),
51
52 Identity(identity::IdentityArgs),
54
55 Poi(poi::PoiArgs),
57
58 Intent(intent::IntentArgs),
60
61 Proof(proof::ProofArgs),
63
64 Config(config_cmd::ConfigArgs),
66
67 Watch(watch::WatchArgs),
69}
70
71impl Cli {
72 pub async fn execute(&self) -> Result<(), CairnError> {
73 match &self.command {
74 Commands::Auth(args) => args.execute(self).await,
75 Commands::Identity(args) => args.execute(self).await,
76 Commands::Poi(args) => args.execute(self).await,
77 Commands::Intent(args) => args.execute(self).await,
78 Commands::Proof(args) => args.execute(self).await,
79 Commands::Config(args) => args.execute(self).await,
80 Commands::Watch(args) => args.execute(self).await,
81 }
82 }
83}
84
85pub fn output_json(value: &serde_json::Value, format: &str) {
87 if format == "text" {
88 if let Some(obj) = value.as_object() {
90 for (k, v) in obj {
91 match v {
92 serde_json::Value::String(s) => println!("{}={}", k, s),
93 serde_json::Value::Null => println!("{}=null", k),
94 _ => println!("{}={}", k, v),
95 }
96 }
97 } else {
98 println!("{}", value);
99 }
100 } else {
101 println!("{}", serde_json::to_string_pretty(value).unwrap());
102 }
103}