1use anyhow::Result;
2use clap::Parser;
3
4use crate::{
5 _use,
6 config::{count_config_path, count_remotes_index_path, FgmContext},
7 current_version, gen_completions, init_script, install, list_installed, list_remote, uninstall,
8 update,
9};
10
11#[derive(Parser, Debug)]
12#[clap(name = "fgm", version = env!("CARGO_PKG_VERSION"), bin_name = "fgm",about,long_about = None)]
13pub struct Cli {
14 #[clap(subcommand)]
15 pub sub: Subcommand,
16}
17
18#[derive(Parser, Debug, Clone)]
19pub enum Subcommand {
20 Install {
22 version: String,
23 #[clap(short, long)]
24 update: bool,
25 },
26 #[clap(name = "list",visible_aliases = &["ls"])]
28 LsLocal {
29 #[clap(short, long)]
31 sort: bool,
32 #[clap(short, long)]
34 reverse: bool,
35 },
36 #[clap(name = "list-remote",visible_aliases = &["ls-remote"])]
38 LsRemote {
39 #[clap(short, long)]
41 sort: bool,
42 #[clap(short, long)]
44 reverse: bool,
45 #[clap(short, long)]
47 update: bool,
48 },
49 Uninstall {
51 version: String,
52 },
53 Use {
55 version: String,
56 #[clap(short, long)]
57 update: bool,
58 },
59 Init,
69 Current,
70 Config,
72 Update,
74 Completions {
87 #[clap(short, long, default_value = "bash")]
88 shell: clap_complete::Shell,
89 },
90}
91
92impl Subcommand {
93 pub fn run(&self, ctx: &mut FgmContext) -> Result<()> {
94 match self {
95 Subcommand::Install { version, update } => {
96 ctx.update = *update;
97 install(ctx, version)?;
98 }
99 Subcommand::LsLocal { sort, reverse } => {
100 list_installed(ctx, *sort, *reverse);
101 }
102 Subcommand::LsRemote {
103 sort,
104 reverse,
105 update,
106 } => {
107 ctx.update = *update;
108 list_remote(ctx, *sort, *reverse)?;
109 }
110 Subcommand::Uninstall { version } => {
111 uninstall(ctx, version)?;
112 }
113 Subcommand::Use { version, update } => {
114 ctx.update = *update;
115 _use(ctx, version)?;
116 }
117 Subcommand::Init => println!("{}", init_script(ctx)),
118 Subcommand::Current => println!(
119 "{}",
120 current_version(ctx).unwrap_or("not version selected".to_owned())
121 ),
122 Subcommand::Config => {
123 println!("## Config Paths\n");
124 println!("config_path: {:?}", count_config_path().ok());
125 println!("remotes_index: {:?}", count_remotes_index_path().ok());
126
127 println!("\n## Configurations\n");
128 println!("installations_dir: {}", ctx.installations_dir);
129 println!("gate_path: {}", ctx.gate_path);
130 println!("remote_source: {}", ctx.remote_source);
131 }
132 Subcommand::Update => {
133 update(ctx)?;
134 }
135 Subcommand::Completions { shell } => gen_completions(*shell)?,
136 }
137 Ok(())
138 }
139}