]> git.seodisparate.com - mpd_info_screen/commitdiff
Backport: Impl passing password via file
authorStephen Seo <seo.disparate@gmail.com>
Thu, 2 Mar 2023 12:33:01 +0000 (21:33 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Thu, 2 Mar 2023 12:33:01 +0000 (21:33 +0900)
src/main.rs

index 51f02016221ffd1e74460fa636abd7f015f5d8bc..74031950b32c9d87f74f816b86166f9fb7c6efd2 100644 (file)
@@ -10,6 +10,8 @@ use ggez::event::{self, ControlFlow, EventHandler};
 use ggez::filesystem::mount;
 use ggez::graphics::{self, Rect};
 use ggez::{ContextBuilder, GameError};
+use std::fs::File;
+use std::io::Read;
 use std::net::Ipv4Addr;
 use std::path::PathBuf;
 use std::thread;
@@ -36,6 +38,8 @@ pub struct Opt {
     disable_show_filename: bool,
     #[structopt(long = "pprompt", help = "input password via prompt")]
     enable_prompt_password: bool,
+    #[structopt(long = "pfile", help = "read password from file")]
+    password_file: Option<PathBuf>,
     #[structopt(
         long = "no-scale-fill",
         help = "don't scale-fill the album art to the window"
@@ -59,9 +63,26 @@ pub struct Opt {
 }
 
 fn main() -> Result<(), String> {
-    let opt = Opt::from_args();
+    let mut opt = Opt::from_args();
     println!("Got host addr == {}, port == {}", opt.host, opt.port);
 
+    // Read password from file if exists, error otherwise.
+    if let Some(psswd_file_path) = opt.password_file.as_ref() {
+        let mut file = File::open(psswd_file_path).expect("pfile/password_file should exist");
+        let mut content: String = String::new();
+
+        file.read_to_string(&mut content)
+            .expect("Should be able to read from pfile/password_file");
+
+        if content.ends_with("\r\n") {
+            content.truncate(content.len() - 2);
+        } else if content.ends_with('\n') {
+            content.truncate(content.len() - 1);
+        }
+
+        opt.password = Some(content);
+    }
+
     let (mut ctx, event_loop) = ContextBuilder::new("mpd_info_screen", "Stephen Seo")
         .window_setup(WindowSetup {
             title: "mpd info screen".into(),