Skip to content
This repository has been archived by the owner on Aug 3, 2023. It is now read-only.

Commit

Permalink
feat(config): split global and project config, add wrangler.toml
Browse files Browse the repository at this point in the history
  • Loading branch information
ashleygwilliams committed Apr 8, 2019
1 parent a74e26b commit a537492
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 24 deletions.
4 changes: 2 additions & 2 deletions src/commands/config.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::fs;
use std::path::Path;

use crate::user::settings::Settings;
use crate::user::settings::GlobalSettings;

pub fn global_config(email: &str, api_key: &str) -> Result<(), failure::Error> {
let s = Settings {
let s = GlobalSettings {
email: email.to_string(),
api_key: api_key.to_string(),
};
Expand Down
8 changes: 4 additions & 4 deletions src/commands/publish/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ fn single_script(zone_id: &str, user: &User) -> Result<(), failure::Error> {

client
.put(&worker_addr)
.header("X-Auth-Key", settings.api_key)
.header("X-Auth-Email", settings.email)
.header("X-Auth-Key", settings.global.api_key)
.header("X-Auth-Email", settings.global.email)
.multipart(build_form()?)
.send()?;

Expand All @@ -56,8 +56,8 @@ fn multi_script(zone_id: &str, user: &User, name: &str) -> Result<(), failure::E

client
.put(&worker_addr)
.header("X-Auth-Key", settings.api_key)
.header("X-Auth-Email", settings.email)
.header("X-Auth-Key", settings.global.api_key)
.header("X-Auth-Email", settings.global.email)
.multipart(build_form()?)
.send()?;

Expand Down
8 changes: 4 additions & 4 deletions src/user/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ fn account_data(settings: Settings) -> Result<AccountData, failure::Error> {

let mut res = client
.get(user_addr)
.header("X-Auth-Key", settings.api_key)
.header("X-Auth-Email", settings.email)
.header("X-Auth-Key", settings.global.api_key)
.header("X-Auth-Email", settings.global.email)
.header(CONTENT_TYPE, "application/json")
.send()?;

Expand All @@ -69,8 +69,8 @@ fn script_status(settings: Settings, account_data: &AccountData) -> Result<bool,

let mut res = client
.get(&addr)
.header("X-Auth-Key", settings.api_key)
.header("X-Auth-Email", settings.email)
.header("X-Auth-Key", settings.global.api_key)
.header("X-Auth-Email", settings.global.email)
.header(CONTENT_TYPE, "application/json")
.send()?;

Expand Down
67 changes: 53 additions & 14 deletions src/user/settings.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,66 @@
use std::collections::HashMap;
use std::path::Path;

use config::{Config, Environment, File};
use serde::{Deserialize, Serialize};

#[derive(Debug, Deserialize, Clone, Serialize)]
pub struct Settings {
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct GlobalSettings {
pub email: String,
pub api_key: String,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct ProjectSettings {
pub zone_id: String,
pub account_id: String,
pub route: Option<String>,
pub routes: Option<HashMap<String, String>>,
}

#[derive(Clone, Serialize)]
pub struct Settings {
pub global: GlobalSettings,
pub project: ProjectSettings,
}

impl Settings {
pub fn new() -> Result<Self, failure::Error> {
let mut s = Config::new();
let global = get_global_config()?;
let project = get_project_config()?;

let config_path = dirs::home_dir()
.expect("oops no home dir")
.join(".wrangler/config/default");
let config_str = config_path
.to_str()
.expect("config path should be a string");
s.merge(File::with_name(config_str))?;
Ok(Settings { global, project })
}
}

// Eg.. `CF_ACCOUNT_AUTH_KEY=farts` would set the `account_auth_key` key
s.merge(Environment::with_prefix("CF"))?;
fn get_global_config() -> Result<GlobalSettings, failure::Error> {
let mut s = Config::new();

Ok(s.try_into()?)
}
let config_path = dirs::home_dir()
.expect("oops no home dir")
.join(".wrangler/config/default");
let config_str = config_path
.to_str()
.expect("global config path should be a string");
s.merge(File::with_name(config_str))?;

// Eg.. `CF_ACCOUNT_AUTH_KEY=farts` would set the `account_auth_key` key
s.merge(Environment::with_prefix("CF"))?;

Ok(s.try_into()?)
}

fn get_project_config() -> Result<ProjectSettings, failure::Error> {
let mut s = Config::new();

let config_path = Path::new("./wrangler.toml");
let config_str = config_path
.to_str()
.expect("project config path should be a string");
s.merge(File::with_name(config_str))?;

// Eg.. `CF_ACCOUNT_AUTH_KEY=farts` would set the `account_auth_key` key
s.merge(Environment::with_prefix("CF"))?;

Ok(s.try_into()?)
}

0 comments on commit a537492

Please sign in to comment.