Skip to content

Commit

Permalink
Environment variable support for tokens. (#240)
Browse files Browse the repository at this point in the history
  • Loading branch information
Xithrius authored Oct 22, 2022
1 parent 392850e commit 0d62233
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 23 deletions.
10 changes: 10 additions & 0 deletions book/src/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,13 @@
The ideal setup would be this application and [streamlink](https://github.com/streamlink/streamlink), to obliterate all need for a browser while watching streams.

This book is still under construction, so some sections might still be missing. Feel free to [submit an issue](https://github.com/Xithrius/twitch-tui/issues/new/choose) if you find that something needs improvement.

## Quick start

This application has vim and emacs inspired keybinds. That means you will start out in visual mode.

To enter insert mode, press `i`. To drop back to visual mode, press `esc`.

Insert modes at this time are message search (`Ctrl + f`), channel swapper (`s`), and the previously mentioned insert mode (`i`).

To see all the options with keybinds, either head over to the [keybinds section](https://xithrius.github.io/twitch-tui/keybinds/index.html) of this book, or press `?` while in visual mode.
6 changes: 6 additions & 0 deletions book/src/guide/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Configuration and installation

The how-to of getting this application up and running.

- [Installation](installation.md)
- [Configuration](configuration.md)
23 changes: 23 additions & 0 deletions book/src/guide/configuration.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Configuration

## Config file

After running `twt` for the first time, a config file will be generated at the following locations, depending on your OS:

- Linux/MacOS: `~/.config/twt/config.toml`
- Windows: `%appdata%\twt\config.toml`

You can find the default configuration values [here](https://github.com/Xithrius/twitch-tui/blob/main/default-config.toml).

## Auth

Get an OAuth token from [Twitch](https://twitchapps.com/tmi/), and put the value in one of two places:

1. The `token` variable in the `config.toml` that was previously generated.
2. The environment variable `TWT_TOKEN`.

The environment variable will be used first, even if a token exists in `config.toml`. If one doesn't exist there, your config token will be used.

## Run it

Run `twt` in the terminal. For help, `twt --help`.
19 changes: 0 additions & 19 deletions book/src/guide/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,3 @@ Installing a version such as `2.0.0-alpha.1` would be `cargo install twitch-tui
(PPT and AUR repos coming soon)

To uninstall, run the command `cargo uninstall twitch-tui`.

## Configuration

### Config file

After running `twt` for the first time, a config file will be generated at the following locations, depending on your OS:

- Linux/MacOS: `~/.config/twt/config.toml`
- Windows: `%appdata%\twt\config.toml`

You can find the default configuration values [here](https://github.com/Xithrius/twitch-tui/blob/main/default-config.toml).

### Auth

Get an OAuth token from [Twitch](https://twitchapps.com/tmi/), and place the value in the `token` variable in the `config.toml` that was previously generated.

### Run it

Run `twt` in the terminal. For help, `twt --help`.
17 changes: 14 additions & 3 deletions src/handlers/config.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![allow(clippy::use_self)]

use std::{
env,
fs::{create_dir_all, read_to_string, File},
io::Write,
path::Path,
Expand Down Expand Up @@ -40,7 +41,7 @@ pub struct TwitchConfig {
/// The IRC channel to connect to.
pub server: String,
/// The authentication token for the IRC.
pub token: String,
pub token: Option<String>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
Expand Down Expand Up @@ -107,7 +108,7 @@ impl Default for TwitchConfig {
username: "".to_string(),
channel: "".to_string(),
server: "irc.chat.twitch.tv".to_string(),
token: "".to_string(),
token: None,
}
}
}
Expand Down Expand Up @@ -239,10 +240,20 @@ impl CompleteConfig {

merge_args_into_config(&mut config, cli);

let token: Option<&'static str> = option_env!("TWT_TOKEN");

if let Some(env_token) = token {
if !env_token.is_empty() {
config.twitch.token = Some(env_token.to_string());
}
}

{
let t = &config.twitch;

if t.username.is_empty() || t.channel.is_empty() || t.token.is_empty() {
let check_token = t.token.as_ref().map_or("", |t| t);

if t.username.is_empty() || t.channel.is_empty() || check_token.is_empty() {
bail!("Twitch config section is missing one or more of the following: username, channel, token.");
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/twitch/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub async fn create_client_stream(config: CompleteConfig) -> (Client, ClientStre
nickname: Some(config.twitch.username.clone()),
server: Some(config.twitch.server.clone()),
channels: vec![format!("#{}", config.twitch.channel)],
password: Some(config.twitch.token.clone()),
password: config.twitch.token.clone(),
port: Some(6667),
use_tls: Some(false),
ping_timeout: Some(10),
Expand Down

0 comments on commit 0d62233

Please sign in to comment.