Skip to content

Commit

Permalink
move dynamic auto-complete to rust (#113)
Browse files Browse the repository at this point in the history
* move dynamic auto-complete to rust

* fix scoop
  • Loading branch information
omer-shtivi authored Jan 24, 2024
1 parent 722bca3 commit cbe6f1d
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 30 deletions.
5 changes: 0 additions & 5 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,6 @@ jobs:
run: |
chmod a+x satori
./satori auto_complete --generate zsh --out satori_auto_complete.zsh
sed -e 's|:datastore_name -- The name as defined in Satori data portal:|:datastore_name -- The name as defined in Satori data portal:_datastores|g' -e 's|:database -- Database name:|:database -- Database name:_databases|g' satori_auto_complete.zsh > satori_auto_complete_new.zsh
mv satori_auto_complete_new.zsh satori_auto_complete.zsh
cat auto_complete_functions/script.zsh >> satori_auto_complete.zsh
- name: Create release
run: |
zip -jr satori-${{ steps.get_version.outputs.version-without-v }}-macOS.zip satori satori_auto_complete.zsh
Expand Down
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ The Satori CLI enables you to access all of your available datasets from the com
- [Overview](#overview)
- [Installation](#installation)
- [Mac](#mac)
- [Windows](#windows)
- [Supported datastores](#supported-datastores)
- [Usage](#usage)
- [Run](#run)
Expand Down Expand Up @@ -38,6 +39,18 @@ Add the following line to your shell configuration file (e.g., ~/.bashrc or ~/.z
```
Once the login is completed (one time), the auto-complete will work.

### Windows
**SCOOP**
To install using [scoop](https://scoop.sh/), run the following command:
```powershell
scoop bucket add satori https://www.github.com/satoricyber/satori-cli
scoop install satori/satori_cli
```

***Download***
You can download the latest version from the [releases page](https://github.com/SatoriCyber/satori-cli/releases)
Download the windows exe file and place it in a folder that is in your PATH.

**first time use**
To enable auto-complete, run the [login](#login) command.

Expand Down
19 changes: 0 additions & 19 deletions bucket/satori.json

This file was deleted.

File renamed without changes.
54 changes: 48 additions & 6 deletions src/cli/auto_complete.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,60 @@
use std::{fs::File, io::BufWriter, path::PathBuf};
use std::{
fs::File,
io::{self, Cursor, Write},
path::PathBuf,
};

use clap::Command;
use clap_complete::{generate, Generator, Shell};

use crate::cli::command;

const AUTO_COMPLETE_FUNCTIONS_ZSH: &str = include_str!("autocomplete_functions/zshell.zsh");

pub fn auto_complete(shell: Shell, out: PathBuf) {
let mut cmd = command::get();
eprintln!("Generating completion file for {shell}...");
let file = File::create(out).unwrap();
let mut buf_writer = BufWriter::new(file);
completions_to_file(shell, &mut cmd, &mut buf_writer);
let mut buffer: Vec<u8> = Vec::new();
let mut cursor = io::Cursor::new(&mut buffer);

generate_autocomplete_buf(shell, &mut cmd, &mut cursor);
let auto_complete_script = String::from_utf8(buffer).unwrap();
let auto_complete_script = make_dynamic_completion_script(shell, &auto_complete_script);
let mut output_file = File::create(out).unwrap();
output_file
.write_all(auto_complete_script.as_bytes())
.unwrap();
}

fn generate_autocomplete_buf<G: Generator>(
gen: G,
cmd: &mut Command,
buf: &mut Cursor<&mut Vec<u8>>,
) {
generate(gen, cmd, cmd.get_name().to_string(), buf);
}

fn make_dynamic_completion_script(shell: Shell, auto_complete_static: &str) -> String {
match shell {
Shell::Bash => todo!(),
Shell::Elvish => unimplemented!(),
Shell::Fish => unimplemented!(),
Shell::PowerShell => todo!(),
Shell::Zsh => handle_zsh(auto_complete_static),
_ => todo!(),
}
}

fn completions_to_file<G: Generator>(gen: G, cmd: &mut Command, file: &mut BufWriter<File>) {
generate(gen, cmd, cmd.get_name().to_string(), file);
fn handle_zsh(auto_complete_static: &str) -> String {
let mut auto_complete_static = auto_complete_static
.replace(
"datastore_name -- The name as defined in Satori data portal:",
"datastore_name -- The name as defined in Satori data portal:_datastores",
)
.replace(
"database -- Database name:",
"database -- Database name:_databases",
);
auto_complete_static.push_str(AUTO_COMPLETE_FUNCTIONS_ZSH);
auto_complete_static
}
25 changes: 25 additions & 0 deletions src/cli/autocomplete_functions/zshell.zsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@


_datastores() {
local datastore_names=()

# Read keys line by line and populate the array
while IFS= read -r line; do
datastore_names+=("$line")
done < <(satori list --datastores)

_values datastore_name $datastore_names
}


_databases() {
local datastore_name="$words[CURRENT-1]" # Get the selected datastore_name
datastore_name=${datastore_name//\\/}
local databases=()
while IFS= read -r line; do
databases+=("$line")
done < <(satori list --databases $datastore_name)
if [[ ! -z "$databases" ]]; then
_values database $databases
fi
}

0 comments on commit cbe6f1d

Please sign in to comment.