]>
Commit | Line | Data |
---|---|---|
44e7b4de | 1 | mod configuration; |
738ec06d RBR |
2 | mod lyrics_engine; |
3 | mod sources; | |
4 | ||
44e7b4de | 5 | use clap::Parser; |
4064639d | 6 | use std::io::{Error, ErrorKind::Other, Result}; |
738ec06d | 7 | |
44e7b4de | 8 | use configuration::Configuration; |
738ec06d | 9 | use lyrics_engine::print_lyrics; |
4064639d | 10 | use sources::{disable, enable, get_track, list, reset}; |
738ec06d RBR |
11 | |
12 | #[derive(Parser, Debug)] | |
13 | #[command(version, about, long_about = None)] | |
14 | struct Arguments { | |
738ec06d RBR |
15 | // Positional Arguments |
16 | /// Specify the artist. | |
17 | artist: Option<String>, | |
18 | ||
19 | /// Specify the artist. | |
20 | track_name: Option<String>, | |
21 | ||
22 | /// Show title of track if present | |
23 | #[arg(short = 't', long)] | |
24 | show_title: bool, | |
25 | ||
26 | /// Lists all sources | |
27 | #[arg(short, long)] | |
28 | list_sources: bool, | |
29 | ||
30 | /// Enables a source | |
31 | #[arg(short, long, value_name = "SOURCE")] | |
32 | enable_source: Option<String>, | |
33 | ||
34 | #[arg(short, long, value_name = "SOURCE")] | |
35 | disable_source: Option<String>, | |
36 | ||
37 | #[arg(short, long, value_name = "SOURCE")] | |
38 | reset_source: Option<String>, | |
39 | } | |
40 | ||
41 | pub struct Track { | |
42 | pub name: String, | |
4064639d | 43 | pub artist: String, |
738ec06d RBR |
44 | } |
45 | ||
46 | #[tokio::main] | |
47 | async fn main() -> Result<()> { | |
6b935f82 RBR |
48 | let result = run().await; |
49 | ||
50 | if cfg!(debug_assertions) { | |
51 | result | |
52 | } else { | |
53 | match result { | |
54 | Ok(_) => Ok(()), | |
55 | Err(e) => { | |
56 | eprintln!("Error: {}", e); | |
57 | std::process::exit(1); | |
58 | } | |
59 | } | |
60 | } | |
61 | } | |
62 | ||
63 | async fn run() -> Result<()> { | |
44e7b4de | 64 | let mut configuration = Configuration::new(); |
738ec06d RBR |
65 | let arguments = Arguments::parse(); |
66 | ||
67 | if arguments.list_sources { | |
44e7b4de RBR |
68 | let sources = list(); |
69 | for source in sources { | |
70 | print!("{}", source); | |
71 | if configuration.is_enabled(&source) { | |
72 | print!(" (enabled)"); | |
73 | } | |
5aaf8666 | 74 | println!(); |
44e7b4de RBR |
75 | } |
76 | return Ok(()); | |
738ec06d RBR |
77 | } |
78 | ||
79 | if let Some(source_name) = arguments.enable_source { | |
44e7b4de RBR |
80 | if !configuration.is_enabled(&source_name) { |
81 | enable(&source_name)?; | |
82 | } | |
83 | return configuration.enable_source(&source_name); | |
738ec06d RBR |
84 | } |
85 | ||
86 | if let Some(source_name) = arguments.disable_source { | |
44e7b4de RBR |
87 | if configuration.is_enabled(&source_name) { |
88 | disable(&source_name)?; | |
89 | } | |
90 | return configuration.disable_source(&source_name); | |
738ec06d RBR |
91 | } |
92 | ||
93 | if let Some(source_name) = arguments.reset_source { | |
44e7b4de | 94 | return reset(&source_name); |
738ec06d RBR |
95 | } |
96 | ||
97 | let current_track: Track; | |
98 | if let Some(artist) = arguments.artist { | |
99 | current_track = Track { | |
100 | name: arguments.track_name.unwrap_or("".to_string()), | |
39ff8a11 | 101 | artist, |
738ec06d RBR |
102 | }; |
103 | } else { | |
4064639d RBR |
104 | current_track = |
105 | get_track().ok_or_else(|| Error::new(Other, "No Artist/Song could be found :("))? | |
738ec06d RBR |
106 | } |
107 | ||
108 | print_lyrics(current_track, arguments.show_title).await | |
109 | } |