Skip to content

Commit

Permalink
Merge pull request #182 from kohbis/rename_configuration_file
Browse files Browse the repository at this point in the history
feat: 🎸 Configuration `pwd`/.token -> $HOME/.rslack
  • Loading branch information
kohbis authored Apr 16, 2023
2 parents 1863510 + c363d4c commit 13eae8d
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 30 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
.token
target

1 change: 0 additions & 1 deletion .token.keep

This file was deleted.

56 changes: 54 additions & 2 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ categories = ["command-line-utilities"]

[dependencies]
anyhow = "1.0.70"
dirs = "5.0"
libc = "0.2"
reqwest = { version = "0.11.16", features = ["json"] }
serde = { version = "1.0.160", features = ["derive"] }
Expand Down
18 changes: 7 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,24 @@
# Install
brew install kohbis/rslack/rslack
# Set token
# Configuration
# If both are set, use the value of `.rslack
export RSLACK_TOKEN=your-token
# or
echo "RSLACK_TOKEN=your-token" > ${HOME}/.rslack
rslack
```

### Local Build

If you want by local build.

```bash
export RSLACK_TOKEN=your-token
# or
mv .token.keep .token
echo "RSLACK_TOKEN=your-token" > .token
# If both are set, use the value of `.token`
# Configuration
# and
cargo run
```

If you want to run on Docker, exec the following command.
If you want to run on Docker, please execute the following command.

```bash
docker build -t rslack .
Expand Down
3 changes: 1 addition & 2 deletions src/bin/rslack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use rslack::option::Opt;
use rslack::slack;

const SLACK_URL: &'static str = "https://slack.com";
const TOKEN_FILE: &'static str = ".token";
const USAGE_MESSAGES: &'static str = "(post: ctrl-p / exit: ctrl-c)";

#[tokio::main]
Expand All @@ -20,7 +19,7 @@ async fn main() {
let mut channel = opts.channel;
let mut message = opts.message;

let config = match Config::new(TOKEN_FILE) {
let config = match Config::new(None) {
Ok(config) => config,
Err(err) => return eprintln!("{}", err),
};
Expand Down
40 changes: 28 additions & 12 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,32 @@
use anyhow::{anyhow, Result};
use dirs;
use std::env;
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::path::Path;
use std::path::{Path, PathBuf};

const RSLACK_TOKEN: &str = "RSLACK_TOKEN";
const RSLACK_TOKEN: &'static str = "RSLACK_TOKEN";
const CONFIG_FILE: &'static str = ".rslack";

#[derive(Debug, PartialEq)]
pub struct Config {
pub token: String,
}

impl Config {
pub fn new(filename: &str) -> Result<Self> {
pub fn new(config_path: Option<PathBuf>) -> Result<Self> {
let mut config = Self {
token: String::new(),
};

config.read_from_env()?;

if Path::new(filename).exists() {
config.read_from_file(filename)?;
if let Some(path) = config_path {
config.read_from_file(&path).ok();
} else {
let home = dirs::home_dir().ok_or(anyhow!("Home directory not found."))?;
let path = home.join(CONFIG_FILE);
config.read_from_file(&path).ok();
}

config.validate()?;
Expand All @@ -39,8 +45,8 @@ impl Config {
}

#[allow(clippy::single_match)]
fn read_from_file(&mut self, filename: &str) -> Result<&Self> {
let file = match File::open(filename) {
fn read_from_file(&mut self, path: &Path) -> Result<&Self> {
let file = match File::open(path) {
Ok(file) => file,
Err(err) => return Err(anyhow!(err)),
};
Expand Down Expand Up @@ -85,7 +91,9 @@ mod tests {
#[serial]
fn initialize_with_valid_file() {
setup();
let actual = Config::new("tests/fixtures/config/token.test.valid").unwrap();

let config_path = PathBuf::from("tests/fixtures/config/config.test.valid");
let actual = Config::new(Some(config_path)).unwrap();
let expected = Config {
token: String::from("token-from-file-123"),
};
Expand All @@ -97,15 +105,19 @@ mod tests {
#[should_panic]
fn initialize_with_invalid_file() {
setup();
Config::new("tests/token.test.invalid").unwrap();

let config_path = PathBuf::from("tests/config.test.invalid");
Config::new(Some(config_path)).unwrap();
}

#[test]
#[serial]
fn initialize_with_env() {
setup();
env::set_var(RSLACK_TOKEN, "token-from-env-123");
let actual = Config::new("no_file").unwrap();

let config_path = PathBuf::from("no_file");
let actual = Config::new(Some(config_path)).unwrap();
let expected = Config {
token: String::from("token-from-env-123"),
};
Expand All @@ -118,18 +130,22 @@ mod tests {
fn initialize_with_empty_env() {
setup();
env::set_var(RSLACK_TOKEN, "");
Config::new("no_file").unwrap();

let config_path = PathBuf::from("no_file");
Config::new(Some(config_path)).unwrap();
}

#[test]
#[serial]
fn initialize_with_env_and_file() {
setup();
env::set_var(RSLACK_TOKEN, "token-from-env-123");

let expected = Config {
token: String::from("token-from-file-123"),
};
let actual = Config::new("tests/fixtures/config/token.test.valid").unwrap();
let config_path = PathBuf::from("tests/fixtures/config/config.test.valid");
let actual = Config::new(Some(config_path)).unwrap();
assert_eq!(actual, expected);
}

Expand Down
File renamed without changes.
File renamed without changes.

0 comments on commit 13eae8d

Please sign in to comment.