Skip to content

Commit

Permalink
Merge pull request #9 from ModProg/session-file-in-.config
Browse files Browse the repository at this point in the history
feat: support `.config/adventofcode.session`
  • Loading branch information
scarvalhojr authored Dec 5, 2022
2 parents 57b9e37 + 281b5aa commit 5fcd679
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 18 deletions.
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

0 comments on commit 5fcd679

Please # to comment.