Skip to content
New issue

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

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

Already on GitHub? # to your account

feat: support .config/adventofcode.session #9

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
42 changes: 32 additions & 10 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ path = "src/main.rs"
[dependencies]
chrono = "0.4"
clap = { version = "4", features = ["color", "derive"]}
home = "0.5"
html2text = "0.4"
regex = "1.7"
reqwest = { version = "0.11", features = ["blocking"] }
term_size = "0.3"
html2md = "0.2"
dirs = "4.0.0"

# Use static linking of OpenSSL on Linux with MUSL
[target.x86_64-unknown-linux-musl.dependencies]
openssl = { version = "0.10", features = ["vendored"] }
openssl = { version = "0.10", features = ["vendored"] }
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ input and submit your answer, you need an adventofcode.com session cookie. To
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) in a file called `.adventofcode.session` in your home
directory. This file should only contain your session number, in a single line.
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.

## Usage

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

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

fn main() -> Result<(), String> {
Expand All @@ -34,10 +34,15 @@ 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 Some(dir) = home_dir() {
} else if let Some(file) = dirs::home_dir()
.map(|dir| dir.join(HIDDEN_SESSION_COOKIE_FILE))
.filter(|file| file.exists())
{
file
} else if let Some(dir) = dirs::config_dir() {
dir.join(SESSION_COOKIE_FILE)
} else {
eprintln!("error: Failed to find home directory.");
eprintln!("error: Failed to find config directory.");
exit(2);
};

Expand Down