Skip to content

Commit

Permalink
Replaced editor, config file location with cross-compile options
Browse files Browse the repository at this point in the history
  • Loading branch information
vedangj044 authored and jbtrystram committed Apr 30, 2021
1 parent ae27cdf commit d49c532
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 17 deletions.
52 changes: 52 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,7 @@ qstring = "0.7.2"
url = { version = "2.2.1", features = ["serde"] }
chrono = { version = "0.4", features = ["serde"]}

webbrowser = "0.5.5"
webbrowser = "0.5.5"

edit = "0.1.3"
dirs = "3.0"
11 changes: 6 additions & 5 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ use anyhow::{anyhow, Context as AnyhowContext, Result};
use serde::{Deserialize, Serialize};
use std::{env, fs::create_dir_all, fs::write, fs::File, path::Path};

use crate::AppId;
use chrono::{DateTime, Utc};
use dirs::config_dir;
use oauth2::basic::BasicTokenResponse;
use read_input::prelude::*;
use url::Url;

use crate::AppId;

type ContextId = String;

#[derive(Serialize, Deserialize, Debug)]
Expand Down Expand Up @@ -194,13 +194,14 @@ impl Context {
// use the provided config path or `$DRGCFG` value if set
// otherwise will default to $XDG_CONFIG_HOME
// fall back to `$HOME/.config` if XDG var is not set.
// todo crossplatform support
fn eval_config_path(path: Option<&str>) -> String {
match path {
Some(p) => p.to_string(),
None => env::var("DRGCFG").unwrap_or_else(|_| {
let xdg = env::var("XDG_CONFIG_HOME")
.unwrap_or(format!("{}/.config", env::var("HOME").unwrap()));
let xdg = match config_dir() {
Some(path) => path.into_os_string().into_string().unwrap(),
None => String::from("."),
};
format!("{}/drg_config.yaml", xdg)
}),
}
Expand Down
19 changes: 8 additions & 11 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,8 @@ use reqwest::StatusCode;
use serde_json::{from_str, Value};
use std::fs;
use std::io::stdout;
use std::io::{Read, Write};
use std::process::exit;
use std::{
env::var,
io::{Read, Write},
process::Command,
};
use tempfile::Builder;
use url::Url;

Expand Down Expand Up @@ -79,8 +75,6 @@ pub fn json_parse(data: Option<&str>) -> Result<Value> {
pub fn editor(original: String) -> Result<Value> {
let data: Value = serde_json::from_str(original.as_str())?;

// todo cross platform support
let editor = var("EDITOR").unwrap_or_else(|_| "vi".to_string());
let file = Builder::new().suffix(".json").tempfile()?;
//the handler needs to be kept to reopen the file later.
let mut file2 = file.reopen()?;
Expand All @@ -89,10 +83,13 @@ pub fn editor(original: String) -> Result<Value> {
file.as_file()
.write_all(serde_json::to_string_pretty(&data)?.as_bytes())?;

Command::new(editor)
.arg(file.path())
.status()
.expect("Could not open current data in editor.");
edit::edit_file(file.path())
.map_err(|err| {
log::debug!("{}", err);
log::error!("Error opening a text editor, pelase try --file");
exit(1);
})
.unwrap();

// Read the data using the second handle.
let mut buf = String::new();
Expand Down

0 comments on commit d49c532

Please sign in to comment.