mirror of
https://github.com/onyx-and-iris/gignore.git
synced 2025-04-01 19:23:51 +01:00
Compare commits
8 Commits
7539d8ca76
...
f0b64e3a75
Author | SHA1 | Date | |
---|---|---|---|
f0b64e3a75 | |||
32714f55d2 | |||
8e53c4fbeb | |||
6fc81a4c96 | |||
820ed2055b | |||
eba8dd0113 | |||
f262277aca | |||
c11b94ec0d |
29
.github/workflows/golang-ci.yml
vendored
Normal file
29
.github/workflows/golang-ci.yml
vendored
Normal 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
30
.github/workflows/update-go-modules.yml
vendored
Normal 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
1
.gitignore
vendored
@ -9,6 +9,7 @@
|
||||
*.dll
|
||||
*.so
|
||||
*.dylib
|
||||
bin/
|
||||
|
||||
# Test binary, built with `go test -c`
|
||||
*.test
|
||||
|
54
.golangci.yml
Normal file
54
.golangci.yml
Normal 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
|
@ -1,3 +1,8 @@
|
||||

|
||||

|
||||

|
||||
|
||||
|
||||
# Gignore - Generate .gitinore files
|
||||
|
||||
## Install
|
||||
|
@ -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()
|
||||
|
||||
|
@ -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
|
||||
}
|
||||
|
1
error.go
1
error.go
@ -1,3 +1,4 @@
|
||||
// Package gignore provides functionality for handling template errors and registry operations.
|
||||
package gignore
|
||||
|
||||
import (
|
||||
|
46
gignore.go
46
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{
|
||||
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
|
||||
}
|
||||
|
@ -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,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
|
||||
|
||||
|
@ -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