Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate shell completions and man page #122

Merged
merged 5 commits into from
Oct 31, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -104,20 +104,27 @@ jobs:
- name: Install dependencies for musl libc
run: |
sudo apt-get update
sudo apt-get install musl-tools
sudo apt-get install help2man musl-tools

- name: Run cargo build
uses: actions-rs/cargo@v1
with:
command: build
args: --release --target x86_64-unknown-linux-musl
env:
GEN_COMPLETIONS: 1

- name: Run cargo test
uses: actions-rs/cargo@v1
with:
command: test
args: --target x86_64-unknown-linux-musl

- name: Build man page and find completions
run: |
help2man target/x86_64-unknown-linux-musl/release/ouch > ouch.1
cp -r target/x86_64-unknown-linux-musl/release/build/ouch-*/out/completions .

- name: Strip binary
run: strip target/x86_64-unknown-linux-musl/release/ouch

Expand All @@ -127,6 +134,17 @@ jobs:
name: 'ouch-x86_64-linux-musl'
path: target/x86_64-unknown-linux-musl/release/ouch

- name: Upload completions
uses: actions/upload-artifact@v2
with:
name: completions
path: completions

- name: Upload man page
uses: actions/upload-artifact@v2
with:
name: ouch.1
path: ouch.1

x86_64_glibc:
name: Ubuntu 20.04 (glibc)
Expand Down
10 changes: 10 additions & 0 deletions Cargo.lock

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

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ zip = { version = "0.5.13", default-features = false, features = ["defl
flate2 = { version = "1.0.22", default-features = false, features = ["zlib"] }
zstd = { version = "0.9.0", default-features = false, features = ["thin"] }

[build-dependencies]
clap = "=3.0.0-beta.5"
clap_generate = "=3.0.0-beta.5"

[dev-dependencies]
tempfile = "3.2.0"
infer = "0.5.0"
Expand Down
22 changes: 22 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use clap::{ArgEnum, IntoApp};
use clap_generate::{generate_to, Shell};

use std::{env, fs::create_dir_all, path::Path};

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

fn main() {
println!("cargo:rerun-if-env-changed=GEN_COMPLETIONS");

if env::var_os("GEN_COMPLETIONS") != Some("1".into()) {
return;
}

let out = &Path::new(&env::var_os("OUT_DIR").unwrap()).join("completions");
create_dir_all(out).unwrap();
let app = &mut Opts::into_app();

for shell in Shell::value_variants() {
generate_to(*shell, app, "ouch", out).unwrap();
}
}
marcospb19 marked this conversation as resolved.
Show resolved Hide resolved
3 changes: 2 additions & 1 deletion src/archive/tar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ use walkdir::WalkDir;

use crate::{
info,
utils::{self, Bytes, QuestionPolicy},
utils::{self, Bytes},
QuestionPolicy,
};

pub fn unpack_archive(
Expand Down
3 changes: 2 additions & 1 deletion src/archive/zip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ use zip::{self, read::ZipFile, ZipArchive};

use crate::{
info,
utils::{self, dir_is_empty, strip_cur_dir, Bytes, QuestionPolicy},
utils::{self, dir_is_empty, strip_cur_dir, Bytes},
QuestionPolicy,
};

use self::utf8::get_invalid_utf8_paths;
Expand Down
46 changes: 2 additions & 44 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,51 +5,9 @@ use std::{
vec::Vec,
};

use clap::{Parser, ValueHint};
use clap::Parser;

pub use crate::utils::QuestionPolicy;
use crate::Error;

#[derive(Parser, Debug)]
#[clap(version, about)]
pub struct Opts {
/// Skip overwrite questions positively.
#[clap(short, long, conflicts_with = "no")]
pub yes: bool,

/// Skip overwrite questions negatively.
#[clap(short, long)]
pub no: bool,

#[clap(subcommand)]
pub cmd: Subcommand,
}

#[derive(Parser, PartialEq, Eq, Debug)]
pub enum Subcommand {
/// Compress files. Alias: c
#[clap(alias = "c")]
Compress {
/// Files to be compressed
#[clap(required = true, min_values = 1)]
files: Vec<PathBuf>,

/// The resulting file. Its extensions specify how the files will be compressed and they need to be supported
#[clap(required = true, value_hint = ValueHint::FilePath)]
output: PathBuf,
},
/// Compress files. Alias: d
#[clap(alias = "d")]
Decompress {
/// Files to be decompressed
#[clap(required = true, min_values = 1)]
files: Vec<PathBuf>,

/// Decompress files in a directory other than the current
#[clap(short, long, value_hint = ValueHint::DirPath)]
output: Option<PathBuf>,
},
}
use crate::{Error, Opts, QuestionPolicy, Subcommand};

impl Opts {
/// A helper method that calls `clap::Parser::parse` and then translates relative paths to absolute.
Expand Down
7 changes: 2 additions & 5 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,14 @@ use utils::colors;

use crate::{
archive,
cli::{Opts, Subcommand},
error::FinalError,
extension::{
self,
CompressionFormat::{self, *},
},
info,
utils::nice_directory_display,
utils::to_utf,
utils::{self, dir_is_empty, QuestionPolicy},
Error,
utils::{self, dir_is_empty, nice_directory_display, to_utf},
Error, Opts, QuestionPolicy, Subcommand,
};

// Used in BufReader and BufWriter to perform less syscalls
Expand Down
7 changes: 5 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,21 @@
//! 2. It's required by some integration tests at tests/ folder.

// Public modules
pub mod cli;
pub mod archive;
pub mod commands;

// Private modules
pub mod archive;
mod cli;
mod dialogs;
mod error;
mod extension;
mod macros;
mod opts;
mod utils;

pub use error::{Error, Result};
pub use opts::{Opts, Subcommand};
pub use utils::QuestionPolicy;

/// The status code ouch has when an error is encountered
pub const EXIT_FAILURE: i32 = libc::EXIT_FAILURE;
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use ouch::{cli::Opts, commands, Result};
use ouch::{commands, Opts, Result};

fn main() {
if let Err(err) = run() {
Expand Down
44 changes: 44 additions & 0 deletions src/opts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use clap::{Parser, ValueHint};

use std::path::PathBuf;

#[derive(Parser, Debug)]
#[clap(version, about)]
pub struct Opts {
/// Skip overwrite questions positively.
#[clap(short, long, conflicts_with = "no")]
pub yes: bool,

/// Skip overwrite questions negatively.
#[clap(short, long)]
pub no: bool,

#[clap(subcommand)]
pub cmd: Subcommand,
}

#[derive(Parser, PartialEq, Eq, Debug)]
pub enum Subcommand {
/// Compress files. Alias: c
#[clap(alias = "c")]
Compress {
/// Files to be compressed
#[clap(required = true, min_values = 1)]
files: Vec<PathBuf>,

/// The resulting file. Its extensions specify how the files will be compressed and they need to be supported
#[clap(required = true, value_hint = ValueHint::FilePath)]
output: PathBuf,
},
/// Compress files. Alias: d
#[clap(alias = "d")]
Decompress {
/// Files to be decompressed
#[clap(required = true, min_values = 1)]
files: Vec<PathBuf>,

/// Decompress files in a directory other than the current
#[clap(short, long, value_hint = ValueHint::DirPath)]
output: Option<PathBuf>,
},
}
5 changes: 1 addition & 4 deletions tests/compress_and_decompress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ use std::{
time::Duration,
};

use ouch::{
cli::{Opts, QuestionPolicy, Subcommand},
commands::run,
};
use ouch::{commands::run, Opts, QuestionPolicy, Subcommand};
use rand::{rngs::SmallRng, RngCore, SeedableRng};
use tempfile::NamedTempFile;
use utils::*;
Expand Down
5 changes: 1 addition & 4 deletions tests/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ use std::{
path::{Path, PathBuf},
};

use ouch::{
cli::{Opts, QuestionPolicy, Subcommand},
commands::run,
};
use ouch::{commands::run, Opts, QuestionPolicy, Subcommand};

pub fn create_empty_dir(at: &Path, filename: &str) -> PathBuf {
let dirname = Path::new(filename);
Expand Down