Skip to content

Commit 3f38a0a

Browse files
authored
Merge pull request #4 from travbale/main
feat: add template registry for handling template files
2 parents 9343962 + e0348c1 commit 3f38a0a

File tree

573 files changed

+143
-32
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

573 files changed

+143
-32
lines changed

cmd/add.go

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ package cmd
22

33
import (
44
"fmt"
5-
"github.com/neptunsk1y/ignore/version"
6-
"github.com/spf13/cobra"
75
"log"
86
"os"
7+
8+
"github.com/neptunsk1y/ignore/internal/ignore"
9+
"github.com/spf13/cobra"
910
)
1011

1112
var addCommand = &cobra.Command{
@@ -21,35 +22,24 @@ var addCommand = &cobra.Command{
2122
log.Fatal("Error:", err)
2223
}
2324
}
24-
dirname, err := os.UserHomeDir()
25-
if err != nil {
26-
log.Fatal(err)
27-
}
28-
_, err = version.Latest()
29-
if err != nil {
30-
fmt.Println("Error version check")
31-
}
32-
pathTemplateFile := dirname + "/go/pkg/mod/github.com/neptunsk1y/ignore@v" + version.Version + "/templates/" + args[1] + ".gitignore"
33-
if _, err = os.Stat(pathTemplateFile); err != nil {
34-
if os.IsNotExist(err) {
35-
log.Fatal("The template does not exist")
36-
} else {
37-
log.Fatal("Error:", err)
38-
}
39-
}
40-
templateFile, err := os.ReadFile(pathTemplateFile)
41-
if err != nil {
42-
log.Fatal("Error reading the file")
25+
26+
tr := ignore.NewTemplateRegistry()
27+
template := args[1]
28+
if !tr.HasTemplate(template) {
29+
log.Fatal("template does not exist")
4330
}
31+
4432
file, err := os.OpenFile(pathFile, os.O_APPEND|os.O_WRONLY, 0666)
4533
if err != nil {
4634
log.Fatal(err)
4735
}
4836
defer file.Close()
49-
if _, err := file.WriteString("\n" + string(templateFile)); err != nil {
37+
38+
err = tr.CopyTemplate(template, file)
39+
if err != nil {
5040
log.Fatal(err)
5141
}
52-
fmt.Println(args[1] + " template has been added to ." + args[0] + "ignore")
42+
fmt.Printf("%s template has been added to .%signore\n", template, args[0])
5343
},
5444
}
5545

cmd/list.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,19 @@ package cmd
22

33
import (
44
"fmt"
5+
6+
"github.com/neptunsk1y/ignore/internal/ignore"
57
"github.com/spf13/cobra"
6-
"log"
7-
"os"
88
)
99

1010
var listCommand = &cobra.Command{
1111
Use: "list",
1212
Short: "available templates for .ignore files",
1313
Run: func(cmd *cobra.Command, args []string) {
14-
files, err := os.ReadDir("templates/")
15-
if err != nil {
16-
log.Fatal(err)
17-
}
18-
for _, file := range files {
19-
fmt.Printf("%s ", file.Name()[:len(file.Name())-10])
14+
tr := ignore.NewTemplateRegistry()
15+
templates := tr.List()
16+
for _, template := range templates {
17+
fmt.Printf("%s ", template)
2018
}
2119
},
2220
}

internal/ignore/templates.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package ignore
2+
3+
import (
4+
"bytes"
5+
"embed"
6+
"fmt"
7+
"io"
8+
"io/fs"
9+
"path/filepath"
10+
"strings"
11+
)
12+
13+
//go:embed templates/*
14+
var templates embed.FS
15+
16+
// TemplateRegistry is a collection of all ignore templates available for use.
17+
type TemplateRegistry struct {
18+
templates fs.FS
19+
}
20+
21+
// NewTemplateRegistry creates a new instance of TemplateRegistry.
22+
func NewTemplateRegistry() *TemplateRegistry {
23+
return &TemplateRegistry{templates: templates}
24+
}
25+
26+
// HasTemplate indicates if the registry contains a spcfic template.
27+
func (tr *TemplateRegistry) HasTemplate(name string) bool {
28+
_, err := fs.Stat(tr.templates, fmt.Sprintf("templates/%s.gitignore", name))
29+
return err == nil
30+
}
31+
32+
// List returns a list of all templates contained in the registry.
33+
func (tr *TemplateRegistry) List() []string {
34+
var templates []string
35+
fs.WalkDir(tr.templates, ".", func(path string, d fs.DirEntry, _ error) error {
36+
if d.IsDir() {
37+
return nil
38+
}
39+
template := strings.TrimSuffix(filepath.Base(path), ".gitignore")
40+
templates = append(templates, template)
41+
return nil
42+
})
43+
return templates
44+
}
45+
46+
// CopyTemplate copys the contents of a template corresponding to the
47+
// provided name to the provided writer.
48+
func (tr *TemplateRegistry) CopyTemplate(name string, dst io.Writer) error {
49+
b, err := fs.ReadFile(tr.templates, fmt.Sprintf("templates/%s.gitignore", name))
50+
if err != nil {
51+
return err
52+
}
53+
io.Copy(dst, bytes.NewReader(b))
54+
return nil
55+
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)