Skip to content

Commit bdf70b9

Browse files
added ability to open config on default editor or specified editor
1 parent 4d71797 commit bdf70b9

File tree

13 files changed

+267
-122
lines changed

13 files changed

+267
-122
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "snip-cli"
3-
version = "0.1.1"
3+
version = "0.1.2"
44
edition = "2021"
55
authors = ["Uriah G. <codeitlikemiley@gmail.com>"]
66
description = "A CLI tool (snip-cli) for managing Neovim and VSCode snippets"
@@ -14,24 +14,20 @@ categories = ["command-line-utilities"]
1414
name = "snip"
1515
path = "src/main.rs"
1616

17-
# Used when Bunding for OSX
18-
[[bin]]
19-
name = "snip-cli"
20-
path = "src/main.rs"
21-
2217

2318
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
2419

2520
[dependencies]
26-
anyhow = "1.0.75"
27-
clap = { version = "4.4.11", features = ["cargo", "derive", "string"] }
28-
serde = { version = "1.0.193", features = ["derive"] }
29-
serde_json = "1.0.108"
30-
tokio = { version = "1.35.0", features = ["full"] }
31-
tempfile = { version = "3.8.1", features = [] }
21+
anyhow = "1.0.94"
22+
clap = { version = "4.5.23", features = ["cargo", "derive", "string"] }
23+
serde = { version = "1.0.215", features = ["derive"] }
24+
serde_json = "1.0.133"
25+
tokio = { version = "1.42.0", features = ["full"] }
26+
tempfile = { version = "3.14.0", features = [] }
3227
prettytable = "0.10.0"
3328
dirs-next = "2.0.0"
3429
dotenv = { version = "0.15.0", features = ["clap"] }
30+
opener = "0.7.2"
3531

3632
[package.metadata.bundle]
3733
name = "snip" # The name of your application
@@ -40,7 +36,7 @@ copyright = "Copyright (c) codeitlikemiley 2023. All rights reserved."
4036
category = "Developer Tool"
4137
short_description = "A CLI tool for managing Neovim and VSCode snippets"
4238
long_description = "A CLI tool for managing Neovim and VSCode snippets"
43-
version = "0.1.1" # Version of your application
39+
version = "0.1.2" # Version of your application
4440
osx_url_schemes = [
4541
"com.codeitlikemiley.snip",
4642
] # URL schemes your application supports

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,3 +230,13 @@ snip config --help
230230
# Config Snippet
231231
snip config <path>
232232
```
233+
234+
10. Edit Config
235+
236+
```sh
237+
snip open
238+
# or we can pass in the editor command e.g. code
239+
snip open --editor code
240+
```
241+
242+
This will open the configuration file in the default editor, or the editor specified with the `--editor` flag.

provision.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ cargo clean
44
rm snip.pkg
55
cargo zigbuild --release
66
cargo bundle --release
7-
pkgbuild --root ./target/release/bundle/osx/snip.app --install-location "/Applications/snip.app" --identifier com.codeitlikemiley.snip --version 0.1.0 --scripts ./scripts snip.pkg
7+
pkgbuild --root ./target/release/bundle/osx/snip.app --install-location "/Applications/snip.app" --identifier com.codeitlikemiley.snip --version 0.1.2 --scripts ./scripts snip.pkg

scripts/postinstall

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ if [ -L "$USER_HOME/.local/bin/snip" ]; then
1717
fi
1818

1919
# Create the symlink in ~/.local/bin
20-
ln -s "/Applications/snip.app/Contents/MacOS/snip-cli" "$USER_HOME/.local/bin/snip"
20+
ln -s "/Applications/snip.app/Contents/MacOS/snip" "$USER_HOME/.local/bin/snip"

src/actions.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
pub mod create_directory_and_file;
22
pub mod edit_snippet_in_file;
33
pub mod list_snippets;
4+
pub mod open_file_with;
45
pub mod remove_snippet_from_file;
56
pub mod search_snippets;
67
pub mod show_snippet;

src/actions/create_directory_and_file.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ pub fn create_directory_and_file(file_path: &str) -> anyhow::Result<()> {
1515
OpenOptions::new()
1616
.write(true)
1717
.create(true) // This will create the file if it doesn't exist
18+
.truncate(false)
1819
.open(file_path)
1920
.context("Failed to create or open the file")?;
2021

src/actions/open_file_with.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
use anyhow::{Context, Result};
2+
use opener;
3+
use std::{fs, process::Command};
4+
5+
/// Opens the file specified in the configuration JSON file using the system's default text editor.
6+
///
7+
/// # Arguments
8+
///
9+
/// * `config_path` - A string slice that holds the path to the JSON configuration file.
10+
///
11+
/// # Returns
12+
///
13+
/// * `Result<()>` - Returns `Ok` if the file is successfully opened, otherwise returns an error.
14+
pub fn open_file_with(config_path: &str, editor: Option<String>) -> Result<()> {
15+
// Read the configuration file
16+
let config_content = fs::read_to_string(config_path)
17+
.with_context(|| format!("Failed to read the configuration file at: {}", config_path))?;
18+
19+
// Parse the JSON to extract the `path` field
20+
let config: serde_json::Value = serde_json::from_str(&config_content)
21+
.with_context(|| "Failed to parse the configuration file as JSON")?;
22+
23+
// Extract the `path` field
24+
let file_path = config["path"].as_str().ok_or_else(|| {
25+
anyhow::anyhow!("`path` field is missing or invalid in configuration file")
26+
})?;
27+
28+
// Open the file with the default editor
29+
// If the editor is not provided, use the default editor
30+
if editor.is_none() {
31+
opener::open(file_path)
32+
.with_context(|| format!("Failed to open the file at path: {}", file_path))
33+
.map_err(|err| anyhow::anyhow!("Failed to open the file: {}", err))?;
34+
} else {
35+
// Use the provided editor
36+
let editor = editor.unwrap();
37+
// get the path to the editor
38+
Command::new(editor.clone())
39+
.arg(file_path)
40+
.spawn()
41+
.with_context(|| {
42+
format!(
43+
"Failed to open the file at path: {} with program: {}",
44+
file_path, editor
45+
)
46+
})?;
47+
}
48+
Ok(())
49+
}

src/helpers/get_app_config.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ pub fn get_app_config() -> String {
1919
#[cfg(test)]
2020
mod tests {
2121
use super::*;
22-
use std::io::{stdout, Write};
2322

2423
#[test]
2524
#[ignore]
@@ -34,7 +33,7 @@ mod tests {
3433
// Act
3534
let actual = get_app_config();
3635
// For debugging purpose on CI
37-
writeln!(stdout(), "DEBUGGER ENV CONFIG PATH: {}", actual).unwrap();
36+
println!("DEBUGGER ENV CONFIG PATH: {}", actual);
3837
// Assert
3938
assert_eq!(actual, expected);
4039
}

src/helpers/is_fuzzy_match.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ mod tests {
1616

1717
#[test]
1818
fn test_is_fuzzy_match() {
19-
assert_ne!(is_fuzzy_match("moon", "bd"), true);
20-
assert_ne!(is_fuzzy_match("moon", "mp"), true);
21-
assert_eq!(is_fuzzy_match("moon", "mn"), true);
22-
assert_eq!(is_fuzzy_match("moon", "oon"), true);
19+
assert!(!is_fuzzy_match("moon", "bd"));
20+
assert!(!is_fuzzy_match("moon", "mp"));
21+
assert!(is_fuzzy_match("moon", "mn"));
22+
assert!(is_fuzzy_match("moon", "oon"));
2323
}
2424
}

0 commit comments

Comments
 (0)