Browse Source

add lint

pull/1/head
andrey1s 2 years ago
parent
commit
fa3b06e51b
  1. 42
      .drone.yml
  2. 43
      .golangci.yml
  3. 1
      mime.go
  4. 2
      scripts/main.go
  5. 107
      scripts/mime/generate.go

42
.drone.yml

@ -0,0 +1,42 @@
kind: pipeline
name: default
steps:
- name: golangci-lint
image: golangci/golangci-lint:v1.49
volumes:
- name: deps
path: /go/src/mod
commands:
- golangci-lint run --timeout 5m
- name: test
image: golang
volumes:
- name: deps
path: /go/src/mod
commands:
- go test ./...
- name: example golangci-lint
image: golangci/golangci-lint:v1.49
volumes:
- name: deps
path: /go/src/mod
commands:
- cd scripts
- golangci-lint run --timeout 5m
- name: example test
image: golang
volumes:
- name: deps
path: /go/src/mod
commands:
- cd scripts
- go test ./...
volumes:
- name: deps
temp: {}

43
.golangci.yml

@ -0,0 +1,43 @@
linters-settings:
dupl:
threshold: 100
funlen:
lines: 100
statements: 50
goconst:
min-len: 2
min-occurrences: 2
gocyclo:
min-complexity: 15
golint:
min-confidence: 0
govet:
check-shadowing: true
lll:
line-length: 140
maligned:
suggest-new: true
misspell:
locale: US
varnamelen:
min-name-length: 2
linters:
enable-all: true
disable:
- varcheck
- maligned
- scopelint
- nosnakecase
- ifshort
- golint
- interfacer
- structcheck
- deadcode
- exhaustivestruct
- exhaustruct
# is disabled because of generics. You can track the evolution of the generics support by following the https://github.com/golangci/golangci-lint/issues/2649
- rowserrcheck
- sqlclosecheck
- wastedassign

1
mime.go

@ -13,6 +13,7 @@ func (v Mime) Is(types ...Mime) bool {
return true return true
} }
} }
return false return false
} }

2
scripts/main.go

@ -11,8 +11,10 @@ import (
func main() { func main() {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
closer.AddLast(func() error { closer.AddLast(func() error {
cancel() cancel()
return nil return nil
}) })

107
scripts/mime/generate.go

@ -1,9 +1,9 @@
package mime package mime
import ( import (
"errors"
"fmt" "fmt"
"go/format" "go/format"
"io/ioutil"
"os" "os"
"path" "path"
"text/template" "text/template"
@ -11,6 +11,16 @@ import (
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
) )
var (
ErrDuplicate = errors.New("duplicate")
ErrNotFound = errors.New("not found")
)
const (
NameExt = "ext"
NameMime = "mime"
)
type Config struct { type Config struct {
Source string Source string
@ -33,6 +43,28 @@ type Data struct {
Value []string `yaml:"value,omitempty"` Value []string `yaml:"value,omitempty"`
} }
func (c *Config) Base(name string) string {
switch name {
case NameExt:
return path.Base(c.ExtTpl)
case NameMime:
return path.Base(c.MimeTpl)
}
return ""
}
func (c *Config) Result(name string) string {
switch name {
case NameExt:
return c.ExtResult
case NameMime:
return c.MimeResult
}
return ""
}
func (c *Config) ValidateAndFillExt() error { func (c *Config) ValidateAndFillExt() error {
mimes := make(map[string]int) mimes := make(map[string]int)
mimeIDs := make(map[int]string) mimeIDs := make(map[int]string)
@ -40,7 +72,7 @@ func (c *Config) ValidateAndFillExt() error {
for idx, ext := range c.Extensions { for idx, ext := range c.Extensions {
if _, ok := extIndex[ext.Name]; ok { if _, ok := extIndex[ext.Name]; ok {
return fmt.Errorf("extension %v duplicate: with id %v", ext.Name, c.Extensions[extIndex[ext.Name]].ID) return fmt.Errorf("extension %v %w: with id %v", ext.Name, ErrDuplicate, c.Extensions[extIndex[ext.Name]].ID)
} }
extIndex[ext.Name] = idx extIndex[ext.Name] = idx
@ -48,22 +80,21 @@ func (c *Config) ValidateAndFillExt() error {
for _, mime := range c.Mimes { for _, mime := range c.Mimes {
if _, ok := mimes[mime.Name]; ok { if _, ok := mimes[mime.Name]; ok {
return fmt.Errorf("mime %v duplicate: with id %v", mime.Name, mimes[mime.Name]) return fmt.Errorf("mime %v %w: with id %v", mime.Name, ErrDuplicate, mimes[mime.Name])
} }
if _, ok := mimeIDs[mime.ID]; ok { if _, ok := mimeIDs[mime.ID]; ok {
return fmt.Errorf("ID %v duplicate: with name %v", mime.ID, mimeIDs[mime.ID]) return fmt.Errorf("ID %v %w: with name %v", mime.ID, ErrDuplicate, mimeIDs[mime.ID])
} }
for _, ext := range mime.Value { for _, ext := range mime.Value {
idx, ok := extIndex[ext] idx, ok := extIndex[ext]
if !ok { if !ok {
return fmt.Errorf("not fount ext by %v", mime.Name) return fmt.Errorf("%w ext by %v", ErrNotFound, mime.Name)
} }
c.Extensions[idx].Value = append(c.Extensions[idx].Value, mime.Name) c.Extensions[idx].Value = append(c.Extensions[idx].Value, mime.Name)
} }
} }
return nil return nil
@ -133,17 +164,17 @@ func Generate(fileName string, opts ...Option) error {
opt(&cfg) opt(&cfg)
} }
data, err := os.ReadFile(fileName) data, cerr := os.ReadFile(fileName)
if err != nil { if cerr != nil {
return fmt.Errorf("read file:%w", err) return fmt.Errorf("read file:%w", cerr)
} }
if err := yaml.Unmarshal(data, &cfg); err != nil { if uerr := yaml.Unmarshal(data, &cfg); uerr != nil {
return fmt.Errorf("unmarshal:%w", err) return fmt.Errorf("unmarshal:%w", uerr)
} }
if err := cfg.ValidateAndFillExt(); err != nil { if validErr := cfg.ValidateAndFillExt(); validErr != nil {
return fmt.Errorf("config:%w", err) return fmt.Errorf("config:%w", validErr)
} }
template, err := template.New("mimes").Funcs(funcMap()).ParseFiles(cfg.ExtTpl, cfg.MimeTpl) template, err := template.New("mimes").Funcs(funcMap()).ParseFiles(cfg.ExtTpl, cfg.MimeTpl)
@ -151,30 +182,29 @@ func Generate(fileName string, opts ...Option) error {
return fmt.Errorf("ext template:%w", err) return fmt.Errorf("ext template:%w", err)
} }
extFile, err := os.Create(cfg.ExtResult) if exErr := Create(template, NameExt, cfg); exErr != nil {
if err != nil { return fmt.Errorf("ext create:%w", exErr)
return fmt.Errorf("ext file:%w", err)
} }
if err := template.ExecuteTemplate(extFile, path.Base(cfg.ExtTpl), cfg); err != nil { if mimeErr := Create(template, NameMime, cfg); mimeErr != nil {
return fmt.Errorf("ext execute:%w", err) return fmt.Errorf("mime create:%w", mimeErr)
} }
if err := Format(extFile.Name()); err != nil { return nil
return fmt.Errorf("format ext:%w", err) }
}
mimeFile, err := os.Create(cfg.MimeResult) func Create(tpl *template.Template, name string, cfg Config) error {
extFile, err := os.Create(cfg.Result(name))
if err != nil { if err != nil {
return fmt.Errorf("mime file:%w", err) return fmt.Errorf("ext file:%w", err)
} }
if err := template.ExecuteTemplate(mimeFile, path.Base(cfg.MimeTpl), cfg); err != nil { if exErr := tpl.ExecuteTemplate(extFile, cfg.Base(name), cfg); exErr != nil {
return fmt.Errorf("mime execute:%w", err) return fmt.Errorf("ext execute:%w", exErr)
} }
if err := Format(mimeFile.Name()); err != nil { if formatErr := Format(extFile.Name()); formatErr != nil {
return fmt.Errorf("format mime:%w", err) return fmt.Errorf("format ext:%w", formatErr)
} }
return nil return nil
@ -182,13 +212,14 @@ func Generate(fileName string, opts ...Option) error {
// Format file and write it. // Format file and write it.
func Format(name string) error { func Format(name string) error {
in, err := ioutil.ReadFile(name) in, err := os.ReadFile(name)
if err != nil { if err != nil {
return err return fmt.Errorf("read file:%w", err)
} }
out, err := format.Source(in) out, err := format.Source(in)
if err != nil { if err != nil {
return err return fmt.Errorf("format source:%w", err)
} }
file, err := os.Create(name) file, err := os.Create(name)
@ -202,19 +233,3 @@ func Format(name string) error {
return nil return nil
} }
func extensions(mimes map[string][]string) map[string][]string {
out := make(map[string][]string)
for mime, exts := range mimes {
for _, ext := range exts {
if _, ok := out[ext]; ok {
out[ext] = append(out[ext], mime)
continue
}
out[ext] = []string{mime}
}
}
return out
}

Loading…
Cancel
Save