Skip to content

Commit efe1d95

Browse files
committed
cleanup and packaging scripts
1 parent 9a7fc7e commit efe1d95

File tree

10 files changed

+192
-56
lines changed

10 files changed

+192
-56
lines changed

.idea/workspace.xml

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

Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,15 @@
22
name = "gh-login"
33
authors = ["ldev"]
44
version = "0.1.0"
5+
license-file="LICENSE.md"
56
edition = "2021"
7+
description=""
68
homepage="https://github.com/Xgames123/gh-login"
79

10+
[[bin]]
11+
name = "credential-gh-login"
12+
path = "src/main.rs"
13+
814
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
915

1016
[dependencies]

README.md

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,29 @@
11
# gh-login
2-
A simple git credentials manager for github
2+
<p align="center">
3+
<img src="https://github.com/Xgames123/gh-login/blob/main/gh-login-logo_transparent.png?raw=true" alt="gh-login-logo"/>
4+
</p>
5+
A simple git credentials manager for GitHub
6+
7+
It authenticates to GitHub and uses a backing credential helper, so you can use normal git credential helpers.
8+
9+
## Install
10+
1. Install gh-login for your os (see below)
11+
2. Set gh-login as your git credential helper
12+
13+
```git config --global credential.https://github.com.helper 'gh-login -b cache'```
14+
15+
You can change cache to any credential helper you like
16+
17+
If that gives an error you can also manually edit $HOME/.gitconfig
18+
19+
### Arch linux
20+
Install gh-login form the AUR
21+
```bash
22+
git clone aur.archlinux.org/gh-login.git
23+
cd gh-login
24+
makepkg --syncdeps --install
25+
```
26+
27+
### Debian/Ubuntu
28+
Download latest release and run ```dpkg -i gh-login.deb```
29+
Replace ```gh-login.deb``` with the file you just downloaded

pack.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
echo Packaging gh-login-debian-armv7
3+
packaging/debian/pack.sh armv7-unknown-linux-gnueabihf
4+
5+
echo Packaging gh-login-debian-x86_64
6+
packaging/debian/pack.sh x86_64-unknown-linux-gnu

packaging/aur/gh-login/PKGBUILD

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
pkgver=0.1.0
2+
3+
pkgname=gh-login
4+
pkgdesc="A simple git credentials helper for github"
5+
license=(MIT)
6+
url="https://github.com/Xgames123/gh-login"
7+
8+
binname=credential-$pkgname
9+
source=("$pkgname-$pkgver.tar.gz::https://github.com/Xgames123/$pkgname/archive/$pkgver.tar.gz")
10+
makedepends=(
11+
cargo
12+
)
13+
14+
15+
prepare() {
16+
export RUSTUP_TOOLCHAIN=stable
17+
cargo fetch --locked --target "$CARCH-unknown-linux-gnu"
18+
}
19+
20+
21+
build() {
22+
export RUSTUP_TOOLCHAIN=stable
23+
export CARGO_TARGET_DIR=target
24+
cargo build --frozen --release --all-features
25+
}
26+
27+
check() {
28+
export RUSTUP_TOOLCHAIN=stable
29+
cargo test --frozen --all-features
30+
}
31+
32+
package() {
33+
install -Dm0755 -t "$pkgdir/bin/" "target/release/$binname"
34+
}

packaging/debian/pack.sh

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
pkgver=0.1.0
2+
3+
pkgname=gh-login
4+
pkgdesc="A simple git credentials helper for github"
5+
license=MIT
6+
url="https://github.com/Xgames123/gh-login"
7+
maintianer=ldev
8+
9+
TARGET=$1
10+
echo "os: Debain"
11+
echo "target: $TARGET"
12+
13+
14+
CARCH=$(echo $TARGET | grep -o "^[^-]*")
15+
builddir=/tmp
16+
fullbuilddir=$builddir/gh-login
17+
binname=credential-$pkgname
18+
19+
debarch=$CARCH
20+
if [ "$CARCH" = "x86_64" ]; then
21+
debarch="amd64"
22+
fi
23+
if [ "$CARCH" = "armv7" ]; then
24+
debarch="arm"
25+
fi
26+
27+
28+
rm -rf $fullbuilddir
29+
mkdir -p $fullbuilddir
30+
cp -rf packaging/debian/gh-login $builddir
31+
32+
# Generate control file
33+
controlfile=$fullbuilddir/DEBIAN/control
34+
echo "" > $controlfile
35+
echo "Package: $pkgname" >> $controlfile
36+
echo "Version: $pkgver" >> $controlfile
37+
echo "Maintainer: $maintianer" >> $controlfile
38+
echo "Architecture: $debarch" >> $controlfile
39+
echo "Description: $pkgdesc" >> $controlfile
40+
echo "Homepage: $url" >> $controlfile
41+
echo "Section: vcs" >> $controlfile
42+
43+
# Build rust app
44+
export RUSTUP_TOOLCHAIN=stable
45+
cargo fetch --locked --target $TARGET
46+
47+
export RUSTUP_TOOLCHAIN=stable
48+
export CARGO_TARGET_DIR=target
49+
cargo build --frozen --release --all-features
50+
51+
export RUSTUP_TOOLCHAIN=stable
52+
cargo test --frozen --all-features
53+
54+
# Copy bin to package
55+
mkdir -p $fullbuilddir/bin
56+
cp -f target/release/$binname $fullbuilddir/bin/$binname
57+
chmod +x $fullbuilddir/bin/$binname
58+
59+
# Building package
60+
dpkg-deb --build $fullbuilddir
61+
62+
# Copy to output dir
63+
mkdir -p target/packaging
64+
mv $builddir/gh-login.deb target/packaging/gh-login-debian-$CARCH.deb

src/credhelper/mod.rs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ pub mod params;
22

33
use params::Params;
44
use std::fmt;
5-
use std::io::{Read, Write};
65
use std::process::{Child, Command, Stdio};
76

87
use log::{debug};
@@ -32,22 +31,16 @@ pub fn spawn(helper: &str, operation: &str) -> Result<Child> {
3231
.ok_or_else(|| InvalidHelper.into())
3332
.and_then(|split| {
3433

35-
let program_name = split[0];
36-
37-
let cmd = Command::new(&program_name)
38-
.args(&split[1..])
34+
let mut cmd = Command::new(&split[0]);
35+
cmd.args(&split[1..])
3936
.arg(operation)
4037
.stdin(Stdio::piped())
4138
.stdout(Stdio::piped());
4239

43-
debug!("{}", cmd);
44-
45-
let process = cmd.spawn()
46-
.map_err(|err| err.into());
47-
48-
40+
debug!("Running command {:?}", &cmd);
4941

50-
process
42+
cmd.spawn()
43+
.map_err(|err| err.into())
5144
})
5245
}
5346

@@ -56,10 +49,11 @@ pub fn run(helper: &str, operation: &str, params: Params) -> Result<Params> {
5649
let mut stdin = process.stdin.take().unwrap();
5750
params.write_to(&mut stdin)?;
5851
drop(stdin);
59-
process.wait()?;
6052

6153
let mut stdout = process.stdout.take().unwrap();
62-
6354
let output_params = params::from_stream(&mut stdout)?;
55+
drop(stdout);
56+
57+
process.wait()?;
6458
Ok(output_params)
6559
}

src/credhelper/params.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,31 +28,31 @@ impl Display for Params{
2828

2929

3030
impl Params {
31-
pub fn get(&self, key: String) -> Option<&String> {
32-
return self.hashmap.get(&key);
31+
pub fn get(&self, name: &str) -> Option<&String> {
32+
return self.hashmap.get(name);
3333
}
3434

3535
pub fn add(&mut self, name: String, value: String) {
3636
self.hashmap.insert(name, value);
3737
}
3838

39-
pub fn contains(&self, name: String) -> bool {
40-
self.hashmap.contains_key(&name)
39+
pub fn contains(&self, name: &str) -> bool {
40+
self.hashmap.contains_key(name)
4141
}
4242

4343
pub fn add_from_string(&mut self, s: &String) -> Result<(), ParamParserError> {
4444
for (i, &character) in s.as_bytes().iter().enumerate() {
4545
if character == b'=' {
4646
self.add(
47-
(&s[..i]).trim().to_string(),
48-
(&s[(i + 1)..]).trim().to_string(),
47+
(s[..i]).trim().to_string(),
48+
(s[(i + 1)..]).trim().to_string(),
4949
);
5050
return Ok(());
5151
}
5252
}
53-
return Err(ParamParserError {
53+
Err(ParamParserError {
5454
data: s.to_string(),
55-
});
55+
})
5656
}
5757

5858
pub fn write_to_sdtout(&self) -> std::io::Result<()> {
@@ -63,9 +63,9 @@ impl Params {
6363

6464
pub fn write_to<T: std::io::Write>(&self, stream: &mut T) -> std::io::Result<()> {
6565
for (key, value) in self.hashmap.iter() {
66-
stream.write_fmt(format_args!("{0}={1}", key, value))?;
66+
stream.write_fmt(format_args!("{0}={1}\n", key, value))?;
6767
}
68-
stream.write_all(b"\n")?;
68+
stream.write_all(b"\n\n")?;
6969
Ok(())
7070
}
7171
}
@@ -81,12 +81,13 @@ pub fn from_stream<T: Read>(stream: T) -> Result<Params, Box<dyn Error>> {
8181
buffer.clear();
8282

8383
buf_reader.read_line(&mut buffer)?;
84+
8485
if buffer.trim().is_empty() {
8586
break;
8687
}
8788
params.add_from_string(&buffer)?;
8889
}
89-
return Ok(params);
90+
Ok(params)
9091
}
9192

9293
pub fn from_stdin() -> Result<Params, Box<dyn Error>> {

src/ghauth.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ static OAUTH_SCOPE: &str = "repo";
33

44
use reqwest::StatusCode;
55
use serde::Deserialize;
6-
use std::fmt::{format, Formatter};
6+
use std::fmt::{Formatter};
77
use std::time::{Duration, SystemTime, UNIX_EPOCH};
88
use std::{error::Error, fmt, fmt::Display, thread::sleep};
99

@@ -23,7 +23,7 @@ impl DeviceCode {
2323
if (epoch_time() - self.time) >= self.expires_in {
2424
return true;
2525
}
26-
return false;
26+
false
2727
}
2828
}
2929

@@ -47,7 +47,7 @@ pub fn epoch_time() -> u64 {
4747
.duration_since(UNIX_EPOCH)
4848
.expect("Time went backwards?????");
4949

50-
return since_the_epoch.as_secs();
50+
since_the_epoch.as_secs()
5151
}
5252

5353
#[derive(Debug)]
@@ -56,7 +56,7 @@ pub enum AccessTokenPollError {
5656
Reqwest(reqwest::Error),
5757
}
5858
impl Display for AccessTokenPollError {
59-
fn fmt(&self, fmt: &mut Formatter<'_>) -> std::fmt::Result {
59+
fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
6060
match &self {
6161
AccessTokenPollError::DeviceCodeExpired => fmt.write_str("The device code has expired"),
6262
AccessTokenPollError::Reqwest(err) => {
@@ -82,7 +82,7 @@ pub async fn poll_for_access_token(
8282
),
8383
];
8484

85-
return loop {
85+
loop {
8686
if device_code.expired() {
8787
return Err(AccessTokenPollError::DeviceCodeExpired);
8888
}
@@ -111,7 +111,7 @@ pub async fn poll_for_access_token(
111111
Err(AccessTokenPollError::Reqwest(err))
112112
}
113113
};
114-
};
114+
}
115115
}
116116

117117
pub async fn get_device_code(client: &reqwest::Client) -> Result<DeviceCode, reqwest::Error> {
@@ -127,5 +127,5 @@ pub async fn get_device_code(client: &reqwest::Client) -> Result<DeviceCode, req
127127

128128
device_code.time = epoch_time();
129129

130-
return Ok(device_code);
130+
Ok(device_code)
131131
}

0 commit comments

Comments
 (0)