add lint
This commit is contained in:
@@ -11,8 +11,10 @@ import (
|
||||
|
||||
func main() {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
|
||||
closer.AddLast(func() error {
|
||||
cancel()
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
package mime
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"go/format"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
"text/template"
|
||||
@@ -11,6 +11,16 @@ import (
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrDuplicate = errors.New("duplicate")
|
||||
ErrNotFound = errors.New("not found")
|
||||
)
|
||||
|
||||
const (
|
||||
NameExt = "ext"
|
||||
NameMime = "mime"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Source string
|
||||
|
||||
@@ -33,6 +43,28 @@ type Data struct {
|
||||
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 {
|
||||
mimes := make(map[string]int)
|
||||
mimeIDs := make(map[int]string)
|
||||
@@ -40,7 +72,7 @@ func (c *Config) ValidateAndFillExt() error {
|
||||
|
||||
for idx, ext := range c.Extensions {
|
||||
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
|
||||
@@ -48,22 +80,21 @@ func (c *Config) ValidateAndFillExt() error {
|
||||
|
||||
for _, mime := range c.Mimes {
|
||||
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 {
|
||||
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 {
|
||||
idx, ok := extIndex[ext]
|
||||
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)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return nil
|
||||
@@ -133,17 +164,17 @@ func Generate(fileName string, opts ...Option) error {
|
||||
opt(&cfg)
|
||||
}
|
||||
|
||||
data, err := os.ReadFile(fileName)
|
||||
if err != nil {
|
||||
return fmt.Errorf("read file:%w", err)
|
||||
data, cerr := os.ReadFile(fileName)
|
||||
if cerr != nil {
|
||||
return fmt.Errorf("read file:%w", cerr)
|
||||
}
|
||||
|
||||
if err := yaml.Unmarshal(data, &cfg); err != nil {
|
||||
return fmt.Errorf("unmarshal:%w", err)
|
||||
if uerr := yaml.Unmarshal(data, &cfg); uerr != nil {
|
||||
return fmt.Errorf("unmarshal:%w", uerr)
|
||||
}
|
||||
|
||||
if err := cfg.ValidateAndFillExt(); err != nil {
|
||||
return fmt.Errorf("config:%w", err)
|
||||
if validErr := cfg.ValidateAndFillExt(); validErr != nil {
|
||||
return fmt.Errorf("config:%w", validErr)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
extFile, err := os.Create(cfg.ExtResult)
|
||||
if exErr := Create(template, NameExt, cfg); exErr != nil {
|
||||
return fmt.Errorf("ext create:%w", exErr)
|
||||
}
|
||||
|
||||
if mimeErr := Create(template, NameMime, cfg); mimeErr != nil {
|
||||
return fmt.Errorf("mime create:%w", mimeErr)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func Create(tpl *template.Template, name string, cfg Config) error {
|
||||
extFile, err := os.Create(cfg.Result(name))
|
||||
if err != nil {
|
||||
return fmt.Errorf("ext file:%w", err)
|
||||
}
|
||||
|
||||
if err := template.ExecuteTemplate(extFile, path.Base(cfg.ExtTpl), cfg); err != nil {
|
||||
return fmt.Errorf("ext execute:%w", err)
|
||||
if exErr := tpl.ExecuteTemplate(extFile, cfg.Base(name), cfg); exErr != nil {
|
||||
return fmt.Errorf("ext execute:%w", exErr)
|
||||
}
|
||||
|
||||
if err := Format(extFile.Name()); err != nil {
|
||||
return fmt.Errorf("format ext:%w", err)
|
||||
}
|
||||
|
||||
mimeFile, err := os.Create(cfg.MimeResult)
|
||||
if err != nil {
|
||||
return fmt.Errorf("mime file:%w", err)
|
||||
}
|
||||
|
||||
if err := template.ExecuteTemplate(mimeFile, path.Base(cfg.MimeTpl), cfg); err != nil {
|
||||
return fmt.Errorf("mime execute:%w", err)
|
||||
}
|
||||
|
||||
if err := Format(mimeFile.Name()); err != nil {
|
||||
return fmt.Errorf("format mime:%w", err)
|
||||
if formatErr := Format(extFile.Name()); formatErr != nil {
|
||||
return fmt.Errorf("format ext:%w", formatErr)
|
||||
}
|
||||
|
||||
return nil
|
||||
@@ -182,13 +212,14 @@ func Generate(fileName string, opts ...Option) error {
|
||||
|
||||
// Format file and write it.
|
||||
func Format(name string) error {
|
||||
in, err := ioutil.ReadFile(name)
|
||||
in, err := os.ReadFile(name)
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("read file:%w", err)
|
||||
}
|
||||
|
||||
out, err := format.Source(in)
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("format source:%w", err)
|
||||
}
|
||||
|
||||
file, err := os.Create(name)
|
||||
@@ -202,19 +233,3 @@ func Format(name string) error {
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user