Skip to content

Commit

Permalink
feat: stderr
Browse files Browse the repository at this point in the history
  • Loading branch information
lei4519 committed Jun 18, 2022
1 parent da6c59d commit 9d072ac
Show file tree
Hide file tree
Showing 9 changed files with 182 additions and 80 deletions.
45 changes: 34 additions & 11 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,48 @@ env:
CARGO_TERM_COLOR: always

jobs:
release:
version:
runs-on: ubuntu-latest
outputs:
new_tag: ${{ steps.tag_version.outputs.new_tag }}
body: ${{ steps.tag_version.outputs.changelog }}

steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Build project
run: cargo build --release --locked

- name: Bump version and push tag
id: tag_version
uses: mathieudutour/github-tag-action@v6.0
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
- name: Create a GitHub release
uses: ncipollo/release-action@v1
with:
token: ${{ secrets.GITHUB_TOKEN }}
name: Release ${{ steps.tag_version.outputs.new_tag }}
tag: ${{ steps.tag_version.outputs.new_tag }}
body: ${{ steps.tag_version.outputs.changelog }}
artifacts: "target/release/extract-chrome-cookies"

release:
needs: version
runs-on: ${{ matrix.os }}

strategy:
matrix:
include:
- os: ubuntu-latest
asset_name: extract-chrome-cookies-linux-amd64.tar.gz
- os: macos-latest
asset_name: extract-chrome-cookies-macos-amd64.tar.gz

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Build project
run: cargo build --release --locked && ./scripts/build

- name: Upload binary to release
uses: svenstaro/upload-release-action@v1-release
with:
repo_token: ${{ secrets.GITHUB_TOKEN }}
file: target/release/extract-chrome-cookies.tar.gz
asset_name: ${{ matrix.asset_name }}
tag: ${{ needs.version.outputs.new_tag }}
release_name: Release ${{ needs.version.outputs.new_tag }}
body: ${{ needs.version.outputs.changelog }}

18 changes: 14 additions & 4 deletions Cargo.lock

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

7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,9 @@ clap = "3.1.18"
home = "0.5.3"
colored = "2"
url = "2.2.2"
tldextract = "0.5.1"
tldextract = "0.5.1"

[build-dependencies]
clap = "3.1.18"
clap_complete = "3.2.1"

24 changes: 24 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use clap_complete::{
generate_to,
shells::{Bash, Fish, Zsh},
};
use std::{env, io::Error};

include!("src/cli.rs");

fn main() -> Result<(), Error> {
let name = "extract-chrome-cookies";

let mut release_dir = env::current_dir()?;
release_dir.push("target/release");

let mut cmd = build_cli();

generate_to(Bash, &mut cmd, name, &release_dir)?;

generate_to(Zsh, &mut cmd, name, &release_dir)?;

generate_to(Fish, &mut cmd, name, &release_dir)?;

Ok(())
}
8 changes: 8 additions & 0 deletions scripts/build
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash

name=extract-chrome-cookies

cd ./target/release

tar -czvf $name.tar.gz $name _$name $name.bash $name.fish

35 changes: 24 additions & 11 deletions scripts/install
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
#!/bin/bash

ROOT_DIR=$(cd `dirname $0`; cd ..; pwd)
name=extract-chrome-cookies

# 删除仓库的 .git 目录
if test -e $ROOT_DIR/.git
then
rm -rf $ROOT_DIR/.git
fi
latest_version=$(curl -L -s -H 'Accept: application/json' https://github.com/lei4519/$name/releases/latest | sed -e 's/.*"tag_name":"\([^"]*\)".*/\1/')

if test -x "/usr/local/bin/extract-chrome-cookies"
then
rm /usr/local/bin/extract-chrome-cookies
fi
download_url=https://github.com/lei4519/$name/releases/download/$latest_version/$name.tar.gz

cd $HOME

ln -s $ROOT_DIR/release/extract-chrome-cookies /usr/local/bin/extract-chrome-cookies
mkdir -p .$name

cd .$name

curl -fsSLO $download_url

tar -xzvf $name.tar.gz

mkdir -p /usr/local/bin
ln -fbs $name /usr/local/bin/$name

mkdir -p /usr/local/share/zsh/site-functions
ln -fbs _$name /usr/local/share/zsh/site-functions/_$name

mkdir -p /usr/local/share/bash-completion/completions
lb -fbs $name.bash /usr/local/share/bash-completion/completions/$name.bash

mkdir -p /usr/local/share/fish/vendor_completions.d
ln -fbs $name.fish /usr/local/share/fish/vendor_completions.d/$name.fish

14 changes: 14 additions & 0 deletions scripts/uninstall
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash

name=extract-chrome-cookies

rm -f /usr/local/bin/$name

rm -f /usr/local/share/zsh/site-functions/_$name

rm -f /usr/local/share/bash-completion/completions/$name.bash

rm -f /usr/local/share/fish/vendor_completions.d/$name.fish

rm -rf $HOME/.$name

38 changes: 38 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use clap::{Arg, Command};

pub fn build_cli() -> Command<'static> {
Command::new("extract-chrome-cookies")
.version("0.0.1")
.about("Getting cookies by reading Chrome's cookie database on your computer.")
.arg(
Arg::new("url")
.required(true)
.help("Extract the cookie of this URL"),
)
// .arg(
// Arg::new("format")
// .short('f')
// .takes_value(true)
// .default_value("header")
// .validator(|v| {
// if FORMATS.contains(&v) {
// return Ok(());
// }
// Err(format!("{}{:?}", "Must One of ", &FORMATS))
// })
// .help(FORMAT_HELP),
// )
.arg(
Arg::new("profile_name")
.short('n')
.takes_value(true)
.default_value("Default")
.help("Chrome Profile Name"),
)
.arg(
Arg::new("profile_path")
.short('p')
.takes_value(true)
.help("Direct pass chrome profile path"),
)
}
73 changes: 20 additions & 53 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use cli::build_cli;
use colored::*;
use core::result::Result;
use crypto::{
Expand All @@ -20,7 +21,9 @@ use std::{
use tldextract::{TldExtractor, TldOption};
use url::Url;

use clap::{Arg, ArgMatches, Command};
use clap::ArgMatches;

mod cli;

static SALT: &[u8; 9] = b"saltysalt";
const KEYLENGTH: usize = 16;
Expand Down Expand Up @@ -52,7 +55,8 @@ fn main() {
match run(matchs) {
Ok(_) => {}
Err(e) => {
println!("{}", e.red());
eprint!("{}", e.red());
io::stderr().flush().unwrap();
exit(1);
}
};
Expand Down Expand Up @@ -101,78 +105,41 @@ fn run(matchs: ArgMatches) -> Result<(), String> {
Ok(())
}

fn build_cli() -> Command<'static> {
Command::new("extract-chrome-cookies")
.version("0.0.1")
.about("Getting cookies by reading Chrome's cookie database on your computer.")
.arg(
Arg::new("url")
.required(true)
.help("Extract the cookie of this URL"),
)
// .arg(
// Arg::new("format")
// .short('f')
// .takes_value(true)
// .default_value("header")
// .validator(|v| {
// if FORMATS.contains(&v) {
// return Ok(());
// }
// Err(format!("{}{:?}", "Must One of ", &FORMATS))
// })
// .help(FORMAT_HELP),
// )
.arg(
Arg::new("profile_name")
.short('n')
.takes_value(true)
.default_value("Default")
.help("Chrome Profile Name"),
)
.arg(
Arg::new("profile_path")
.short('p')
.takes_value(true)
.help("Direct pass chrome profile path"),
)
}

fn cant_resolve_profile_path(home_path: Option<String>) -> ! {
if home_path.is_none() {
print!("{}\n\n", "The program can't resolve your Google profile path automatically, Please use '-p' option to pass it in manually.".red());
eprint!("{}\n\n", "The program can't resolve your Google profile path automatically, Please use '-p' option to pass it in manually.".red());
} else {
print!("{}\n\n", "The program can't resolve your Google profile path automatically, you need to pass it in manually.".red());
eprint!("{}\n\n", "The program can't resolve your Google profile path automatically, you need to pass it in manually.".red());
}
print!("{}\n", "Follow these steps find your profile path:".green());
print!(" {}\n", "1. Open Chrome");
print!(" {}\n", "2. Enter 'chrome://version'");
print!(" {}\n\n", "3. Find 'profile path'");
eprint!("{}\n", "Follow these steps find your profile path:".green());
eprint!(" {}\n", "1. Open Chrome");
eprint!(" {}\n", "2. Enter 'chrome://version'");
eprint!(" {}\n\n", "3. Find 'profile path'");

if home_path.is_some() {
let path = get_profile_path(&home_path.unwrap(), "<PROFILE NAME>");
print!("{}\n", "If the path looks like this:".green());
print!(" {}\n\n", path);
print!("{}\n", "You just pass in the profile name:".green());
print!(
eprint!("{}\n", "If the path looks like this:".green());
eprint!(" {}\n\n", path);
eprint!("{}\n", "You just pass in the profile name:".green());
eprint!(
" {}\n\n",
"extract-chrome-cookies <URL> -n <PROFILE NAME>".cyan()
);

print!(
eprint!(
"{}\n",
"Otherwise, you need to pass in the entire profile path:".green()
);
} else {
print!("{}\n", "Run command:".green());
eprint!("{}\n", "Run command:".green());
}

print!(
eprint!(
" {}",
"extract-chrome-cookies <URL> -p <PROFILE PATH>".cyan()
);

io::stdout().flush().unwrap();
io::stderr().flush().unwrap();

exit(1)
}
Expand Down

0 comments on commit 9d072ac

Please sign in to comment.