1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
use anyhow::Result;
use clap::Parser;
use crate::{
_use,
config::{count_config_path, count_remotes_index_path, FgmContext},
current_version, gen_completions, init_script, install, list_installed, list_remote, uninstall,
update,
};
#[derive(Parser, Debug)]
#[clap(name = "fgm", version = env!("CARGO_PKG_VERSION"), bin_name = "fgm",about,long_about = None)]
pub struct Cli {
#[clap(subcommand)]
pub sub: Subcommand,
}
#[derive(Parser, Debug, Clone)]
pub enum Subcommand {
/// Install a specific version of Go
Install {
version: String,
},
/// List installed versions
#[clap(name = "list",visible_aliases = &["ls"])]
LsLocal {
// sort
#[clap(short, long)]
sort: bool,
// reverse
#[clap(short, long)]
reverse: bool,
},
/// List all remote versions
#[clap(name = "list-remote",visible_aliases = &["ls-remote"])]
LsRemote {
// sort
#[clap(short, long)]
sort: bool,
// reverse
#[clap(short, long)]
reverse: bool,
},
/// Uninstall a specific version of Go
Uninstall {
version: String,
},
/// Use a specific version of Go
Use {
version: String,
},
/// Print and set up required environment variables for fgm
///
/// This command generates a series of shell commands that
/// should be evaluated by your shell to create a fgm-ready environment.
///
/// Each shell has its own syntax of evaluating a dynamic expression.
/// For example, evaluating fgm on Bash and Zsh would look like `eval "$(fgm init)"`.
///
/// Now, only Bash and Zsh are supported.It may also work on other shells that support the `export` command.
Init,
Current,
/// show the runtime configuration
Config,
/// Update the fgm remotes index
Update,
/// Generate shell completions, which can be evaluated to enable shell completions for fgm
///
/// The `shell` argument specifies the shell for which completions should be generated.
///
/// Supported shells are:
/// zsh, bash, fish, powershell, and elvish
///
/// For example, to enable completions for bash, add to your shell profile:
/// ```sh
/// eval "$(fgm completions --shell <YOUR_SHELL>)"
/// ```
/// where `<YOUR_SHELL>` is the shell you are using.
Completions {
#[clap(short, long, default_value = "bash")]
shell: clap_complete::Shell,
},
}
impl Subcommand {
pub fn run(&self, ctx: &FgmContext) -> Result<()> {
match self {
Subcommand::Install { version } => {
install(ctx, version)?;
}
Subcommand::LsLocal { sort, reverse } => {
list_installed(ctx, *sort, *reverse);
}
Subcommand::LsRemote { sort, reverse } => {
list_remote(ctx, *sort, *reverse)?;
}
Subcommand::Uninstall { version } => {
uninstall(ctx, version)?;
}
Subcommand::Use { version } => {
_use(ctx, version)?;
}
Subcommand::Init => println!("{}", init_script(ctx)),
Subcommand::Current => println!(
"{}",
current_version(ctx).unwrap_or("not version selected".to_owned())
),
Subcommand::Config => {
println!("## Config Paths\n");
println!("config_path: {:?}", count_config_path().ok());
println!("remotes_index: {:?}", count_remotes_index_path().ok());
println!("\n## Configurations\n");
println!("installations_dir: {}", ctx.installations_dir);
println!("gate_path: {}", ctx.gate_path);
println!("remote_source: {}", ctx.remote_source);
}
Subcommand::Update => {
update(ctx)?;
}
Subcommand::Completions { shell } => gen_completions(*shell)?,
}
Ok(())
}
}