Skip to content

Commit 513f496

Browse files
authored
Refactor into a library for in quick-flash-gui (#27)
* build: Update Cargo.toml to define library and binary targets * creds refactor wip * creds manager wip * main refactor wip * mostly working binary version, bump to 0.3.0 * run clippy [no ci]
1 parent 59910c5 commit 513f496

File tree

9 files changed

+435
-151
lines changed

9 files changed

+435
-151
lines changed

Cargo.lock

Lines changed: 133 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
[package]
22
name = "quick-flash"
3-
version = "0.2.3"
3+
version = "0.3.0"
44
edition = "2021"
55
repository = "https://github.com/manakjiri/quick-flash"
66
description = "Flash centrally hosted firmware binaries with one command"
77
license = "MIT"
88

9+
[lib]
10+
name = "quick_flash"
11+
path = "src/lib.rs"
12+
13+
[[bin]]
14+
name = "quick-flash"
15+
path = "src/main.rs"
16+
917
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1018

1119
[dependencies]
@@ -18,6 +26,7 @@ serde = { version = "1.0.213", features = ["derive"] }
1826
serde_json = "1.0.132"
1927
toml = "0.8.19"
2028
openssl = { version = "0.10", features = ["vendored"] }
29+
chrono = "0.4.38"
2130

2231
# The profile that 'cargo dist' will build with
2332
[profile.dist]

src/config.rs

Lines changed: 0 additions & 76 deletions
This file was deleted.

src/credentials.rs

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
use crate::utils;
2+
use anyhow::{self, Context};
3+
use chrono::Utc;
4+
use serde::{Deserialize, Serialize};
5+
use std::fs;
6+
use std::path::Path;
7+
8+
#[derive(Serialize, Deserialize)]
9+
pub enum StorageType {
10+
R2,
11+
}
12+
13+
#[derive(Serialize, Deserialize)]
14+
pub struct Credentials {
15+
pub user_storage_name: String,
16+
pub storage_type: StorageType,
17+
pub storage_name: String,
18+
pub storage_account_id: String,
19+
pub storage_access_key: String,
20+
pub storage_secret_key: String,
21+
pub timestamp: i64,
22+
}
23+
24+
impl Credentials {
25+
pub fn new_r2(
26+
user_storage_name: String,
27+
storage_name: String,
28+
storage_account_id: String,
29+
storage_access_key: String,
30+
storage_secret_key: String,
31+
) -> Self {
32+
Self {
33+
user_storage_name,
34+
storage_type: StorageType::R2,
35+
storage_name,
36+
storage_account_id,
37+
storage_access_key,
38+
storage_secret_key,
39+
timestamp: Utc::now().timestamp(),
40+
}
41+
}
42+
43+
pub fn read_from_path(path: &Path) -> anyhow::Result<Self> {
44+
let contents = fs::read_to_string(path).context(format!(
45+
"Failed to read credentials file {}",
46+
path.display()
47+
))?;
48+
let credentials: Credentials = toml::from_str(&contents).context(format!(
49+
"Failed to parse credentials file {}",
50+
path.display()
51+
))?;
52+
Ok(credentials)
53+
}
54+
55+
pub fn write_to_path(&self, path: &Path) -> anyhow::Result<()> {
56+
let contents = toml::to_string(self)?;
57+
fs::write(path, contents)?;
58+
Ok(())
59+
}
60+
}
61+
62+
pub fn get_credentials_from_command_line() -> anyhow::Result<Credentials> {
63+
eprintln!("Input credentials for the R2 bucket below:");
64+
65+
eprint!("Bucket Name: ");
66+
let storage_name = utils::read_line()?;
67+
eprint!("Bucket Account ID: ");
68+
let storage_account_id = utils::read_line()?;
69+
eprint!("Bucket Access Key: ");
70+
let storage_access_key = utils::read_line()?;
71+
eprint!("Bucket Secret Key: ");
72+
let storage_secret_key = utils::read_line()?;
73+
eprint!(
74+
"Optionally, name the storage for future reference [{}]: ",
75+
&storage_name
76+
);
77+
let user_storage_name = utils::read_line().unwrap_or(storage_name.clone());
78+
79+
let creds = Credentials::new_r2(
80+
user_storage_name,
81+
storage_name,
82+
storage_account_id,
83+
storage_access_key,
84+
storage_secret_key,
85+
);
86+
/* eprintln!("Saving credentials to {}...", path.display());
87+
write_credentials(&path, &creds)?; */
88+
Ok(creds)
89+
}

0 commit comments

Comments
 (0)