From a7ed84d3353c50cef9dc10757be377f209b4295b Mon Sep 17 00:00:00 2001 From: Roland Fredenhagen Date: Sun, 4 Dec 2022 20:29:26 +0100 Subject: [PATCH] feat: support environment variable containing session fixes #8 --- README.md | 6 +++++- src/main.rs | 4 ++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 39002a1..d453bf0 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/main.rs b/src/main.rs index 64199e3..9afe3f7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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> { @@ -34,6 +36,8 @@ fn main() -> Result<(), String> { fn read_session_cookie(session_file: &Option) -> String { let path = if let Some(file) = session_file { PathBuf::from(file) + } else if let Ok(cookie) = env::var(SESSION_COOKIE_ENV) { + return cookie; } else if let Some(file) = dirs::home_dir() .map(|dir| dir.join(HIDDEN_SESSION_COOKIE_FILE)) .filter(|file| file.exists())