Skip to content

Commit 50a116d

Browse files
committed
read environment variables
1 parent fd39e44 commit 50a116d

File tree

4 files changed

+26
-14
lines changed

4 files changed

+26
-14
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "porkbun-ddns"
3-
version = "0.1.0"
3+
version = "0.2.0"
44
edition = "2021"
55

66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

readme.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ address = ""
1616
ipv6 = false
1717
```
1818

19-
you need to fill in your api `keys` and your `domain` information (the subdomain can be empty for base domain).
19+
you can fill in your api `keys` or omit the header to read from the `PORKBUN_API_KEY` and `PORKBUN_SECRET_API_KEY` environment variables.
20+
21+
your `domain` information (the subdomain can be empty for base domain).
2022

2123
you can fill in an `address` optionally to skip the ping request that returns your ip.
2224

src/main.rs

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use serde::{Deserialize, Serialize};
22
use serde_json::Value;
3-
use std::fs;
43
use std::path::Path;
4+
use std::{env, fs};
55

66
use ureq::Agent;
77

@@ -24,6 +24,11 @@ fn main() {
2424

2525
let agent = Agent::new();
2626

27+
let keys = match config.keys {
28+
Some(x) => x,
29+
None => get_env().expect("Couldn't get environment variables"),
30+
};
31+
2732
let ip: String;
2833
match config.ip.address.is_empty() {
2934
true => {
@@ -33,7 +38,7 @@ fn main() {
3338
};
3439
let ping_response: Value = agent
3540
.post(&ping_endpoint)
36-
.send_json(&config.keys)
41+
.send_json(&keys)
3742
.unwrap()
3843
.into_json()
3944
.unwrap();
@@ -50,9 +55,7 @@ fn main() {
5055
}
5156

5257
let full_domain = match config.domain.subdomain.is_empty() {
53-
true => {
54-
config.domain.base.clone()
55-
}
58+
true => config.domain.base.clone(),
5659
false => {
5760
format!("{}.{}", config.domain.subdomain, &config.domain.base)
5861
}
@@ -71,7 +74,7 @@ fn main() {
7174
let records_endpoint = format!("{}/dns/retrieve/{}", ENDPOINT, &config.domain.base);
7275
let records_response: RecordsResponse = agent
7376
.post(&records_endpoint)
74-
.send_json(&config.keys)
77+
.send_json(&keys)
7578
.unwrap()
7679
.into_json()
7780
.unwrap();
@@ -97,7 +100,7 @@ fn main() {
97100
let delete_endpoint = format!("{}/dns/delete/{}/{}", ENDPOINT, &config.domain.base, x.id);
98101
let delete_response: Value = agent
99102
.post(&delete_endpoint)
100-
.send_json(&config.keys)
103+
.send_json(&keys)
101104
.unwrap()
102105
.into_json()
103106
.unwrap();
@@ -116,8 +119,8 @@ fn main() {
116119

117120
let create_endpoint = format!("{}/dns/create/{}", ENDPOINT, &config.domain.base);
118121
let create_body = CreateRecord {
119-
secretapikey: config.keys.secretapikey,
120-
apikey: config.keys.apikey,
122+
secretapikey: keys.secretapikey,
123+
apikey: keys.apikey,
121124
name: config.domain.subdomain,
122125
_type: String::from(record_type),
123126
content: String::from(&ip),
@@ -127,7 +130,7 @@ fn main() {
127130
};
128131
let create_response: Value = agent
129132
.post(&create_endpoint)
130-
.send_json(&create_body)
133+
.send_json(create_body)
131134
.unwrap()
132135
.into_json()
133136
.unwrap();
@@ -141,9 +144,16 @@ fn main() {
141144
}
142145
}
143146

147+
fn get_env() -> Result<Keys, env::VarError> {
148+
Ok(Keys {
149+
secretapikey: env::var("PORKBUN_SECRET_API_KEY")?,
150+
apikey: env::var("PORKBUN_API_KEY")?,
151+
})
152+
}
153+
144154
#[derive(Serialize, Deserialize, Default)]
145155
struct Config {
146-
keys: Keys,
156+
keys: Option<Keys>,
147157
domain: Domain,
148158
ip: Ip,
149159
}

0 commit comments

Comments
 (0)