Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support environment variable containing session #10

Merged
merged 1 commit into from
Dec 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Comment on lines -92 to +94
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added some linebreaks to match the rest

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 {
ModProg marked this conversation as resolved.
Show resolved Hide resolved
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())
Expand Down