Browse Source

Have config files be spawned by the binaries if necessary.

Closes: #42
parazyd 4 years ago
parent
commit
a431fab6d7

+ 1 - 9
Makefile

@@ -13,7 +13,7 @@ BINS = drk darkfid gatewayd
 BINDEPS = \
 	Cargo.toml \
 	$(shell find bin/*/src -type f) \
-	$(shell find bin -type f -name Cargo.toml) \
+	$(shell find bin -type f -name *.toml) \
 	$(shell find src -type f) \
 	$(shell find sql -type f) \
 	$(shell find contrib/token -type f)
@@ -42,20 +42,12 @@ clean:
 
 install: all
 	mkdir -p $(DESTDIR)$(PREFIX)/bin
-	mkdir -p $(DESTDIR)$(PREFIX)/share/darkfi
-	mkdir -p $(DESTDIR)$(PREFIX)/share/doc/darkfi
 	cp -f $(BINS) $(DESTDIR)$(PREFIX)/bin
-	for i in $(BINS); \
-	do \
-		cp -f example/config/$$i.toml $(DESTDIR)$(PREFIX)/share/doc/darkfi; \
-	done;
 
 uninstall:
 	for i in $(BINS); \
 	do \
 		rm -f $(DESTDIR)$(PREFIX)/bin/$$i; \
 	done;
-	rm -rf $(DESTDIR)$(PREFIX)/share/doc/darkfi
-	rm -rf $(DESTDIR)$(PREFIX)/share/darkfi
 
 .PHONY: all check fix clippy test clean install uninstall

+ 7 - 13
README.md

@@ -53,21 +53,15 @@ introductory advice in the
 
 ## Install
 
-This will install the binaries and configurations in the configured
-namespace (`/usr/local` by default). The configurations are installed
-as TOML files in `/usr/local/share/doc/darkfi`. They have to be copied
-in your user's `HOME/.config/darkfi` directory. You can review the
-installed config files, but the defaults should be good for using
-the testnet if you're following this document.
+This will install the binaries on your system (`/usr/local` by
+default). The configuration files for the binaries are bundled with the
+binaries and contain sane defaults. You'll have to run each daemon once
+in order for them to spawn a config file, which you can then review.
 
 ```shell
 % sudo make install
-% mkdir -p ~/.config/darkfi
-% cp -f /usr/local/share/doc/darkfi/*.toml ~/.config/darkfi
 ```
 
-N.B. On OSX this is `/Users/x/Library/Application Support/darkfi`.
-
 ## Bash Completion
 This will add the options auto completion of `drk` and `darkfid`.
 ```shell
@@ -76,9 +70,9 @@ This will add the options auto completion of `drk` and `darkfid`.
 
 ## Usage
 
-After the installation, you should have `drk` and `darkfid`
-binaries in `/usr/local`. Also, the configuration files should be in
-`~/.config/darkfi`. Now we're ready to use the testnet.
+After the installation, you should have `drk` and `darkfid` binaries in
+`/usr/local`. Trying to run them once should place the configuration
+files in their respective path. Now we're ready to use the testnet.
 
 In one terminal, start `darkfid`, which is the daemon that will
 communicate with the DarkFi network:

+ 0 - 0
example/config/cashierd.toml → bin/cashierd/cashierd_config.toml


+ 6 - 1
bin/cashierd/src/main.rs

@@ -12,7 +12,7 @@ use simplelog::{ColorChoice, LevelFilter, TermLogger, TerminalMode};
 
 use darkfi::{
     blockchain::{rocks::columns, Rocks, RocksColumn},
-    cli::{CashierdConfig, CliCashierd, Config},
+    cli::{cli_config::spawn_config, CashierdConfig, CliCashierd, Config},
     crypto::{
         address::Address,
         keypair::{PublicKey, SecretKey},
@@ -36,6 +36,8 @@ use darkfi::{
 
 use cashierd::service::{bridge, bridge::Bridge};
 
+const CONFIG_FILE_CONTENTS: &[u8] = include_bytes!("../cashierd_config.toml");
+
 fn handle_bridge_error(error_code: u32) -> Result<()> {
     match error_code {
         1 => Err(Error::CashierError("Not Supported Client".into())),
@@ -659,6 +661,9 @@ async fn main() -> Result<()> {
         join_config_path(&PathBuf::from("cashierd.toml"))?
     };
 
+    // Spawn config file if it's not in place already.
+    spawn_config(&config_path, CONFIG_FILE_CONTENTS)?;
+
     let mut verbosity_level = 0;
     verbosity_level += matches.occurrences_of("verbose");
     let loglevel = match verbosity_level {

+ 0 - 0
example/config/darkfid.toml → bin/darkfid/darkfid_config.toml


+ 6 - 1
bin/darkfid/src/main.rs

@@ -13,7 +13,7 @@ use url::Url;
 
 use darkfi::{
     blockchain::{rocks::columns, Rocks, RocksColumn},
-    cli::{CliDarkfid, Config, DarkfidConfig},
+    cli::{cli_config::spawn_config, CliDarkfid, Config, DarkfidConfig},
     crypto::{
         address::Address,
         keypair::{Keypair, PublicKey, SecretKey},
@@ -38,6 +38,8 @@ use darkfi::{
     Error, Result,
 };
 
+const CONFIG_FILE_CONTENTS: &[u8] = include_bytes!("../darkfid_config.toml");
+
 pub const ETH_NATIVE_TOKEN_ID: &str = "0x0000000000000000000000000000000000000000";
 
 #[derive(Clone, Debug)]
@@ -812,6 +814,9 @@ async fn main() -> Result<()> {
         join_config_path(&PathBuf::from("darkfid.toml"))?
     };
 
+    // Spawn config file if it's not in place already.
+    spawn_config(&config_path, CONFIG_FILE_CONTENTS)?;
+
     let mut verbosity_level = 0;
     verbosity_level += matches.occurrences_of("verbose");
     let loglevel = match verbosity_level {

+ 0 - 0
example/config/drk.toml → bin/drk/drk_config.toml


+ 9 - 6
bin/drk/src/main.rs

@@ -1,20 +1,20 @@
 use std::{path::PathBuf, str::FromStr};
 
-#[macro_use]
-extern crate prettytable;
 use clap::{IntoApp, Parser};
-use log::debug;
-use prettytable::{format, Table};
+use log::{debug, error};
+use prettytable::{cell, format, row, Table};
 use serde_json::{json, Value};
 use simplelog::{ColorChoice, LevelFilter, TermLogger, TerminalMode};
 
 use darkfi::{
-    cli::{CliDrk, CliDrkSubCommands, Config, DrkConfig},
+    cli::{cli_config::spawn_config, CliDrk, CliDrkSubCommands, Config, DrkConfig},
     rpc::{jsonrpc, jsonrpc::JsonResult},
     util::{join_config_path, path::expand_path, NetworkName},
     Error, Result,
 };
 
+const CONFIG_FILE_CONTENTS: &[u8] = include_bytes!("../drk_config.toml");
+
 struct Drk {
     url: String,
 }
@@ -343,7 +343,7 @@ async fn start(config: &DrkConfig, options: CliDrk) -> Result<()> {
         None => {}
     }
 
-    println!("Please run 'drk help' to see usage.");
+    error!("Please run 'drk help' to see usage.");
     Err(Error::MissingParams)
 }
 
@@ -358,6 +358,9 @@ async fn main() -> Result<()> {
         join_config_path(&PathBuf::from("drk.toml"))?
     };
 
+    // Spawn config file if it's not in place already.
+    spawn_config(&config_path, CONFIG_FILE_CONTENTS)?;
+
     let mut verbosity_level = 0;
     verbosity_level += matches.occurrences_of("verbose");
     let loglevel = match verbosity_level {

+ 0 - 0
example/config/gatewayd.toml → bin/gatewayd/gatewayd_config.toml


+ 6 - 1
bin/gatewayd/src/main.rs

@@ -8,12 +8,14 @@ use simplelog::{ColorChoice, LevelFilter, TermLogger, TerminalMode};
 
 use darkfi::{
     blockchain::{rocks::columns, Rocks, RocksColumn},
-    cli::{CliGatewayd, Config, GatewaydConfig},
+    cli::{cli_config::spawn_config, CliGatewayd, Config, GatewaydConfig},
     node::service::gateway::GatewayService,
     util::{expand_path, join_config_path},
     Result,
 };
 
+const CONFIG_FILE_CONTENTS: &[u8] = include_bytes!("../gatewayd_config.toml");
+
 async fn start(executor: Arc<Executor<'_>>, config: &GatewaydConfig) -> Result<()> {
     let rocks = Rocks::new(&expand_path(&config.database_path)?)?;
     let rocks_slabstore_column = RocksColumn::<columns::Slabs>::new(rocks);
@@ -38,6 +40,9 @@ async fn main() -> Result<()> {
         join_config_path(&PathBuf::from("gatewayd.toml"))?
     };
 
+    // Spawn config file if it's not in place already.
+    spawn_config(&config_path, CONFIG_FILE_CONTENTS)?;
+
     let mut verbosity_level = 0;
     verbosity_level += matches.occurrences_of("verbose");
     let loglevel = match verbosity_level {

+ 16 - 0
src/cli/cli_config.rs

@@ -1,5 +1,6 @@
 use std::{
     fs,
+    io::Write,
     marker::PhantomData,
     net::SocketAddr,
     path::{Path, PathBuf},
@@ -137,3 +138,18 @@ pub struct CashierdConfig {
     /// The configured networks to use
     pub networks: Vec<FeatureNetwork>,
 }
+
+pub fn spawn_config(path: &Path, contents: &[u8]) -> Result<()> {
+    if !path.exists() {
+        if let Some(parent) = path.parent() {
+            fs::create_dir_all(parent)?;
+        }
+
+        let mut file = fs::File::create(path.clone())?;
+        file.write_all(contents)?;
+        println!("Config file created in `{:?}`. Please review it and try again.", path);
+        std::process::exit(2);
+    }
+
+    Ok(())
+}