1pub mod cli;
2pub mod commands;
3pub(crate) mod tui;
4pub mod utils;
5
6use anyhow::Result;
7use clap::{CommandFactory, Parser};
8
9use crate::cli::{BashersApp, TOPLEVEL_ALIAS_PARENTS};
10
11pub fn run(args: Vec<String>) -> Result<()> {
12 let mut args = args;
13 if let Some(name) = args.get(1).map(String::as_str) {
14 let root = BashersApp::command();
15 let is_root_subcommand = root.get_subcommands().any(|c| c.get_name() == name);
16 if !is_root_subcommand {
17 if let Some(parent) = root.get_subcommands().find(|parent| {
18 TOPLEVEL_ALIAS_PARENTS.contains(&parent.get_name())
19 && parent.get_subcommands().any(|c| c.get_name() == name)
20 }) {
21 args.insert(1, parent.get_name().to_string());
22 }
23 }
24 }
25 let app = BashersApp::parse_from(args);
26
27 match app.command {
28 Some(cli::Commands::Update {
29 package,
30 dry_run,
31 auto_select,
32 }) => commands::update::run(package.as_deref(), dry_run, auto_select)?,
33 Some(cli::Commands::Setup {
34 frozen,
35 rm,
36 dry_run,
37 }) => commands::setup::run(frozen, rm, dry_run)?,
38 Some(cli::Commands::Show { patterns }) => commands::show::run(&patterns)?,
39 Some(cli::Commands::Git { command }) => match command {
40 cli::GitCommands::Sync { current, dry_run } => {
41 commands::git::sync::run(current, dry_run)?
42 }
43 },
44 Some(cli::Commands::Kube { command }) => match command {
45 cli::KubeCommands::Kmg { pattern } => commands::kube::kmg::run(&pattern)?,
46 cli::KubeCommands::Track {
47 patterns,
48 err_only,
49 simple,
50 } => commands::kube::track::run(&patterns, err_only, simple)?,
51 },
52 Some(cli::Commands::Docker { command }) => match command {
53 cli::DockerCommands::Build {
54 dockerfile,
55 tag,
56 no_cache,
57 context,
58 } => commands::docker::build::run(
59 dockerfile.as_deref(),
60 tag.as_deref(),
61 no_cache,
62 context.as_deref(),
63 )?,
64 },
65 Some(cli::Commands::Version) => println!("v{}", env!("CARGO_PKG_VERSION")),
66 Some(cli::Commands::Watch {
67 command,
68 interval,
69 no_diff,
70 }) => commands::watch::run(&command, interval, no_diff)?,
71 Some(cli::Commands::SelfCmd { command }) => match command {
72 cli::SelfCommands::Update => commands::self_cmd::update::run()?,
73 },
74 None => commands::help::run()?,
75 }
76
77 Ok(())
78}
79
80#[cfg(feature = "pyo3")]
81use pyo3::prelude::*;
82
83#[cfg(feature = "pyo3")]
84#[pyfunction]
85fn run_cli(py: Python<'_>) -> PyResult<()> {
86 let sys = py.import("sys")?;
87 let argv: Vec<String> = sys
88 .getattr("argv")?
89 .extract::<Vec<String>>()?;
90 let args = if argv.len() <= 1 {
91 vec!["bashers".to_string()]
92 } else {
93 let mut a = vec!["bashers".to_string()];
94 a.extend(argv[1..].to_vec());
95 a
96 };
97 run(args).map_err(|e| pyo3::exceptions::PyRuntimeError::new_err(e.to_string()))
98}
99
100#[cfg(feature = "pyo3")]
101#[pymodule]
102fn bashers(m: &Bound<'_, pyo3::types::PyModule>) -> PyResult<()> {
103 m.add_function(pyo3::wrap_pyfunction!(run_cli, m)?)?;
104 Ok(())
105}