From dccf70017ae9b5f859fe1b3b14f8718d33cff838 Mon Sep 17 00:00:00 2001 From: Cole Helbling Date: Tue, 17 Oct 2023 10:12:12 -0700 Subject: [PATCH 1/2] Replace lazy_static with once_cell's Lazy --- Cargo.lock | 1 - Cargo.toml | 1 - src/cli/cmd/init/prompt.rs | 15 ++++++++------- src/cli/cmd/mod.rs | 10 +++++----- 4 files changed, 13 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7a2f760d..f830de81 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -594,7 +594,6 @@ dependencies = [ "handlebars", "indicatif", "inquire", - "lazy_static", "nixel", "once_cell", "prettytable-rs", diff --git a/Cargo.toml b/Cargo.toml index d79a5ef8..281b3e04 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,7 +24,6 @@ color-eyre = { version = "0.6.2", default-features = false, features = [ handlebars = "4.4.0" indicatif = { version = "0.17.6", default-features = false } inquire = "0.6.2" -lazy_static = "1.4.0" nixel = "5.2.0" once_cell = "1.18.0" prettytable-rs = "0.10.0" diff --git a/src/cli/cmd/init/prompt.rs b/src/cli/cmd/init/prompt.rs index 236fe096..f56d48f1 100644 --- a/src/cli/cmd/init/prompt.rs +++ b/src/cli/cmd/init/prompt.rs @@ -4,17 +4,18 @@ use inquire::{ ui::{Color, RenderConfig, StyleSheet, Styled}, Confirm, MultiSelect, Select, Text, }; -use lazy_static::lazy_static; +use once_cell::sync::Lazy; -lazy_static! { - static ref MAGENTA_TEXT: StyleSheet = StyleSheet::default().with_fg(Color::DarkMagenta); - static ref GREY_TEXT: StyleSheet = StyleSheet::default().with_fg(Color::Grey); - static ref PROMPT_CONFIG: RenderConfig = RenderConfig::default() +static MAGENTA_TEXT: Lazy = + Lazy::new(|| StyleSheet::default().with_fg(Color::DarkMagenta)); +static GREY_TEXT: Lazy = Lazy::new(|| StyleSheet::default().with_fg(Color::Grey)); +static PROMPT_CONFIG: Lazy = Lazy::new(|| { + RenderConfig::default() .with_prompt_prefix(Styled::new(">").with_fg(Color::LightBlue)) .with_selected_option(Some(*MAGENTA_TEXT)) .with_answer(*GREY_TEXT) - .with_help_message(*GREY_TEXT); -} + .with_help_message(*GREY_TEXT) +}); pub(crate) struct Prompt; diff --git a/src/cli/cmd/mod.rs b/src/cli/cmd/mod.rs index a1df0001..74d40c9b 100644 --- a/src/cli/cmd/mod.rs +++ b/src/cli/cmd/mod.rs @@ -7,7 +7,7 @@ pub(crate) mod login; pub(crate) mod search; pub(crate) mod status; -use lazy_static::lazy_static; +use once_cell::sync::Lazy; use prettytable::format::{FormatBuilder, LinePosition, LineSeparator, TableFormat}; use reqwest::Client as HttpClient; use serde::Serialize; @@ -17,16 +17,16 @@ use self::{ search::SearchResult, }; -lazy_static! { - pub(crate) static ref TABLE_FORMAT: TableFormat = FormatBuilder::new() +pub(crate) static TABLE_FORMAT: Lazy = Lazy::new(|| { + FormatBuilder::new() .borders('|') .padding(1, 1) .separators( &[LinePosition::Top, LinePosition::Title, LinePosition::Bottom], LineSeparator::new('-', '+', '+', '+'), ) - .build(); -} + .build() +}); #[async_trait::async_trait] pub trait CommandExecute { From 69c4f15c53eb7abe08f489c0d73a8a9575e3cf3f Mon Sep 17 00:00:00 2001 From: Cole Helbling Date: Tue, 17 Oct 2023 10:30:12 -0700 Subject: [PATCH 2/2] fh add: use auth if it exists --- src/cli/cmd/add/mod.rs | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/cli/cmd/add/mod.rs b/src/cli/cmd/add/mod.rs index 55705470..c493c8bd 100644 --- a/src/cli/cmd/add/mod.rs +++ b/src/cli/cmd/add/mod.rs @@ -7,6 +7,7 @@ use std::process::ExitCode; use clap::Parser; use color_eyre::eyre::WrapErr; +use reqwest::header::{HeaderValue, ACCEPT, AUTHORIZATION}; use serde::Deserialize; use self::flake::InputsInsertionLocation; @@ -191,10 +192,22 @@ pub(crate) async fn get_flakehub_project_and_url( version: Option<&str>, ) -> color_eyre::Result<(String, url::Url)> { let mut headers = reqwest::header::HeaderMap::new(); - headers.insert( - "Accept", - reqwest::header::HeaderValue::from_static("application/json"), - ); + headers.insert(ACCEPT, HeaderValue::from_static("application/json")); + + let xdg = xdg::BaseDirectories::new()?; + // $XDG_CONFIG_HOME/fh/auth; basically ~/.config/fh/auth + let token_path = xdg.get_config_file("flakehub/auth"); + + if token_path.exists() { + let token = tokio::fs::read_to_string(&token_path) + .await + .wrap_err_with(|| format!("Could not open {}", token_path.display()))?; + + headers.insert( + AUTHORIZATION, + HeaderValue::from_str(&format!("Bearer {token}"))?, + ); + } let client = reqwest::Client::builder() .user_agent(crate::APP_USER_AGENT)