Skip to content

Commit bc9f9f2

Browse files
committed
add: option to read the GitHub PAT from a file
Files can be made readable to certail users only while command line arguments are world-readable.
1 parent 7cdb116 commit bc9f9f2

File tree

3 files changed

+45
-6
lines changed

3 files changed

+45
-6
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
/issues
33
/pulls
44
state.json
5+
pat.sec

src/main.rs

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const EXIT_CREATING_DIRS: u8 = 1;
2828
const EXIT_CREATING_OCTOCRAB_INSTANCE: u8 = 2;
2929
const EXIT_API_ERROR: u8 = 3;
3030
const EXIT_WRITING: u8 = 3;
31+
const EXIT_NO_PAT: u8 = 4;
3132

3233
mod types;
3334

@@ -472,18 +473,52 @@ fn get_last_backup_time(destination: PathBuf) -> Option<DateTime<Utc>> {
472473
}
473474
}
474475

476+
fn personal_access_token(args: Args) -> Option<String> {
477+
if let Some(pat) = args.personal_access_token {
478+
info!("Using the GitHub personal access token specified on the command line");
479+
return Some(pat);
480+
} else if let Some(pat_file) = args.personal_access_token_file {
481+
info!(
482+
"Reading the GitHub personal access token from '{}'",
483+
pat_file.display()
484+
);
485+
match fs::read_to_string(pat_file.clone()) {
486+
Ok(pat) => {
487+
return Some(pat.trim().to_string());
488+
}
489+
Err(e) => {
490+
error!(
491+
"Could not read GitHub personal access token from '{}': {}",
492+
pat_file.display(),
493+
e
494+
);
495+
return None;
496+
}
497+
}
498+
}
499+
None
500+
}
501+
475502
#[tokio::main]
476503
async fn main() -> ExitCode {
477504
env_logger::Builder::from_env(Env::default().default_filter_or("info")).init();
478505

479506
let args: Args = Args::parse();
480507
info!(
481-
"Starting backup of {}:{} on GitHub to {}..",
508+
"Starting backup of {}:{} on GitHub to '{}'",
482509
args.owner,
483510
args.repo,
484511
args.destination.display()
485512
);
486513

514+
let pat = match personal_access_token(args.clone()) {
515+
Some(pat) => pat,
516+
None => {
517+
error!("No GitHub personal access token present - exiting.");
518+
return ExitCode::from(EXIT_NO_PAT);
519+
}
520+
};
521+
487522
let issues_dir = args.destination.join("issues");
488523
let pulls_dir = args.destination.join("pulls");
489524
info!(
@@ -512,7 +547,7 @@ async fn main() -> ExitCode {
512547
let last_backup_time: Option<DateTime<Utc>> = get_last_backup_time(args.destination.clone());
513548

514549
let instance = match octocrab::OctocrabBuilder::default()
515-
.personal_token(args.personal_access_token)
550+
.personal_token(pat)
516551
.build()
517552
{
518553
Ok(instance) => instance,

src/types.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ impl fmt::Display for WriteError {
3636
}
3737
}
3838

39-
#[derive(Parser, Debug)]
39+
#[derive(Parser, Debug, Clone)]
4040
#[command(author, version, about, long_about = None)]
4141
pub struct Args {
4242
/// Owner of the repository to backup
@@ -45,9 +45,12 @@ pub struct Args {
4545
/// Name of the repository to backup
4646
#[arg(short, long)]
4747
pub repo: String,
48-
/// Personal Access Token to the GitHub API
49-
#[arg(short, long)]
50-
pub personal_access_token: String,
48+
/// Personal Access Token to the GitHub API supplied via the command line
49+
#[arg(short, long, group = "pat")]
50+
pub personal_access_token: Option<String>,
51+
/// Personal Access Token to the GitHub API read from a file
52+
#[arg(short = 'f', long, value_name = "PATH", group = "pat")]
53+
pub personal_access_token_file: Option<PathBuf>,
5154
/// Destination where the backup should be written to
5255
#[arg(short, long, value_name = "PATH")]
5356
pub destination: PathBuf,

0 commit comments

Comments
 (0)