forked from gpu-mode/popcorn-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod.rs
More file actions
274 lines (246 loc) · 8.57 KB
/
mod.rs
File metadata and controls
274 lines (246 loc) · 8.57 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
use anyhow::{anyhow, Result};
use clap::{Parser, Subcommand};
use serde::{Deserialize, Serialize};
use std::fs::File;
use std::path::PathBuf;
mod admin;
mod auth;
mod setup;
mod submissions;
mod submit;
pub use admin::AdminAction;
#[derive(Serialize, Deserialize, Debug, Default)]
struct Config {
cli_id: Option<String>,
}
fn get_config_path() -> Result<PathBuf> {
dirs::home_dir()
.map(|mut path| {
path.push(".popcorn.yaml");
path
})
.ok_or_else(|| anyhow!("Could not find home directory"))
}
fn load_config() -> Result<Config> {
let path = get_config_path()?;
if !path.exists() {
return Err(anyhow!(
"Config file not found at {}. Please run `popcorn register` first.",
path.display()
));
}
let file = File::open(path)?;
serde_yaml::from_reader(file).map_err(|e| anyhow!("Failed to parse config file: {}", e))
}
#[derive(Parser, Debug)]
#[command(author, version = env!("CLI_VERSION"), about, long_about = None)]
pub struct Cli {
#[command(subcommand)]
command: Option<Commands>,
/// Optional: Path to the solution file
filepath: Option<String>,
/// Optional: Directly specify the GPU to use (e.g., "mi300")
#[arg(long)]
pub gpu: Option<String>,
/// Optional: Directly specify the leaderboard (e.g., "fp8")
#[arg(long)]
pub leaderboard: Option<String>,
/// Optional: Specify submission mode (test, benchmark, leaderboard, profile)
#[arg(long)]
pub mode: Option<String>,
// Optional: Specify output file
#[arg(short, long)]
pub output: Option<String>,
/// Skip the TUI and print results directly to stdout
#[arg(long)]
pub no_tui: bool,
}
#[derive(Subcommand, Debug)]
enum AuthProvider {
Discord,
Github,
}
#[derive(Subcommand, Debug)]
enum SubmissionsAction {
/// List your submissions for a leaderboard
List {
/// Leaderboard name (required)
#[arg(long)]
leaderboard: String,
/// Maximum number of submissions to show
#[arg(long, default_value = "50")]
limit: i32,
},
/// Show a specific submission with full details and code
Show {
/// Submission ID
id: i64,
},
/// Delete a submission
Delete {
/// Submission ID
id: i64,
/// Skip confirmation prompt
#[arg(long)]
force: bool,
},
}
#[derive(Subcommand, Debug)]
enum Commands {
/// Bootstrap this project with Popcorn agent skills and a submission template
Setup {
/// Overwrite files if they already exist
#[arg(long)]
force: bool,
},
Reregister {
#[command(subcommand)]
provider: AuthProvider,
},
Register {
#[command(subcommand)]
provider: AuthProvider,
},
Submit {
/// Optional: Path to the solution file (can also be provided as a top-level argument)
filepath: Option<String>,
/// Optional: Directly specify the GPU to use (e.g., "MI300")
#[arg(long)]
gpu: Option<String>,
/// Optional: Directly specify the leaderboard (e.g., "amd-fp8-mm")
#[arg(long)]
leaderboard: Option<String>,
/// Optional: Specify submission mode (test, benchmark, leaderboard, profile)
#[arg(long)]
mode: Option<String>,
// Optional: Specify output file
#[arg(short, long)]
output: Option<String>,
/// Skip the TUI and print results directly to stdout
#[arg(long)]
no_tui: bool,
},
/// Admin commands (requires POPCORN_ADMIN_TOKEN env var)
Admin {
#[command(subcommand)]
action: AdminAction,
},
/// Manage your submissions
Submissions {
#[command(subcommand)]
action: SubmissionsAction,
},
}
pub async fn execute(cli: Cli) -> Result<()> {
match cli.command {
Some(Commands::Setup { force }) => setup::run_setup(force),
Some(Commands::Reregister { provider }) => {
let provider_str = match provider {
AuthProvider::Discord => "discord",
AuthProvider::Github => "github",
};
auth::run_auth(true, provider_str).await
}
Some(Commands::Register { provider }) => {
let provider_str = match provider {
AuthProvider::Discord => "discord",
AuthProvider::Github => "github",
};
auth::run_auth(false, provider_str).await
}
Some(Commands::Submit {
filepath,
gpu,
leaderboard,
mode,
output,
no_tui,
}) => {
let config = load_config()?;
let cli_id = config.cli_id.ok_or_else(|| {
anyhow!(
"cli_id not found in config file ({}). Please run 'popcorn-cli register' first.",
get_config_path()
.map_or_else(|_| "unknown path".to_string(), |p| p.display().to_string())
)
})?;
// Use filepath from Submit command first, fallback to top-level filepath
let final_filepath = filepath.or(cli.filepath);
if no_tui {
submit::run_submit_plain(
final_filepath, // Resolved filepath
gpu, // From Submit command
leaderboard, // From Submit command
mode, // From Submit command
cli_id,
output, // From Submit command
)
.await
} else {
submit::run_submit_tui(
final_filepath, // Resolved filepath
gpu, // From Submit command
leaderboard, // From Submit command
mode, // From Submit command
cli_id,
output, // From Submit command
)
.await
}
}
Some(Commands::Admin { action }) => admin::handle_admin(action).await,
Some(Commands::Submissions { action }) => {
let config = load_config()?;
let cli_id = config.cli_id.ok_or_else(|| {
anyhow!(
"cli_id not found in config file ({}). Please run `popcorn register` first.",
get_config_path()
.map_or_else(|_| "unknown path".to_string(), |p| p.display().to_string())
)
})?;
match action {
SubmissionsAction::List { leaderboard, limit } => {
submissions::list_submissions(cli_id, leaderboard, Some(limit)).await
}
SubmissionsAction::Show { id } => submissions::show_submission(cli_id, id).await,
SubmissionsAction::Delete { id, force } => {
submissions::delete_submission(cli_id, id, force).await
}
}
}
None => {
// Check if any of the submission-related flags were used at the top level
if cli.gpu.is_some() || cli.leaderboard.is_some() || cli.mode.is_some() {
return Err(anyhow!(
"Please use the 'submit' subcommand when specifying submission options:\n\
popcorn-cli submit [--gpu GPU] [--leaderboard LEADERBOARD] [--mode MODE] FILEPATH"
));
}
// Handle the case where only a filepath is provided (for backward compatibility)
if let Some(top_level_filepath) = cli.filepath {
let config = load_config()?;
let cli_id = config.cli_id.ok_or_else(|| {
anyhow!(
"cli_id not found in config file ({}). Please run `popcorn register` first.",
get_config_path()
.map_or_else(|_| "unknown path".to_string(), |p| p.display().to_string())
)
})?;
// Run TUI with only filepath, no other options
submit::run_submit_tui(
Some(top_level_filepath),
None, // No GPU option
None, // No leaderboard option
None, // No mode option
cli_id,
None, // No output option
)
.await
} else {
Err(anyhow!(
"No command or submission file specified. Use --help for usage."
))
}
}
}
}