mirror of
https://github.com/onyx-and-iris/gignore.git
synced 2025-04-01 19:23:51 +01:00
Some checks are pending
CI / Lint (push) Waiting to run
add prune task to Taskfile
67 lines
1.2 KiB
Go
67 lines
1.2 KiB
Go
// Package main generates gitignore.io templates using the gogi library.
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
|
|
"github.com/cuonglm/gogi"
|
|
log "github.com/sirupsen/logrus"
|
|
)
|
|
|
|
func main() {
|
|
log.Info("Generating gitignore.io templates...")
|
|
|
|
gogiClient, _ := gogi.NewHTTPClient()
|
|
|
|
templates, err := fetchTemplates(gogiClient)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
errChan := make(chan error)
|
|
|
|
for _, template := range templates {
|
|
go func() {
|
|
err := createTemplate(template)
|
|
if err != nil {
|
|
errChan <- fmt.Errorf("Failed to create template %s: %v", template.Name, err)
|
|
return
|
|
}
|
|
errChan <- nil
|
|
}()
|
|
}
|
|
|
|
for range templates {
|
|
if err := <-errChan; err != nil {
|
|
log.Error(err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func fetchTemplates(gogiClient *gogi.Client) (map[string]*gogi.ListJsonItem, error) {
|
|
data, err := gogiClient.ListJson()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return data, nil
|
|
}
|
|
|
|
func createTemplate(template *gogi.ListJsonItem) error {
|
|
file, err := os.Create(
|
|
fmt.Sprintf("internal/registry/templates/gitignoreio/%s.gitignore", strings.ToLower(template.Name)),
|
|
)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer file.Close()
|
|
|
|
_, err = file.Write([]byte(template.Contents))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|