Compare commits

..

17 Commits
v0.2.0 ... main

Author SHA1 Message Date
github-actions[bot]
2c1d2ed99c chore: auto-update Go modules 2025-04-07 00:05:05 +00:00
29270a2c14 fix loglevel example in readme
Some checks failed
CI / Lint (push) Has been cancelled
Auto-Update Go Modules / update-go-modules (push) Has been cancelled
2025-04-05 22:24:49 +01:00
ee86db76a2 fix loglevel example in readme 2025-04-05 22:19:37 +01:00
6aac14b9ed -loglevel now string flag
upd README, CHANGELOG
2025-04-05 22:18:28 +01:00
38b0611e4e update error/logging messages
Some checks failed
CI / Lint (push) Has been cancelled
2025-03-31 22:05:22 +01:00
6b41418c00 split long desc across lines
Some checks failed
Auto-Update Go Modules / update-go-modules (push) Has been cancelled
2025-03-17 22:52:12 +00:00
github-actions[bot]
4800b29707 chore: auto-update Go modules 2025-03-17 00:05:32 +00:00
8a9539ea60 Taskfile skip the generate task if templates already exist
Some checks failed
Auto-Update Go Modules / update-go-modules (push) Has been cancelled
make note of --force flag.
2025-03-16 02:14:25 +00:00
626e40b653 add 0.2.0 and 0.3.0 to CHANGELOG
Some checks failed
CI / Lint (push) Has been cancelled
2025-03-14 21:58:57 +00:00
fd9c7194c1 CLI may now accept multiple template names 2025-03-14 21:53:06 +00:00
388a204299 open file in append mode 2025-03-14 21:49:55 +00:00
a3c2d2cfbf test against empty string
Some checks are pending
CI / Lint (push) Waiting to run
2025-03-13 18:22:52 +00:00
7ada4e9328 remove the done channel
Some checks are pending
CI / Lint (push) Waiting to run
add prune task to Taskfile
2025-03-12 21:03:52 +00:00
eaf091dc1a upd links
Some checks are pending
CI / Lint (push) Waiting to run
2025-03-12 15:48:43 +00:00
99871a9040 fix links in special thanks 2025-03-12 15:46:56 +00:00
1ca4304806 add Install with Go tools
remove release task from Taskfile
2025-03-12 15:15:42 +00:00
59521e1cd0 rename gen.go to main.go
no changes to file contents
2025-03-12 15:15:08 +00:00
11 changed files with 113 additions and 49 deletions

View File

@ -5,6 +5,29 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
# [0.4.0] - 2025-04-05
### Changed
- `-loglevel` flag is now of type string. It accepts any one of trace, debug, info, warn, error, fatal or panic.
- It defaults to warn.
# [0.3.0] - 2025-14-03
### Added
- CLI may now accept multiple template names, example `gignore go python`. One will be appended after the other.
### Changed
- Filewriter now opens file in append mode.
# [0.2.0] - 2025-10-03
### Fixed
- Template .gitignore are now written concurrently.
# [0.1.0] - 2025-09-03
### Added

View File

@ -7,6 +7,13 @@
## Install
With Go tools:
```bash
go generate ./...
go install ./cmd/gignore
```
With [Task][task]:
```bash
@ -37,7 +44,7 @@ Example:
## Custom Templates
It's possible to add your own custom templates, simply create a directory in `internal/registry/templates`. You'll need to rebuild the project before you can load the new templates.
It's possible to add your own custom templates, simply create a directory in `internal/registry/templates`. You'll need to [reinstall](https://github.com/onyx-and-iris/gignore?tab=readme-ov-file#install) the project before you can load the new templates.
Then pass the dir name as a flag, for example:
@ -49,15 +56,37 @@ You may set an environment variable `GIGNORE_TEMPLATE_DIR` to avoid passing the
If a template is requested but not found in the custom directory then the gitignoreio registry will act as a fallback.
## Logging
The `-loglevel` flag allows you to control the verbosity of the application's logging output.
Acceptable values for this flag are:
- `trace`
- `debug`
- `info`
- `warn`
- `error`
- `fatal`
- `panic`
For example, to set the log level to `debug`, you can use:
```bash
gignore -loglevel=debug -dir=custom go
```
The default log level is `warn` if the flag is not specified.
## Special Thanks
[gitignore.io][gitignoreio] For providing such a useful .gitignore service
[mh-cbon][mh-cbon] For writing the [gigo][gigo] client library for gitignore.io
[cuonglm][cuonglm] For writing the [gogi][gogi] client library for gitignore.io
[task]: https://taskfile.dev/
[gitignoreio]: https://www.toptal.com/developers/gitignore
[mh-cbon]: https://github.com/mh-cbon
[gigo]: https://github.com/mh-cbon/gigo
[cuonglm]: https://github.com/cuonglm
[gogi]: https://github.com/cuonglm/gogi
[ignore]: https://github.com/neptship/ignore

View File

@ -23,13 +23,6 @@ tasks:
- task: build-windows
- task: build-linux
release:
desc: Generate the gitignore.io templates and then build the gignore project for Windows and Linux
deps: [generate]
cmds:
- task: build-windows
- task: build-linux
vet:
desc: Vet the code
deps: [fmt]
@ -42,19 +35,26 @@ tasks:
- go fmt ./...
generate:
desc: Generate the gitignore.io templates
desc: |
Generate the gitignore.io templates.
This task will be skipped if the templates already exist.
You may use the `--force` flag to regenerate the templates.
cmds:
- go generate ./...
status:
- ls internal/registry/templates/gitignoreio/*.gitignore >/dev/null || exit 1
build-windows:
desc: Build the gignore project for Windows
cmds:
- GOOS=windows GOARCH=amd64 go build -o {{.WINDOWS}} -ldflags="-X main.Version={{.GIT_COMMIT}}" ./cmd/{{.PROGRAM}}
internal: true
build-linux:
desc: Build the gignore project for Linux
cmds:
- GOOS=linux GOARCH=amd64 go build -o {{.LINUX}} -ldflags="-X main.Version={{.GIT_COMMIT}}" ./cmd/{{.PROGRAM}}
internal: true
test:
desc: Run tests
@ -71,3 +71,8 @@ tasks:
desc: Clean the build artifacts
cmds:
- '{{.SHELL}} rm -r {{.BIN_DIR}}'
prune:
desc: Empty the gitignoreio template registry
cmds:
- '{{.SHELL}} rm internal/registry/templates/gitignoreio/*.gitignore'

View File

@ -21,7 +21,6 @@ func main() {
}
errChan := make(chan error)
doneChan := make(chan struct{})
for _, template := range templates {
go func() {
@ -30,15 +29,13 @@ func main() {
errChan <- fmt.Errorf("Failed to create template %s: %v", template.Name, err)
return
}
doneChan <- struct{}{}
errChan <- nil
}()
}
for range templates {
select {
case err := <-errChan:
if err := <-errChan; err != nil {
log.Error(err)
case <-doneChan:
}
}
}

View File

@ -5,32 +5,37 @@ package main
import (
"flag"
"fmt"
"slices"
"os"
"github.com/onyx-and-iris/gignore"
log "github.com/sirupsen/logrus"
)
func exit(err error) {
_, _ = fmt.Fprintf(os.Stderr, "Error: %s\n", err)
os.Exit(1)
}
func main() {
flag.Usage = func() {
w := flag.CommandLine.Output()
fmt.Fprint(w, "Usage of gignore:\n")
fmt.Fprintf(w, " gignore [flags] <template>\n")
fmt.Fprint(w, " gignore [flags] <template>\n")
fmt.Fprint(w, "\n")
fmt.Fprint(w, "Flags:\n")
flag.PrintDefaults()
fmt.Fprint(w, "\n")
fmt.Fprintf(w, "Example:\n")
fmt.Fprint(w, "Example:\n")
fmt.Fprint(w, " gignore go\n")
}
var (
list bool
templateDir string
loglevel int
loglevel string
)
flag.BoolVar(&list, "list", false, "list available templates")
@ -41,35 +46,39 @@ func main() {
getEnv("GIGNORE_TEMPLATE_DIR", "gitignoreio"),
"directory containing .gitignore templates",
)
flag.IntVar(&loglevel, "loglevel", int(log.WarnLevel), "log level")
flag.IntVar(&loglevel, "l", int(log.WarnLevel), "log level (shorthand)")
flag.StringVar(&loglevel, "loglevel", "warn", "log level")
flag.StringVar(&loglevel, "l", "warn", "log level (shorthand)")
flag.Parse()
if slices.Contains(log.AllLevels, log.Level(loglevel)) {
log.SetLevel(log.Level(loglevel))
level, err := log.ParseLevel(loglevel)
if err != nil {
exit(fmt.Errorf("invalid log level: %s", loglevel))
}
log.SetLevel(level)
client := gignore.New(gignore.WithTemplateDirectory(templateDir))
if list {
if err := listTemplates(client); err != nil {
log.Fatalf("failed to list templates: %v", err)
exit(fmt.Errorf("failed to list templates: %v", err))
}
return
}
args := flag.Args()
if len(args) != 1 {
if len(args) == 0 {
flag.Usage()
return
}
err := client.Create(args[0])
if err != nil {
log.Fatalf("failed to create .gitignore file: %v", err)
for _, arg := range args {
err := client.Create(arg)
if err != nil {
exit(fmt.Errorf("failed to create .gitignore file: %v", err))
}
fmt.Printf("√ created %s .gitignore file\n", arg)
}
fmt.Printf("√ created %s .gitignore file\n", args[0])
}
func listTemplates(client *gignore.Client) error {

View File

@ -4,7 +4,7 @@ import "os"
func getEnv(key, defaultValue string) string {
value := os.Getenv(key)
if len(value) == 0 {
if value == "" {
return defaultValue
}
return value

View File

@ -1,17 +1,16 @@
// Package gignore provides functionality for handling template errors and registry operations.
// Package gignore provides a way to manage .gitignore files and templates.
package gignore
import (
"fmt"
"github.com/onyx-and-iris/gignore/internal/registry"
"strings"
)
type templateNotFoundError struct {
template string
registry *registry.TemplateRegistry
template string
templatesSearched []string
}
func (e *templateNotFoundError) Error() string {
return fmt.Sprintf("template '%s' not found in %s registry", e.template, e.registry.Directory)
return fmt.Sprintf("template '%s' not found in %s registry", e.template, strings.Join(e.templatesSearched, ", "))
}

View File

@ -8,7 +8,7 @@ import (
log "github.com/sirupsen/logrus"
)
//go:generate go run cmd/gen/gen.go
//go:generate go run cmd/gen/main.go
// Client is a client for managing .gitignore templates.
type Client struct {
@ -42,22 +42,23 @@ func (c *Client) Create(template string) error {
return err
}
if !ok {
templateNotFoundErr := &templateNotFoundError{template, c.registry}
templateNotFoundErr := &templateNotFoundError{template, []string{c.registry.Directory}}
if c.registry.Directory == "gitignoreio" {
return templateNotFoundErr
}
log.Errorf("%s. Checking default registry...", templateNotFoundErr)
c.registry.Directory = "gitignoreio"
ok, err = c.registry.Contains(template)
if err != nil {
return err
}
if !ok {
templateNotFoundErr.templatesSearched = append(templateNotFoundErr.templatesSearched, c.registry.Directory)
return templateNotFoundErr
}
log.Infof("template '%s' found in default gitignoreio registry", template)
log.Debugf("template '%s' found in gitignoreio registry", template)
} else {
log.Debugf("template '%s' found in %s registry", template, c.registry.Directory)
}
content, err := c.registry.Get(template)

2
go.mod
View File

@ -7,4 +7,4 @@ require (
github.com/sirupsen/logrus v1.9.3
)
require golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
require golang.org/x/sys v0.32.0 // indirect

3
go.sum
View File

@ -10,8 +10,9 @@ github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVs
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.32.0 h1:s77OFDvIQeibCmezSnk/q6iAfkdiQaJi4VzroCFrN20=
golang.org/x/sys v0.32.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View File

@ -29,14 +29,14 @@ func (fw *FileWriter) writeContent(content []byte, dst io.Writer) (int64, error)
}
func (fw *FileWriter) Write(content []byte) (int, error) {
f, err := os.Create(fw.targetFileName)
f, err := os.OpenFile(fw.targetFileName, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o600)
if err != nil {
return 0, err
}
defer f.Close()
const header = `# Auto-generated .gitignore by gignore: github.com/onyx-and-iris/gignore`
const footer = `# End of gignore: github.com/onyx-and-iris/gignore`
const header = "# Auto-generated .gitignore by gignore: github.com/onyx-and-iris/gignore\n"
const footer = "\n# End of gignore: github.com/onyx-and-iris/gignore\n"
var sz int64
n, err := fw.writeContent([]byte(header), f)