Skip to content

Commit a1e8356

Browse files
committed
stuff
1 parent 4daea57 commit a1e8356

File tree

8 files changed

+83
-41
lines changed

8 files changed

+83
-41
lines changed

.fleet/run.json

Lines changed: 0 additions & 10 deletions
This file was deleted.

.idea/.gitignore

Lines changed: 0 additions & 3 deletions
This file was deleted.

.idea/misc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/workspace.xml

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/credhelper/mod.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,17 @@ pub fn spawn(helper: &str, operation: &str) -> Result<Child> {
3939
cmd
4040
})
4141
}
42+
43+
pub fn run(helper: &str, operation: &str, params: Params) -> Result<params::Params> {
44+
let process = spawn(helper, operation)?;
45+
eprintln!("Opening stdin of helper");
46+
let mut stdin = process.stdin.unwrap();
47+
eprintln!("Writing to stdin of helper");
48+
params.write_to(&mut stdin)?;
49+
50+
eprintln!("Opening to stdout of helper");
51+
let stdout = process.stdout.unwrap();
52+
let output = params::from_stream(stdout)?;
53+
eprintln!("Parsing stdout of helper");
54+
Ok(output)
55+
}

src/credhelper/params.rs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ impl Params {
2727
pub fn add(&mut self, name: String, value: String) {
2828
self.hashmap.insert(name, value);
2929
}
30-
30+
3131
pub fn contains(&self, name: String) -> bool {
3232
self.hashmap.contains_key(&name)
3333
}
34-
34+
3535
pub fn add_from_string(&mut self, s: &String) -> Result<(), ParamParserError> {
3636
for (i, &character) in s.as_bytes().iter().enumerate() {
3737
if character == b'=' {
@@ -47,39 +47,41 @@ impl Params {
4747
});
4848
}
4949

50-
pub fn write_to_sdtout(&self) {
50+
pub fn write_to_sdtout(&self) -> std::io::Result<()> {
5151
let mut stdout = io::stdout().lock();
52-
self.write_to(&mut stdout);
52+
self.write_to(&mut stdout)?;
53+
Ok(())
5354
}
5455

55-
pub fn write_to<T: std::io::Write>(&self, stream: &mut T) {
56+
pub fn write_to<T: std::io::Write>(&self, stream: &mut T) -> std::io::Result<()> {
5657
for (key, value) in self.hashmap.iter() {
57-
stream.write_fmt(format_args!("{0}={1}", key, value));
58+
stream.write_fmt(format_args!("{0}={1}", key, value))?;
5859
}
59-
stream.write_all(b"\n");
60+
stream.write_all(b"\n")?;
61+
Ok(())
6062
}
6163
}
6264

63-
pub fn from_stream<T: std::io::Read>(stream: T) -> Result<Params, ParamParserError> {
65+
pub fn from_stream<T: std::io::Read>(stream: T) -> Result<Params, Box<dyn std::error::Error>> {
6466
let mut params = Params {
6567
hashmap: HashMap::new(),
6668
};
67-
69+
6870
let mut buf_reader = std::io::BufReader::new(stream);
71+
let mut buffer = String::new();
6972
loop {
70-
let mut buffer = String::new();
71-
buf_reader.read_line(&mut buffer);
72-
if buffer == "" {
73+
buffer.clear();
74+
75+
buf_reader.read_line(&mut buffer)?;
76+
if buffer.trim().is_empty() {
7377
break;
7478
}
7579
params.add_from_string(&buffer)?;
7680
}
7781
return Ok(params);
7882
}
7983

80-
81-
82-
pub fn from_stdin() -> Result<Params, ParamParserError> {
84+
pub fn from_stdin() -> Result<Params, Box<dyn std::error::Error>> {
8385
let mut stdin = io::stdin().lock();
8486
from_stream(&mut stdin)
8587
}

src/main.rs

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::ghauth::AccessTokenPollError;
55
use clap::{Parser, Subcommand};
66
use reqwest::Client;
77

8-
use std::string::String;
8+
use std::{io::Read, string::String};
99

1010
#[derive(Parser)]
1111
#[command(author, version)]
@@ -20,6 +20,10 @@ struct Cli {
2020
#[arg(short = 'p', long)]
2121
no_prompt: bool,
2222

23+
///If set logs everything gh-login is doing to stderr
24+
#[arg(short = 'l', long)]
25+
log: bool,
26+
2327
#[command(subcommand)]
2428
operation: Commands,
2529
}
@@ -56,24 +60,38 @@ async fn main() {
5660
eprintln!("NOTE: use --no-prompt to disable this message");
5761
}
5862

59-
let process = credhelper::spawn(&cli.backing_helper, "get").unwrap();
60-
let mut stdout = process.stdout.unwrap();
61-
let mut stdin = process.stdin.unwrap();
63+
if cli.log {
64+
eprintln!("Reading parameters from stdin");
65+
}
66+
let params = credhelper::params::from_stdin().expect("Failled to read data from stdin");
6267

63-
std::io::copy(&mut std::io::stdin(), &mut stdin)
64-
.expect("Error sending data to backing helper");
68+
if cli.log {
69+
eprintln!("Running backing helper '{}'", &cli.backing_helper);
70+
}
71+
let mut output = credhelper::run(&cli.backing_helper, "get", params)
72+
.expect("Failled to run backing helper");
6573

66-
let mut returned_params = credhelper::params::from_stream(&mut stdout)
67-
.expect("Invalid data returned by backing helper");
74+
if cli.log {
75+
eprintln!("Done running credentials helper '{}'", &cli.backing_helper);
76+
}
6877

69-
if !returned_params.contains(String::from("password")) {
78+
if !output.contains(String::from("password")) {
79+
if cli.log {
80+
eprintln!("No password returned by helper. Fetching credentails..");
81+
}
7082
let client = reqwest::Client::new();
7183
let access_token = get_access_token_via_device_code(&client).await;
7284

73-
returned_params.add(String::from("password"), access_token.access_token);
85+
output.add(String::from("password"), access_token.access_token);
86+
}
87+
88+
if cli.log {
89+
eprintln!("Done. Writing credentails to stdout");
7490
}
7591

76-
returned_params.write_to_sdtout();
92+
output
93+
.write_to_sdtout()
94+
.expect("Failled to write to stdout");
7795
}
7896
}
7997
}

0 commit comments

Comments
 (0)