]> git.r.bdr.sh - rbdr/lyricli/blob - src/main.rs
Update dependencies
[rbdr/lyricli] / src / main.rs
1 mod configuration;
2 mod lyrics_engine;
3 mod sources;
4
5 use clap::Parser;
6 use std::io::{Error, ErrorKind::Other, Result};
7
8 use configuration::Configuration;
9 use lyrics_engine::print_lyrics;
10 use sources::{disable, enable, get_track, list, reset};
11
12 #[derive(Parser, Debug)]
13 #[command(version, about, long_about = None)]
14 struct Arguments {
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,
43 pub artist: String,
44 }
45
46 #[tokio::main]
47 async fn main() -> Result<()> {
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<()> {
64 let mut configuration = Configuration::new();
65 let arguments = Arguments::parse();
66
67 if arguments.list_sources {
68 let sources = list();
69 for source in sources {
70 print!("{}", source);
71 if configuration.is_enabled(&source) {
72 print!(" (enabled)");
73 }
74 println!();
75 }
76 return Ok(());
77 }
78
79 if let Some(source_name) = arguments.enable_source {
80 if !configuration.is_enabled(&source_name) {
81 enable(&source_name)?;
82 }
83 return configuration.enable_source(&source_name);
84 }
85
86 if let Some(source_name) = arguments.disable_source {
87 if configuration.is_enabled(&source_name) {
88 disable(&source_name)?;
89 }
90 return configuration.disable_source(&source_name);
91 }
92
93 if let Some(source_name) = arguments.reset_source {
94 return reset(&source_name);
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()),
101 artist,
102 };
103 } else {
104 current_track =
105 get_track().ok_or_else(|| Error::new(Other, "No Artist/Song could be found :("))?
106 }
107
108 print_lyrics(current_track, arguments.show_title).await
109 }