Skip to content

Commit 078d9c1

Browse files
Add play command
1 parent 723dee2 commit 078d9c1

File tree

2 files changed

+62
-19
lines changed

2 files changed

+62
-19
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main.rs

Lines changed: 61 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ fn cli() -> Command {
5050
Command::new("logout")
5151
.about("Logout of your account")
5252
.long_about(
53-
"Logs out of your Put.io account by removing the auth token saved on your device.",
54-
),
53+
"Logs out of your Put.io account by removing the auth token saved on your device.",
54+
),
5555
)
5656
.subcommand(
5757
Command::new("files")
@@ -99,25 +99,25 @@ fn cli() -> Command {
9999
.arg_required_else_help(true)
100100
.arg(
101101
Arg::new("parent_id")
102-
.short('p')
103-
.long("parent")
104-
.help("ID of a Put folder to upload to instead of the root folder")
105-
.required(false)
102+
.short('p')
103+
.long("parent")
104+
.help("ID of a Put folder to upload to instead of the root folder")
105+
.required(false)
106106
)
107107
.arg(
108108
Arg::new("file_name")
109-
.short('n')
110-
.long("name")
111-
.help("Override file name")
112-
.required(false)
109+
.short('n')
110+
.long("name")
111+
.help("Override file name")
112+
.required(false)
113113
)
114114
.arg(
115115
Arg::new("is_silent")
116-
.short('s')
117-
.long("silent")
118-
.help("Run CURL in silent mode")
119-
.required(false)
120-
.num_args(0)
116+
.short('s')
117+
.long("silent")
118+
.help("Run CURL in silent mode")
119+
.required(false)
120+
.num_args(0)
121121
)
122122
.arg(
123123
arg!(<PATH> ... "Valid paths of files to upload")
@@ -198,7 +198,17 @@ fn cli() -> Command {
198198
"Clears all finshed transfers on your account. Does not remove files.",
199199
),
200200
)
201-
,
201+
,
202+
)
203+
.subcommand(
204+
Command::new("play")
205+
.about("Stream a video file")
206+
.long_about(
207+
"Plays a video file using MPV.\n\
208+
If you do not have MPV installed, visit https://mpv.io/installation/.",
209+
)
210+
.arg_required_else_help(true)
211+
.arg(arg!(<FILE_ID> "ID of a file (required)")),
202212
)
203213
.subcommand(
204214
Command::new("whoami")
@@ -271,6 +281,33 @@ fn main() {
271281
confy::store(APP_NAME, None, cfg).expect("updating config file");
272282
println!("Signed out successfully!")
273283
}
284+
Some(("play", sub_matches)) => {
285+
require_auth(&config);
286+
287+
let file_id = sub_matches
288+
.get_one::<String>("FILE_ID")
289+
.expect("missing file ID argument")
290+
.parse::<u32>()
291+
.expect("parsing file_id");
292+
293+
let file_info =
294+
put::files::list(config.api_token.clone(), file_id).expect("fetching file info");
295+
296+
if file_info.parent.file_type != "VIDEO" {
297+
println!("File type must be video.");
298+
return;
299+
}
300+
301+
let download_url = put::files::url(config.api_token, file_id).expect("generating url");
302+
303+
ProcessCommand::new("mpv")
304+
.arg(download_url.url)
305+
.stdout(Stdio::piped())
306+
.spawn()
307+
.expect("error while spawning mpv (is it installed?)")
308+
.wait()
309+
.expect("error while running MPV");
310+
}
274311
Some(("whoami", _sub_matches)) => {
275312
let account = require_auth(&config);
276313
println!(
@@ -295,9 +332,12 @@ fn main() {
295332
None => 0,
296333
};
297334
let files = put::files::list(config.api_token, folder_id).expect("fetching files");
335+
298336
if files.parent.file_type != "FOLDER" {
299-
panic!("the ID provided should be for a folder and not a file")
337+
println!("The ID provided should be for a folder and not a file");
338+
return;
300339
}
340+
301341
let table = Table::new(&files.files).with(Style::markdown()).to_string();
302342
println!("\n# {}\n", &files.parent.name);
303343
println!("{}\n", table);
@@ -331,9 +371,12 @@ fn main() {
331371
let query = sub_matches
332372
.get_one::<String>("QUERY")
333373
.expect("missing query argument");
374+
334375
let files = put::files::search(config.api_token, query.to_string())
335376
.expect("querying files");
377+
336378
let table = Table::new(files.files).with(Style::markdown()).to_string();
379+
337380
println!("\n# Results for `{}`\n", &query);
338381
println!("{}\n", table);
339382
}
@@ -362,7 +405,7 @@ fn main() {
362405
.arg(url_response.url)
363406
.stdout(Stdio::piped())
364407
.spawn()
365-
.expect("running CURL command")
408+
.expect("error while spawning curl")
366409
.wait_with_output()
367410
.expect("running CURL command");
368411

0 commit comments

Comments
 (0)