fgm/
cli.rs

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 a specific version of Go
21    Install {
22        version: String,
23        #[clap(short, long)]
24        update: bool,
25    },
26    /// List installed versions
27    #[clap(name = "list",visible_aliases = &["ls"])]
28    LsLocal {
29        // sort
30        #[clap(short, long)]
31        sort: bool,
32        // reverse
33        #[clap(short, long)]
34        reverse: bool,
35    },
36    /// List all remote versions
37    #[clap(name = "list-remote",visible_aliases = &["ls-remote"])]
38    LsRemote {
39        // sort
40        #[clap(short, long)]
41        sort: bool,
42        // reverse
43        #[clap(short, long)]
44        reverse: bool,
45        // update cache
46        #[clap(short, long)]
47        update: bool,
48    },
49    /// Uninstall a specific version of Go
50    Uninstall {
51        version: String,
52    },
53    /// Use a specific version of Go
54    Use {
55        version: String,
56        #[clap(short, long)]
57        update: bool,
58    },
59    /// Print and set up required environment variables for fgm
60    ///
61    /// This command generates a series of shell commands that
62    /// should be evaluated by your shell to create a fgm-ready environment.
63    ///
64    /// Each shell has its own syntax of evaluating a dynamic expression.
65    /// For example, evaluating fgm on Bash and Zsh would look like `eval "$(fgm init)"`.
66    ///
67    /// Now, only Bash and Zsh are supported.It may also work on other shells that support the `export` command.
68    Init,
69    Current,
70    /// show the runtime configuration
71    Config,
72    /// Update the fgm remotes index
73    Update,
74    /// Generate shell completions, which can be evaluated to enable shell completions for fgm
75    ///
76    /// The `shell` argument specifies the shell for which completions should be generated.
77    ///
78    /// Supported shells are:
79    /// zsh, bash, fish, powershell, and elvish
80    ///
81    /// For example, to enable completions for bash, add to your shell profile:
82    /// ```sh
83    /// eval "$(fgm completions  --shell <YOUR_SHELL>)"
84    /// ```
85    /// where `<YOUR_SHELL>` is the shell you are using.
86    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}