Skip to content

Commit c33293d

Browse files
authored
Add completion spec: SDKMAN! (sdk) (#220)
1 parent bd5848b commit c33293d

3 files changed

Lines changed: 329 additions & 0 deletions

File tree

command-signatures/json/sdk.json

Lines changed: 231 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,231 @@
1+
{
2+
"name": "sdk",
3+
"description": "SDKMAN! - The Software Development Kit Manager",
4+
"subcommands": [
5+
{
6+
"name": [
7+
"install",
8+
"i"
9+
],
10+
"description": "Install a candidate version",
11+
"args": [
12+
{
13+
"name": "candidate",
14+
"generatorName": "candidates"
15+
},
16+
{
17+
"name": "version",
18+
"isOptional": true,
19+
"generatorName": "available_versions"
20+
},
21+
{
22+
"name": "path",
23+
"isOptional": true,
24+
"template": "folders"
25+
}
26+
]
27+
},
28+
{
29+
"name": [
30+
"uninstall",
31+
"rm"
32+
],
33+
"description": "Uninstall a candidate version",
34+
"args": [
35+
{
36+
"name": "candidate",
37+
"generatorName": "candidates"
38+
},
39+
{
40+
"name": "version",
41+
"generatorName": "installed_versions"
42+
}
43+
]
44+
},
45+
{
46+
"name": [
47+
"list",
48+
"ls"
49+
],
50+
"description": "List available candidates or candidate versions",
51+
"args": {
52+
"name": "candidate",
53+
"isOptional": true,
54+
"generatorName": "candidates"
55+
}
56+
},
57+
{
58+
"name": [
59+
"use",
60+
"u"
61+
],
62+
"description": "Use a specific version in the current shell",
63+
"args": [
64+
{
65+
"name": "candidate",
66+
"generatorName": "candidates"
67+
},
68+
{
69+
"name": "version",
70+
"generatorName": "installed_versions"
71+
}
72+
]
73+
},
74+
{
75+
"name": [
76+
"default",
77+
"d"
78+
],
79+
"description": "Set the default candidate version for all shells",
80+
"args": [
81+
{
82+
"name": "candidate",
83+
"generatorName": "candidates"
84+
},
85+
{
86+
"name": "version",
87+
"isOptional": true,
88+
"generatorName": "installed_versions"
89+
}
90+
]
91+
},
92+
{
93+
"name": "home",
94+
"description": "Output the path of a specific candidate version",
95+
"args": [
96+
{
97+
"name": "candidate",
98+
"generatorName": "candidates"
99+
},
100+
{
101+
"name": "version",
102+
"generatorName": "installed_versions"
103+
}
104+
]
105+
},
106+
{
107+
"name": [
108+
"current",
109+
"c"
110+
],
111+
"description": "Display the current default installed versions",
112+
"args": {
113+
"name": "candidate",
114+
"isOptional": true,
115+
"generatorName": "candidates"
116+
}
117+
},
118+
{
119+
"name": [
120+
"upgrade",
121+
"ug"
122+
],
123+
"description": "Upgrade installed candidate versions",
124+
"args": {
125+
"name": "candidate",
126+
"isOptional": true,
127+
"generatorName": "candidates"
128+
}
129+
},
130+
{
131+
"name": "env",
132+
"description": "Manage project-specific SDK versions via .sdkmanrc",
133+
"args": {
134+
"name": "command",
135+
"isOptional": true,
136+
"suggestions": [
137+
{
138+
"name": "init",
139+
"description": "Create a default .sdkmanrc file"
140+
},
141+
{
142+
"name": "install",
143+
"description": "Install and switch to versions in .sdkmanrc"
144+
},
145+
{
146+
"name": "clear",
147+
"description": "Reset all SDK versions to their defaults"
148+
}
149+
]
150+
}
151+
},
152+
{
153+
"name": [
154+
"version",
155+
"v"
156+
],
157+
"description": "Display the current version of SDKMAN!"
158+
},
159+
{
160+
"name": "selfupdate",
161+
"description": "Update SDKMAN! itself",
162+
"args": {
163+
"name": "qualifier",
164+
"isOptional": true,
165+
"suggestions": [
166+
{
167+
"name": "force",
168+
"description": "Force a reinstallation of SDKMAN!"
169+
}
170+
]
171+
}
172+
},
173+
{
174+
"name": "update",
175+
"description": "Refresh the candidate metadata cache"
176+
},
177+
{
178+
"name": "flush",
179+
"description": "Flush SDKMAN! local state",
180+
"args": {
181+
"name": "target",
182+
"isOptional": true,
183+
"suggestions": [
184+
{
185+
"name": "tmp",
186+
"description": "Clean pre/post hooks and residual archives"
187+
},
188+
{
189+
"name": "metadata",
190+
"description": "Remove header metadata"
191+
},
192+
{
193+
"name": "version",
194+
"description": "Flush version files"
195+
}
196+
]
197+
}
198+
},
199+
{
200+
"name": "config",
201+
"description": "Open the SDKMAN! configuration file"
202+
},
203+
{
204+
"name": [
205+
"help",
206+
"h"
207+
],
208+
"description": "Display help information",
209+
"args": {
210+
"name": "subcommand",
211+
"isOptional": true,
212+
"suggestions": [
213+
"install",
214+
"uninstall",
215+
"list",
216+
"use",
217+
"default",
218+
"home",
219+
"env",
220+
"current",
221+
"upgrade",
222+
"version",
223+
"selfupdate",
224+
"update",
225+
"flush",
226+
"config"
227+
]
228+
}
229+
}
230+
]
231+
}

command-signatures/src/generators/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ mod powershell;
4343
mod pyenv;
4444
mod react_native;
4545
mod screen;
46+
mod sdk;
4647
mod ssh;
4748
mod systemctl;
4849
mod tar;
@@ -102,6 +103,7 @@ pub fn dynamic_command_signature_data() -> HashMap<String, DynamicCompletionData
102103
tmux::generator(),
103104
node::generator(),
104105
screen::generator(),
106+
sdk::generator(),
105107
powershell::get_help_generator(),
106108
powershell::get_process_generator(),
107109
powershell::debug_process_generator(),
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
use warp_completion_metadata::{
2+
CommandBuilder, CommandSignatureGenerators, Generator, GeneratorResultsCollector, Suggestion,
3+
};
4+
5+
/// Returns the SDKMAN directory reference using the SDKMAN_DIR env var with a fallback.
6+
fn sdkman_dir() -> &'static str {
7+
"${SDKMAN_DIR:-$HOME/.sdkman}"
8+
}
9+
10+
/// Extracts the candidate name from tokens.
11+
///
12+
/// When `has_trailing_whitespace` is true, the candidate is the last token
13+
/// (the user just finished typing the candidate and pressed space).
14+
/// Otherwise, the candidate is the second-to-last token (the user is typing
15+
/// the version prefix).
16+
fn candidate_from_tokens<'a>(tokens: &'a [&str], has_trailing_whitespace: bool) -> Option<&'a str> {
17+
if has_trailing_whitespace {
18+
tokens.last().copied()
19+
} else {
20+
tokens.get(tokens.len().saturating_sub(2)).copied()
21+
}
22+
}
23+
24+
pub fn generator() -> CommandSignatureGenerators {
25+
CommandSignatureGenerators::new("sdk")
26+
.add_generator(
27+
"candidates",
28+
Generator::script(
29+
CommandBuilder::single_command(format!(
30+
"cat {}/var/candidates | tr ',' '\\n'",
31+
sdkman_dir()
32+
)),
33+
|output| {
34+
output
35+
.trim()
36+
.lines()
37+
.filter(|line| !line.is_empty())
38+
.map(|line| Suggestion::with_description(line.trim(), "SDK candidate"))
39+
.collect_unordered_results()
40+
},
41+
),
42+
)
43+
.add_generator(
44+
"installed_versions",
45+
Generator::command_from_tokens(
46+
|tokens, has_trailing_whitespace, _| {
47+
match candidate_from_tokens(tokens, has_trailing_whitespace) {
48+
Some(candidate) => CommandBuilder::pipe(
49+
CommandBuilder::single_command(format!(
50+
"ls {}/candidates/{}/",
51+
sdkman_dir(),
52+
candidate
53+
)),
54+
CommandBuilder::single_command("grep -v current"),
55+
),
56+
None => CommandBuilder::single_command(""),
57+
}
58+
},
59+
|output| {
60+
output
61+
.trim()
62+
.lines()
63+
.filter(|line| !line.is_empty())
64+
.map(|line| {
65+
Suggestion::with_description(line.trim(), "Installed version")
66+
})
67+
.collect_unordered_results()
68+
},
69+
),
70+
)
71+
.add_generator(
72+
"available_versions",
73+
Generator::command_from_tokens(
74+
|tokens, has_trailing_whitespace, _| {
75+
match candidate_from_tokens(tokens, has_trailing_whitespace) {
76+
Some(candidate) => CommandBuilder::single_command(format!(
77+
"curl -s https://api.sdkman.io/2/candidates/{}/$(cat {}/var/platform)/versions/all | tr ',' '\\n'",
78+
candidate,
79+
sdkman_dir()
80+
)),
81+
None => CommandBuilder::single_command(""),
82+
}
83+
},
84+
|output| {
85+
output
86+
.trim()
87+
.lines()
88+
.filter(|line| !line.is_empty())
89+
.map(|line| {
90+
Suggestion::with_description(line.trim(), "Available version")
91+
})
92+
.collect_unordered_results()
93+
},
94+
),
95+
)
96+
}

0 commit comments

Comments
 (0)