Skip to content

Commit

Permalink
add completions
Browse files Browse the repository at this point in the history
  • Loading branch information
GoldsteinE committed Sep 3, 2022
1 parent c1db31e commit f3dd18c
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 87 deletions.
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.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ base64 = "0.13.0"
bitvec = "1.0.1"
chacha20poly1305 = "0.10.1"
clap = { version = "3.2.20", features = ["clap_derive", "derive", "wrap_help"] }
clap_complete = "3.2.4"
color-eyre = "0.6.2"
crc-any = "2.4.3"
either = { version = "1.8.0", features = ["serde"] }
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Use a simple encryption program and put the config into a NixOS module. Just lik
url = "github:GoldsteinE/classified";
# to avoid having one more copy of nixpkgs
inputs.nixpkgs.follows = "nixpkgs";
# you can also do this with rust-overlay and naersk
# you can also do this with naersk
};
};
outputs = { nixpkgs, classified, ... }: {
Expand Down
69 changes: 4 additions & 65 deletions flake.lock

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

35 changes: 15 additions & 20 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -1,36 +1,29 @@
{
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
rust-overlay.url = "github:oxalica/rust-overlay";
flake-utils.url = "github:numtide/flake-utils";
naersk.url = "github:nix-community/naersk";
naersk.inputs.nixpkgs.follows = "nixpkgs";
};

outputs = { self, nixpkgs, rust-overlay, flake-utils, naersk }:
outputs = { self, nixpkgs, flake-utils, naersk }:
flake-utils.lib.eachDefaultSystem (system:
let
packageName = "classified";

overlays = [ (import rust-overlay) ];
pkgs = import nixpkgs {
inherit system overlays;
};
rust = (pkgs.rust-bin.stable.latest.default.override {
extensions = [
"rust-src"
"cargo"
"rustc"
"rustfmt"
];
});
naersk-lib = naersk.lib."${system}".override {
cargo = rust;
rustc = rust;
inherit system;
};
naersk-lib = naersk.lib."${system}";
in rec {
packages.${packageName} = naersk-lib.buildPackage {
pname = "${packageName}";
root = ./.;
postInstall = ''
mkdir -p $out/share/{bash-completion/completions,zsh/site-functions,fish/vendor_completions.d}
$out/bin/classified completions bash > $out/share/bash-completion/completions/classified.bash
$out/bin/classified completions zsh > $out/share/zsh/site-functions/_classified
$out/bin/classified completions fish > $out/share/fish/vendor_completions.d/classified.fish
'';
};
defaultPackage = packages.${packageName};

Expand All @@ -40,9 +33,11 @@
nixosModules.default = import ./module.nix defaultApp;

devShell = pkgs.mkShell {
buildInputs = [
rust
pkgs.rust-analyzer
buildInputs = with pkgs;[
rustc
cargo
rustfmt
rust-analyzer
];
};
}
Expand Down
37 changes: 36 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use chacha20poly1305::{
aead::{Aead as _, Key, Nonce},
AeadCore, KeyInit as _, XChaCha20Poly1305 as Cipher,
};
use clap::Parser;
use clap::{CommandFactory as _, Parser, Subcommand};
use color_eyre::eyre::{self, eyre, WrapErr as _};
use indexmap::IndexMap;
use itertools::Itertools as _;
Expand All @@ -45,19 +45,41 @@ use crate::config::{Config, FileDesc};
mod config;
mod keyarmor;

#[derive(Subcommand)]
enum Shell {
Bash,
Zsh,
Fish,
}

impl From<Shell> for clap_complete::Shell {
fn from(this: Shell) -> Self {
match this {
Shell::Bash => Self::Bash,
Shell::Zsh => Self::Zsh,
Shell::Fish => Self::Fish,
}
}
}

#[allow(clippy::doc_markdown)]
/// Simple encryption tool intended for use with NixOS
#[derive(Parser)]
enum Command {
/// Generate a new encryption key and print it to stdout
#[clap(display_order = 1)]
GenKey,
/// Encrypt file or stdin with given encryption key and print result to stdout (armored as
/// base64)
#[clap(display_order = 2)]
Encrypt {
/// Path to the key file
#[clap(short, long)]
key: PathBuf,
/// File to encrypt, stdin if absent
file: Option<PathBuf>,
},
#[clap(display_order = 3)]
/// Decrypt file that was previously encrypted with `encrypt` and print result to stdout
Decrypt {
/// Path to the key file
Expand All @@ -66,11 +88,16 @@ enum Command {
/// File to decrypt, stdin if absent
file: Option<PathBuf>,
},
#[clap(display_order = 4)]
/// Decrypt multiple files to their target directories, according to JSON/TOML config
Batch {
/// Config file, stdin if absent
config: Option<PathBuf>,
},
#[clap(display_order = 5)]
/// Generate shell completions
#[clap(subcommand)]
Completions(Shell),
}

fn trim_newline(mut x: &[u8]) -> &[u8] {
Expand Down Expand Up @@ -229,6 +256,14 @@ fn main() -> eyre::Result<()> {
contents.zeroize();
}
}
Command::Completions(shell) => {
clap_complete::generate(
clap_complete::Shell::from(shell),
&mut Command::command(),
"classified",
&mut io::stdout().lock(),
);
}
}

Ok(())
Expand Down

0 comments on commit f3dd18c

Please sign in to comment.