newton_cli/cli/
mod.rs

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