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