Skip to content

Commit

Permalink
add hostname resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
figsoda committed Feb 7, 2021
1 parent 9d52ad9 commit de34cc3
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 29 deletions.
7 changes: 2 additions & 5 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,14 @@ use serde::{
};
use tui::style::Color;

use std::{
fmt::{self, Formatter},
net::SocketAddr,
};
use std::fmt::{self, Formatter};

use crate::defaults;

#[derive(Deserialize)]
pub struct Config {
#[serde(default = "defaults::address")]
pub address: SocketAddr,
pub address: String,
#[serde(default)]
pub clear_query_on_play: bool,
#[serde(default)]
Expand Down
6 changes: 2 additions & 4 deletions src/defaults.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use tui::style::Color;

use std::net::{IpAddr, Ipv4Addr, SocketAddr};

use crate::config::{
AddStyle, Column, Condition, Config, Constrained, SearchFields, Texts, Widget,
};
Expand All @@ -19,8 +17,8 @@ pub fn config() -> Config {
}
}

pub fn address() -> SocketAddr {
SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 6600)
pub fn address() -> String {
String::from("127.0.0.1:6600")
}

pub fn jump_lines() -> usize {
Expand Down
1 change: 0 additions & 1 deletion src/fail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@ macro_rules! fail {
};
}

fail!(connect addr = "Failed to connect to {}");
fail!(parse_cfg path = "Failed to parse configuration file {}");
fail!(read path = "Failed to read file {}");
21 changes: 12 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,11 @@ use crossterm::{
};
use dirs_next::config_dir;
use futures_lite::StreamExt;
use tui::{backend::CrosstermBackend, widgets::ListState, Terminal};

use std::{
cmp::min,
env, fs,
io::stdout,
net::SocketAddr,
process::exit,
sync::{
atomic::{AtomicU8, Ordering},
Expand All @@ -37,6 +35,7 @@ use std::{
thread::{self, Thread},
time::Duration,
};
use tui::{backend::CrosstermBackend, widgets::ListState, Terminal};

use crate::{
app::{Command, Opts, State},
Expand Down Expand Up @@ -85,17 +84,21 @@ async fn run() -> Result<()> {
defaults::config()
};

let addr = &if let Some(addr) = opts.address {
addr
let (mut idle_cl, mut cl) = if let Some(addr) = opts.address {
let idle_cl = Client::init(addr).await?;
let cl = Client::init(addr).await?;
(idle_cl, cl)
} else if let (Ok(host), Ok(port)) = (env::var("MPD_HOST"), env::var("MPD_PORT")) {
SocketAddr::new(host.parse()?, port.parse()?)
let addr = &*async_net::resolve((host, port.parse()?)).await?;
let idle_cl = Client::init(addr).await?;
let cl = Client::init(addr).await?;
(idle_cl, cl)
} else {
cfg.address
let idle_cl = Client::init(&cfg.address).await?;
let cl = Client::init(&cfg.address).await?;
(idle_cl, cl)
};

let mut idle_cl = Client::init(addr).await?;
let mut cl = Client::init(addr).await?;

let status = cl.status().await?;
let (queue, mut queue_strings) = idle_cl.queue(status.queue_len, &cfg.search_fields).await?;
let mut s = State {
Expand Down
14 changes: 4 additions & 10 deletions src/mpd.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
use anyhow::{bail, Context, Result};
use async_net::TcpStream;
use async_net::{AsyncToSocketAddrs, TcpStream};
use expand::expand;
use futures_lite::{
io::{split, AsyncBufReadExt, AsyncReadExt, AsyncWriteExt, BufReader, ReadHalf, WriteHalf},
StreamExt,
};

use std::net::SocketAddr;

use crate::{config::SearchFields, fail};
use crate::config::SearchFields;

pub struct Client {
r: BufReader<ReadHalf<TcpStream>>,
Expand Down Expand Up @@ -80,13 +78,9 @@ fn track_string(track: &Track, search_fields: &SearchFields) -> String {
}

impl Client {
pub async fn init(addr: &SocketAddr) -> Result<Client> {
pub async fn init(addr: impl AsyncToSocketAddrs) -> Result<Client> {
async move {
let (r, w) = split(
TcpStream::connect(addr)
.await
.with_context(fail::connect(addr))?,
);
let (r, w) = split(TcpStream::connect(addr).await?);
let mut cl = Client {
r: BufReader::new(r),
w,
Expand Down

0 comments on commit de34cc3

Please sign in to comment.