Skip to content

Commit

Permalink
feat: support environment variable containing session
Browse files Browse the repository at this point in the history
fixes #8
  • Loading branch information
ModProg committed Dec 5, 2022
1 parent 5fcd679 commit 0e72af3
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,11 @@ obtain your session cookie, login to the
[Advent of Code](https://adventofcode.com) website and inspect the `session`
value of the cookie that gets stored in your browser. Put the session number (a
long hex string) either in a file called `.adventofcode.session` in your home
directory or `adventofcode.session` in your users config directory (`~/.config` on Linux, `~/Library/Application Support` on macOS or `~\AppData\Roaming` on Windows). This file should only contain your session number, in a single line.
directory or `adventofcode.session` in your users config directory (`~/.config`
on Linux, `~/Library/Application Support` on macOS or `~\AppData\Roaming` on
Windows). This file should only contain your session number, in a single line.
Alternatively you can export your session as the environment variable
`ADVENT_OF_CODE_SESSION`.

## Usage

Expand Down
4 changes: 4 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ mod args;
use aoc::*;
use args::*;
use clap::Parser;
use std::env;
use std::fs::read_to_string;
use std::path::PathBuf;
use std::process::exit;

const SESSION_COOKIE_FILE: &str = "adventofcode.session";
const HIDDEN_SESSION_COOKIE_FILE: &str = ".adventofcode.session";
const SESSION_COOKIE_ENV: &str = "ADVENT_OF_CODE_SESSION";
const DEFAULT_COL_WIDTH: usize = 80;

fn main() -> Result<(), String> {
Expand All @@ -34,6 +36,8 @@ fn main() -> Result<(), String> {
fn read_session_cookie(session_file: &Option<String>) -> String {
let path = if let Some(file) = session_file {
PathBuf::from(file)
} else if let Ok(cookie) = env::var(SESSION_COOKIE_ENV) {
cookie
} else if let Some(file) = dirs::home_dir()
.map(|dir| dir.join(HIDDEN_SESSION_COOKIE_FILE))
.filter(|file| file.exists())
Expand Down

0 comments on commit 0e72af3

Please sign in to comment.