-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added ability to open config on default editor or specified editor
- Loading branch information
1 parent
4d71797
commit bdf70b9
Showing
13 changed files
with
267 additions
and
122 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "snip-cli" | ||
version = "0.1.1" | ||
version = "0.1.2" | ||
edition = "2021" | ||
authors = ["Uriah G. <[email protected]>"] | ||
description = "A CLI tool (snip-cli) for managing Neovim and VSCode snippets" | ||
|
@@ -14,24 +14,20 @@ categories = ["command-line-utilities"] | |
name = "snip" | ||
path = "src/main.rs" | ||
|
||
# Used when Bunding for OSX | ||
[[bin]] | ||
name = "snip-cli" | ||
path = "src/main.rs" | ||
|
||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
anyhow = "1.0.75" | ||
clap = { version = "4.4.11", features = ["cargo", "derive", "string"] } | ||
serde = { version = "1.0.193", features = ["derive"] } | ||
serde_json = "1.0.108" | ||
tokio = { version = "1.35.0", features = ["full"] } | ||
tempfile = { version = "3.8.1", features = [] } | ||
anyhow = "1.0.94" | ||
clap = { version = "4.5.23", features = ["cargo", "derive", "string"] } | ||
serde = { version = "1.0.215", features = ["derive"] } | ||
serde_json = "1.0.133" | ||
tokio = { version = "1.42.0", features = ["full"] } | ||
tempfile = { version = "3.14.0", features = [] } | ||
prettytable = "0.10.0" | ||
dirs-next = "2.0.0" | ||
dotenv = { version = "0.15.0", features = ["clap"] } | ||
opener = "0.7.2" | ||
|
||
[package.metadata.bundle] | ||
name = "snip" # The name of your application | ||
|
@@ -40,7 +36,7 @@ copyright = "Copyright (c) codeitlikemiley 2023. All rights reserved." | |
category = "Developer Tool" | ||
short_description = "A CLI tool for managing Neovim and VSCode snippets" | ||
long_description = "A CLI tool for managing Neovim and VSCode snippets" | ||
version = "0.1.1" # Version of your application | ||
version = "0.1.2" # Version of your application | ||
osx_url_schemes = [ | ||
"com.codeitlikemiley.snip", | ||
] # URL schemes your application supports | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
use anyhow::{Context, Result}; | ||
use opener; | ||
use std::{fs, process::Command}; | ||
|
||
/// Opens the file specified in the configuration JSON file using the system's default text editor. | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `config_path` - A string slice that holds the path to the JSON configuration file. | ||
/// | ||
/// # Returns | ||
/// | ||
/// * `Result<()>` - Returns `Ok` if the file is successfully opened, otherwise returns an error. | ||
pub fn open_file_with(config_path: &str, editor: Option<String>) -> Result<()> { | ||
// Read the configuration file | ||
let config_content = fs::read_to_string(config_path) | ||
.with_context(|| format!("Failed to read the configuration file at: {}", config_path))?; | ||
|
||
// Parse the JSON to extract the `path` field | ||
let config: serde_json::Value = serde_json::from_str(&config_content) | ||
.with_context(|| "Failed to parse the configuration file as JSON")?; | ||
|
||
// Extract the `path` field | ||
let file_path = config["path"].as_str().ok_or_else(|| { | ||
anyhow::anyhow!("`path` field is missing or invalid in configuration file") | ||
})?; | ||
|
||
// Open the file with the default editor | ||
// If the editor is not provided, use the default editor | ||
if editor.is_none() { | ||
opener::open(file_path) | ||
.with_context(|| format!("Failed to open the file at path: {}", file_path)) | ||
.map_err(|err| anyhow::anyhow!("Failed to open the file: {}", err))?; | ||
} else { | ||
// Use the provided editor | ||
let editor = editor.unwrap(); | ||
// get the path to the editor | ||
Command::new(editor.clone()) | ||
.arg(file_path) | ||
.spawn() | ||
.with_context(|| { | ||
format!( | ||
"Failed to open the file at path: {} with program: {}", | ||
file_path, editor | ||
) | ||
})?; | ||
} | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters