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

Support relative path for snippet's filename attribute #322

Open
wants to merge 17 commits into
base: main
Choose a base branch
from

Conversation

csessh
Copy link

@csessh csessh commented Oct 11, 2024

closes #320

This pull request contains changes to support both relative and absolute path types for snippet file's filename attribute.

Given the test case listed in #320:

The same case presented in the issue

Given this config:

[General]
...
SnippetDirs = []
SnippetFile = "~/.config/test/test.toml"
...

SnippetFile is a relative path which expands to /home/csessh/.config/test/test.toml

Current Pet version:

fw13 :: ~/Documents/pet ‹feat-syspath*› » pet new 
Command> ping google.com
Description> hey Google
fw13 :: ~/Documents/pet ‹feat-syspath*› » cat ~/.config/test/test.toml 
   1   │ 
   2   │ [[Snippets]]
   3   │   Description = "hey Google"
   4   │   Filename = "/home/csessh/.config/test/test.toml"
   5   │   Output = ""
   6   │   Tag = []
   7   │   command = "ping google.com"

As expected, ~/.config/test/test.toml expands to /home/csessh/.config/test/test.toml

Using feature branch version:

fw13 :: ~/Documents/pet ‹feat-syspath*› » go run main.go new      
Command> ping google.com
Description> Hey Goolge
fw13 :: ~/Documents/pet ‹feat-syspath*› » go run main.go edit
fw13 :: ~/Documents/pet ‹feat-syspath*› » cat ~/.config/test/test.toml
   1   │ 
   2   │ [[Snippets]]
   3   │   Description = "Hey Goolge"
   4   │   Filename = "~/.config/test/test.toml"
   5   │   Output = ""
   6   │   Tag = []
   7   │   command = "ping google.com"

file path stays the same. The same result applies for the following test case:

[General]
...
SnippetDirs = []
SnippetFile = "/home/csessh/.config/test/test.toml"
...

Full path is rendered for every snippets.

I didn't go too deep into testing filename with the use of multiple directories as I ran into #321 problem. However, I did include the safeguard check for it in this PR too.

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@csessh
Copy link
Author

csessh commented Oct 15, 2024

@RamiAwar could you review this PR, please?

cmd/new.go Outdated Show resolved Hide resolved
@RamiAwar
Copy link
Collaborator

How are you handling interaction with snippet directories?

@csessh
Copy link
Author

csessh commented Oct 15, 2024

Directories can also be expressed in relative path format, e.g. ~/.config/pet/.
This PR doesn't touch this feature's logic, apart from expanding paths to ensure snippet files can be read/opened.

cmd/new.go Outdated Show resolved Hide resolved
@csessh csessh requested a review from RamiAwar October 16, 2024 08:55
@csessh
Copy link
Author

csessh commented Oct 24, 2024

Resolved conflicts

@RamiAwar
Copy link
Collaborator

Awesome, thank you! @csessh Will review this carefully this week. Thanks for waiting 🙏🏼 ✨

config/config_test.go Outdated Show resolved Hide resolved
cmd/new.go Outdated Show resolved Hide resolved
for _, dir := range cfg.General.SnippetDirs {
snippetdirs = append(snippetdirs, expandPath(dir)) // note the = instead of :=
if !strings.HasSuffix(dir, "/") {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this necessary?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I recall correctly, that's because it would lead to new snippet filename being incorrect.

snippetFile = config.Conf.General.SnippetDirs[0] + fmt.Sprintf("%s.toml", strings.ToLower(sanitize.BaseName(snippet.Description)))

for example:

/var/tmp/petNewCommand.toml

instead of

/var/tmp/pet/NewCommand.toml

config/config.go Outdated Show resolved Hide resolved
config/config.go Outdated Show resolved Hide resolved
snippet/snippet.go Outdated Show resolved Hide resolved
snippet/snippet.go Outdated Show resolved Hide resolved
@csessh csessh requested a review from RamiAwar November 7, 2024 22:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: No status
Development

Successfully merging this pull request may close these issues.

Support relative paths for snippet's filename attribute - only absolute works now
3 participants