newton_cli/cli/
mod.rs

1use crate::{
2    commands::{
3        policy::PolicyCommand, policy_client::PolicyClientCommand, policy_data::PolicyDataCommand,
4        policy_files::PolicyFilesCommand, task::TaskCommand,
5    },
6    config::NewtonCliConfig,
7};
8use clap::{Parser, Subcommand};
9use newton_cli_runner::NewtonRunner;
10use newton_prover_core::config::{
11    log::{init_logger, LogFormat, LoggerConfig},
12    NewtonAvsConfigBuilder,
13};
14use std::{ffi::OsString, path::PathBuf};
15use tracing::info;
16
17/// newton protocol cli entry point interface
18#[derive(Debug, Parser)]
19#[command(author, about = "Newton protocol cli", long_about = None)]
20pub struct NewtonCli {
21    /// chain id
22    #[arg(long, value_name = "CHAIN_ID", global = true, env = "CHAIN_ID")]
23    chain_id: Option<u64>,
24
25    /// optional path to the configuration file
26    #[arg(long, value_name = "FILE", global = true)]
27    config_path: Option<PathBuf>,
28
29    /// log format
30    #[arg(
31        long,
32        value_enum,
33        default_value = "minimal",
34        global = true,
35        help = "Log format: full, compact, pretty, json, or minimal"
36    )]
37    log_format: LogFormat,
38
39    /// Suppress verbose config loading logs
40    #[arg(long, global = true)]
41    quiet: bool,
42
43    #[command(subcommand)]
44    command: Commands,
45}
46
47impl NewtonCli {
48    /// parsers only the default cli arguments
49    pub fn parse_args() -> Self {
50        Self::parse()
51    }
52
53    /// parsers only the default cli arguments from the given iterator
54    pub fn try_parse_args_from<I, T>(itr: I) -> Result<Self, clap::error::Error>
55    where
56        I: IntoIterator<Item = T>,
57        T: Into<OsString> + Clone,
58    {
59        Self::try_parse_from(itr)
60    }
61
62    /// execute the configured cli command.
63    pub fn run(self) -> eyre::Result<()> {
64        // Build log filter - suppress verbose config loading logs by default
65        let env_filter = "warn,newton_cli=info".to_string();
66
67        // Initialize logging with specified format
68        init_logger(LoggerConfig::new(self.log_format).with_env_filter(env_filter));
69
70        if self.chain_id.is_none() {
71            eyre::bail!("chain id is required");
72        }
73        let chain_id = self.chain_id.unwrap();
74
75        let runner = NewtonRunner::default();
76
77        let mut builder = NewtonAvsConfigBuilder::new(chain_id);
78        if let Some(config_path) = self.config_path {
79            info!("Loading cli config from: {:?}", config_path);
80            builder = builder.with_service_path(config_path);
81        }
82        let config = builder.build::<NewtonCliConfig>()?;
83
84        match self.command {
85            Commands::Task(command) => runner.run_blocking_until_ctrl_c(Box::new(command).execute(config))?,
86            Commands::PolicyData(command) => runner.run_blocking_until_ctrl_c(Box::new(command).execute(config))?,
87            Commands::Policy(command) => runner.run_blocking_until_ctrl_c(Box::new(command).execute(config))?,
88            Commands::PolicyFiles(command) => runner.run_blocking_until_ctrl_c(Box::new(command).execute(config))?,
89            Commands::PolicyClient(command) => runner.run_blocking_until_ctrl_c(Box::new(command).execute(config))?,
90        }
91
92        Ok(())
93    }
94}
95
96/// commands to be executed
97#[derive(Debug, Subcommand)]
98pub enum Commands {
99    /// task commands
100    Task(TaskCommand),
101
102    /// policy data commands
103    PolicyData(PolicyDataCommand),
104
105    /// policy commands
106    Policy(PolicyCommand),
107
108    /// policy files commands
109    PolicyFiles(PolicyFilesCommand),
110
111    /// policy client commands
112    #[command(name = "policy-client")]
113    PolicyClient(PolicyClientCommand),
114}