Compare commits

...

8 Commits

Author SHA1 Message Date
f0b64e3a75 add golangci-lint config + action
Some checks failed
CI / Lint (push) Has been cancelled
2025-03-10 15:21:54 +00:00
32714f55d2 rename GignoreClient to Client
add docstrings
2025-03-10 15:21:27 +00:00
8e53c4fbeb add platform badges 2025-03-10 11:38:31 +00:00
6fc81a4c96 add bin/ to .gitignore 2025-03-10 11:19:37 +00:00
820ed2055b return err from listTemplates 2025-03-10 11:19:30 +00:00
eba8dd0113 log gen message at info level 2025-03-10 11:19:03 +00:00
f262277aca rename TemplateRegistry factory method 2025-03-10 11:18:42 +00:00
c11b94ec0d add upd go modules action 2025-03-10 11:08:37 +00:00
12 changed files with 174 additions and 32 deletions

29
.github/workflows/golang-ci.yml vendored Normal file
View File

@ -0,0 +1,29 @@
name: CI
on:
push:
branches: [ "main" ]
paths:
- '**.go'
pull_request:
branches: [ "main" ]
paths:
- '**.go'
jobs:
lint:
name: Lint
runs-on: ubuntu-latest
if: github.event_name == 'pull_request'
timeout-minutes: 3
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 1
- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: '1.24'
- name: Install golangci-lint
run: go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
- name: Run golangci-lint
run: golangci-lint run ./...

30
.github/workflows/update-go-modules.yml vendored Normal file
View File

@ -0,0 +1,30 @@
name: Auto-Update Go Modules
on:
schedule:
- cron: "0 0 * * 1" # Runs every Monday at midnight
jobs:
update-go-modules:
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- name: Checkout Code
uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: stable
- name: Update Dependencies
run: |
go get -u ./...
go mod tidy
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git add go.mod go.sum
git commit -m "chore: auto-update Go modules"
git push

1
.gitignore vendored
View File

@ -9,6 +9,7 @@
*.dll
*.so
*.dylib
bin/
# Test binary, built with `go test -c`
*.test

54
.golangci.yml Normal file
View File

@ -0,0 +1,54 @@
run:
# timeout for analysis, e.g. 30s, 3m, default is 1m
timeout: 3m
# exclude test files
tests: true
linters:
# Set to true runs only fast linters.
# Good option for 'lint on save', pre-commit hook or CI.
fast: true
disable-all: true
enable:
- gosimple
- govet
- ineffassign
- staticcheck
- unused
- gofmt
- gofumpt
- misspell
- unparam
- gosec
- asciicheck
- errname
- gci
- godot
- goimports
- revive
linters-settings:
gofmt:
rewrite-rules:
- pattern: 'interface{}'
replacement: 'any'
- pattern: 'a[b:len(a)]'
replacement: 'a[b:]'
misspell:
locale: UK
errcheck:
check-type-assertions: true
issues:
max-same-issues: 0
max-issues-per-linter: 0
exclude-use-default: false
exclude:
# gosec: Duplicated errcheck checks
- G104
# gosec: integer overflow conversion int -> uint32
- G115

View File

@ -1,3 +1,8 @@
![Windows](https://img.shields.io/badge/Windows-0078D6?style=for-the-badge&logo=windows&logoColor=white)
![Linux](https://img.shields.io/badge/Linux-FCC624?style=for-the-badge&logo=linux&logoColor=black)
![macOS](https://img.shields.io/badge/mac%20os-000000?style=for-the-badge&logo=macos&logoColor=F0F0F0)
# Gignore - Generate .gitinore files
## Install

View File

@ -1,16 +1,17 @@
// Package main generates gitignore.io templates using the gogi library.
package main
import (
"fmt"
"log"
"os"
"strings"
"github.com/cuonglm/gogi"
log "github.com/sirupsen/logrus"
)
func main() {
fmt.Println("Generating gitignore.io templates...")
log.Info("Generating gitignore.io templates...")
gogiClient, _ := gogi.NewHTTPClient()

View File

@ -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 (
@ -50,7 +52,9 @@ func main() {
client := gignore.New(gignore.WithTemplateDirectory(templateDir))
if list {
listTemplates(client)
if err := listTemplates(client); err != nil {
log.Fatalf("failed to list templates: %v", err)
}
return
}
@ -68,12 +72,13 @@ func main() {
fmt.Printf("√ created %s .gitignore file\n", args[0])
}
func listTemplates(client *gignore.GignoreClient) {
func listTemplates(client *gignore.Client) error {
templates, err := client.List()
if err != nil {
log.Fatalf("failed to list templates: %v", err)
return err
}
for _, template := range templates {
fmt.Println(template)
}
return nil
}

View File

@ -1,3 +1,4 @@
// Package gignore provides functionality for handling template errors and registry operations.
package gignore
import (

View File

@ -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{
registry.NewTemplateRegistry(),
filewriter.New()}
for _, option := range options {
option(gc)
// New creates a new Client with the provided options.
func New(options ...Option) *Client {
c := &Client{
registry.New(),
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
}

View File

@ -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
}

View File

@ -1,3 +1,4 @@
// Package registry provides functionality to manage and retrieve gitignore templates.
package registry
import (
@ -10,12 +11,14 @@ 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
}
func NewTemplateRegistry() *TemplateRegistry {
// 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

View File

@ -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
}
}