mirror of
https://github.com/onyx-and-iris/gignore.git
synced 2025-03-31 10:51:19 +01:00
rename GignoreClient to Client
add docstrings
This commit is contained in:
parent
8e53c4fbeb
commit
32714f55d2
@ -1,3 +1,4 @@
|
||||
// Package main generates gitignore.io templates using the gogi library.
|
||||
package main
|
||||
|
||||
import (
|
||||
|
@ -1,3 +1,5 @@
|
||||
// Package main provides the entry point for the gignore command-line tool,
|
||||
// which generates .gitignore files based on specified templates.
|
||||
package main
|
||||
|
||||
import (
|
||||
@ -70,7 +72,7 @@ func main() {
|
||||
fmt.Printf("√ created %s .gitignore file\n", args[0])
|
||||
}
|
||||
|
||||
func listTemplates(client *gignore.GignoreClient) error {
|
||||
func listTemplates(client *gignore.Client) error {
|
||||
templates, err := client.List()
|
||||
if err != nil {
|
||||
return err
|
||||
|
1
error.go
1
error.go
@ -1,3 +1,4 @@
|
||||
// Package gignore provides functionality for handling template errors and registry operations.
|
||||
package gignore
|
||||
|
||||
import (
|
||||
|
44
gignore.go
44
gignore.go
@ -3,50 +3,54 @@ package gignore
|
||||
import (
|
||||
"io"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
||||
"github.com/onyx-and-iris/gignore/internal/filewriter"
|
||||
"github.com/onyx-and-iris/gignore/internal/registry"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
//go:generate go run cmd/gen/gen.go
|
||||
|
||||
type GignoreClient struct {
|
||||
// Client is a client for managing .gitignore templates.
|
||||
type Client struct {
|
||||
registry *registry.TemplateRegistry
|
||||
writer io.Writer
|
||||
}
|
||||
|
||||
func New(options ...Option) *GignoreClient {
|
||||
gc := &GignoreClient{
|
||||
// New creates a new Client with the provided options.
|
||||
func New(options ...Option) *Client {
|
||||
c := &Client{
|
||||
registry.New(),
|
||||
filewriter.New()}
|
||||
|
||||
for _, option := range options {
|
||||
option(gc)
|
||||
filewriter.New(),
|
||||
}
|
||||
|
||||
return gc
|
||||
for _, option := range options {
|
||||
option(c)
|
||||
}
|
||||
|
||||
return c
|
||||
}
|
||||
|
||||
func (g *GignoreClient) List() ([]string, error) {
|
||||
return g.registry.ListTemplates()
|
||||
// List returns a list of available .gitignore templates.
|
||||
func (c *Client) List() ([]string, error) {
|
||||
return c.registry.ListTemplates()
|
||||
}
|
||||
|
||||
func (g *GignoreClient) Create(template string) error {
|
||||
ok, err := g.registry.Contains(template)
|
||||
// Create generates a .gitignore file from the specified template.
|
||||
func (c *Client) Create(template string) error {
|
||||
ok, err := c.registry.Contains(template)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !ok {
|
||||
templateNotFoundErr := &templateNotFoundError{template, g.registry}
|
||||
if g.registry.Directory == "gitignoreio" {
|
||||
templateNotFoundErr := &templateNotFoundError{template, c.registry}
|
||||
if c.registry.Directory == "gitignoreio" {
|
||||
return templateNotFoundErr
|
||||
}
|
||||
|
||||
log.Errorf("%s. Checking default registry...", templateNotFoundErr)
|
||||
|
||||
g.registry.Directory = "gitignoreio"
|
||||
ok, err = g.registry.Contains(template)
|
||||
c.registry.Directory = "gitignoreio"
|
||||
ok, err = c.registry.Contains(template)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -56,12 +60,12 @@ func (g *GignoreClient) Create(template string) error {
|
||||
log.Infof("template '%s' found in default gitignoreio registry", template)
|
||||
}
|
||||
|
||||
content, err := g.registry.GetTemplate(template)
|
||||
content, err := c.registry.GetTemplate(template)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = g.writer.Write(content)
|
||||
_, err = c.writer.Write(content)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -1,3 +1,4 @@
|
||||
// Package filewriter provides functionality to write content to a .gitignore file.
|
||||
package filewriter
|
||||
|
||||
import (
|
||||
@ -6,11 +7,14 @@ import (
|
||||
"os"
|
||||
)
|
||||
|
||||
// FileWriter provides functionality to write content to a .gitignore file.
|
||||
type FileWriter struct {
|
||||
targetFileName string
|
||||
}
|
||||
|
||||
// New creates a new FileWriter with the default target file name.
|
||||
func New() *FileWriter {
|
||||
return &FileWriter{}
|
||||
return &FileWriter{".gitignore"}
|
||||
}
|
||||
|
||||
func (fw *FileWriter) writeContent(content []byte, dst io.Writer) (int64, error) {
|
||||
@ -25,7 +29,7 @@ func (fw *FileWriter) writeContent(content []byte, dst io.Writer) (int64, error)
|
||||
}
|
||||
|
||||
func (fw *FileWriter) Write(content []byte) (int, error) {
|
||||
f, err := os.Create(".gitignore")
|
||||
f, err := os.Create(fw.targetFileName)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
@ -1,3 +1,4 @@
|
||||
// Package registry provides functionality to manage and retrieve gitignore templates.
|
||||
package registry
|
||||
|
||||
import (
|
||||
@ -10,11 +11,13 @@ import (
|
||||
//go:embed templates
|
||||
var templates embed.FS
|
||||
|
||||
// TemplateRegistry provides methods to manage and retrieve gitignore templates.
|
||||
type TemplateRegistry struct {
|
||||
templates fs.FS
|
||||
Directory string
|
||||
}
|
||||
|
||||
// New creates a new instance of TemplateRegistry.
|
||||
func New() *TemplateRegistry {
|
||||
return &TemplateRegistry{
|
||||
templates: templates,
|
||||
@ -25,6 +28,7 @@ func (t *TemplateRegistry) filePath(name string) string {
|
||||
return fmt.Sprintf("templates/%s/%s.gitignore", t.Directory, name)
|
||||
}
|
||||
|
||||
// Contains checks if a template with the given name exists in the registry.
|
||||
func (t *TemplateRegistry) Contains(name string) (bool, error) {
|
||||
_, err := fs.Stat(t.templates, t.filePath(name))
|
||||
if err != nil {
|
||||
@ -37,6 +41,7 @@ func (t *TemplateRegistry) Contains(name string) (bool, error) {
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// GetTemplate retrieves the content of the gitignore template with the given name.
|
||||
func (t *TemplateRegistry) GetTemplate(name string) ([]byte, error) {
|
||||
data, err := fs.ReadFile(t.templates, t.filePath(name))
|
||||
if err != nil {
|
||||
@ -45,6 +50,7 @@ func (t *TemplateRegistry) GetTemplate(name string) ([]byte, error) {
|
||||
return data, nil
|
||||
}
|
||||
|
||||
// ListTemplates lists all the gitignore templates in the registry.
|
||||
func (t *TemplateRegistry) ListTemplates() ([]string, error) {
|
||||
var paths []string
|
||||
|
||||
|
@ -1,9 +1,11 @@
|
||||
package gignore
|
||||
|
||||
type Option func(*GignoreClient)
|
||||
// Option is a function that configures a GignoreClient.
|
||||
type Option func(*Client)
|
||||
|
||||
// WithTemplateDirectory sets the template directory for the GignoreClient.
|
||||
func WithTemplateDirectory(directory string) Option {
|
||||
return func(g *GignoreClient) {
|
||||
g.registry.Directory = directory
|
||||
return func(c *Client) {
|
||||
c.registry.Directory = directory
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user