Skip to content

Commit

Permalink
refactor: Improved error handling in snippet multi-files
Browse files Browse the repository at this point in the history
  • Loading branch information
RamiAwar committed Aug 19, 2024
1 parent be81733 commit e2753dc
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 18 deletions.
3 changes: 1 addition & 2 deletions cmd/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ func filter(options []string, tag string) (commands []string, err error) {

snippetTexts[t] = s
if config.Flag.Color || config.Conf.General.Color {
t = config.Conf.General.Format
t = strings.Replace(format, "$command", command, 1)
t = strings.Replace(t, "$description", color.HiRedString(s.Description), 1)
t = strings.Replace(t, "$tags", color.HiCyanString(tags), 1)
Expand Down Expand Up @@ -107,7 +106,7 @@ func filter(options []string, tag string) (commands []string, err error) {
func selectFile(options []string, tag string) (snippetFile string, err error) {
var snippets snippet.Snippets
if err := snippets.Load(); err != nil {
return snippetFile, fmt.Errorf("Load snippet failed: %v", err)
return snippetFile, fmt.Errorf("load snippet failed: %v", err)
}

if 0 < len(tag) {
Expand Down
7 changes: 6 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os/exec"
"path/filepath"
"runtime"

"github.com/pelletier/go-toml"
"github.com/pkg/errors"
)
Expand All @@ -24,7 +25,7 @@ type Config struct {
// GeneralConfig is a struct of general config
type GeneralConfig struct {
SnippetFile string
SnippetDirs []string
SnippetDirs []string
Editor string
Column int
SelectCmd string
Expand Down Expand Up @@ -88,6 +89,10 @@ func (cfg *Config) Load(file string) error {
_, err := os.Stat(file)
if err == nil {
f, err := os.ReadFile(file)
if err != nil {
return err
}

err = toml.Unmarshal(f, cfg)
if err != nil {
return err
Expand Down
44 changes: 29 additions & 15 deletions snippet/snippet.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"sort"
"strings"

"github.com/kennygrant/sanitize"
"github.com/knqyf263/pet/config"
"github.com/pelletier/go-toml"
Expand All @@ -25,27 +26,40 @@ type SnippetInfo struct {

// Load reads toml file.
func (snippets *Snippets) Load() error {
var files []string
if config.Conf.General.SnippetFile != "" {
files = append(files, config.Conf.General.SnippetFile)
var snippetFiles []string

snippetFile := config.Conf.General.SnippetFile
if snippetFile != "" {
if _, err := os.Stat(snippetFile); err == nil {
snippetFiles = append(snippetFiles, snippetFile)
} else if !os.IsNotExist(err) {
return fmt.Errorf("failed to load snippet file. %v", err)
}
}

for _, dir := range config.Conf.General.SnippetDirs {
files = append(files, getFiles(dir)...)
for _, dir := range config.Conf.General.SnippetDirs {
if _, err := os.Stat(dir); err != nil {
if os.IsNotExist(err) {
return fmt.Errorf("snippet directory not found. %s", dir)
}
return fmt.Errorf("failed to load snippet directory. %v", err)
}
snippetFiles = append(snippetFiles, getFiles(dir)...)
}

for _, file := range files {
// Read files and load snippets
for _, file := range snippetFiles {
tmp := Snippets{}
f, err := os.ReadFile(file)
if err != nil {
return fmt.Errorf("failed to load snippet file. %v", err)
}
err = toml.Unmarshal(f, tmp)
if err != nil {
return fmt.Errorf("failed to parse snippet file. %v", err)
}
if err != nil {
return fmt.Errorf("failed to load snippet file. %v", err)
}

err = toml.Unmarshal(f, &tmp)
if err != nil {
return fmt.Errorf("failed to parse snippet file. %v", err)
}

for _, snippet := range tmp.Snippets {
snippet.Filename = file
snippets.Snippets = append(snippets.Snippets, snippet)
Expand Down

0 comments on commit e2753dc

Please sign in to comment.