Skip to content

Commit

Permalink
test: write file
Browse files Browse the repository at this point in the history
  • Loading branch information
thesimplekid committed May 9, 2023
1 parent 1ab6db9 commit 5d14d97
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 25 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/target
/result
/test
/test
test.txt
33 changes: 16 additions & 17 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,32 @@
mod utils;
use std::ops::Add;
use std::path::PathBuf;
use std::str::FromStr;
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};

use anyhow::{bail, Result};
use log::{info, warn};

use cln_plugin::options::{ConfigOption, Value};
use cln_plugin::Plugin;
use cln_rpc::model::{requests::PayRequest, Request, Response};
use cln_rpc::primitives::{Amount, Secret};

use dirs::config_dir;
use futures::{Stream, StreamExt};
use lightning_invoice::{Invoice, InvoiceDescription};
use nostr_sdk::prelude::encrypt;

use std::ops::Add;
use std::path::PathBuf;
use std::str::FromStr;
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};

use tokio::io::{stdin, stdout};

use nostr_sdk::nips::nip04::decrypt;
use log::{info, warn};
use nostr_sdk::secp256k1::{SecretKey, XOnlyPublicKey};
use nostr_sdk::{nips::nip47, ClientMessage, EventBuilder, Filter, Kind, SubscriptionId};
use nostr_sdk::{
nips::{
nip04::{decrypt, encrypt},
nip47,
},
ClientMessage, EventBuilder, Filter, Kind, SubscriptionId,
};
use nostr_sdk::{RelayMessage, Tag, Url};

use tokio::io::{stdin, stdout};
use tungstenite::{connect, Message as WsMessage};

mod utils;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
info!("Starting cln-nostr-connect");
Expand Down
34 changes: 27 additions & 7 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,27 +47,27 @@ pub fn write_to_config(key: &str, value: &str, config_path: &PathBuf) -> std::io

// Create the file if it doesn't exist
if !config_path.exists() {
File::create(&config_path)?;
File::create(config_path)?;
}

let file = File::open(config_path)?;
let reader = BufReader::new(file);

let mut lines = reader.lines();
let lines = reader.lines();

// Create a new vector to hold the updated lines
let mut updated_lines = Vec::new();

let mut key_found = false;

// Loop through each line in the config file
while let Some(line) = lines.next() {
for line in lines {
let line = line?;

// Check if the line contains the key we're looking for
if line.starts_with(&format!("{}=", key)) {
// Update the value for the key
updated_lines.push(format!("{}={}\n", key, value));
updated_lines.push(format!("{}={}", key, value));
key_found = true;
} else {
// Add the original line to the updated_lines vector
Expand All @@ -77,7 +77,7 @@ pub fn write_to_config(key: &str, value: &str, config_path: &PathBuf) -> std::io

// If the key was not found in the config file, add it to the end
if !key_found {
updated_lines.push(format!("{}={}\n", key, value));
updated_lines.push(format!("{}={}", key, value));
}

// Write the updated lines to the config file
Expand All @@ -94,10 +94,10 @@ pub fn read_from_config(key: &str, config_path: &PathBuf) -> Result<Option<Strin
let file = File::open(config_path)?;
let reader = BufReader::new(file);

let mut lines = reader.lines();
let lines = reader.lines();

// Loop through each line in the config file
while let Some(line) = lines.next() {
for line in lines {
let line = line?;

// Check if the line contains the key we're looking for
Expand All @@ -111,3 +111,23 @@ pub fn read_from_config(key: &str, config_path: &PathBuf) -> Result<Option<Strin

Ok(None)
}

#[cfg(test)]
mod tests {
use super::*;

const TEST: &str = "./test.txt";

#[test]
fn test_write_to_config() {
let path = PathBuf::from(TEST);
write_to_config("ONE", "Hello", &path).unwrap();
write_to_config("TWO", "World", &path).unwrap();

let one = read_from_config("ONE", &path).unwrap();
assert_eq!(one.unwrap(), "Hello");

let two = read_from_config("TWO", &path).unwrap();
assert_eq!(two.unwrap(), "World");
}
}

0 comments on commit 5d14d97

Please sign in to comment.