fix: better errors and import spacing
ci/woodpecker/tag/goreleaser Pipeline was successful
Details
ci/woodpecker/tag/goreleaser Pipeline was successful
Details
Signed-off-by: jolheiser <john.olheiser@gmail.com>main v0.0.2
parent
81144edfe4
commit
37925d9220
|
@ -2,13 +2,14 @@ package format
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
importRe = regexp.MustCompile(`(?ms)import \(([^)]+)\)`)
|
importRe = regexp.MustCompile(`(?ms)import \(([^)]+)\)`)
|
||||||
otherRe = regexp.MustCompile(`(?:var|const|func)\s`)
|
otherRe = regexp.MustCompile(`^(?:var|const|func)\s`)
|
||||||
ErrNoImports = errors.New("no imports found")
|
ErrNoImports = errors.New("no imports found")
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -16,17 +17,17 @@ var (
|
||||||
func Source(src []byte, module string) ([]byte, error) {
|
func Source(src []byte, module string) ([]byte, error) {
|
||||||
importStart := importRe.FindIndex(src)
|
importStart := importRe.FindIndex(src)
|
||||||
if importStart == nil {
|
if importStart == nil {
|
||||||
return nil, ErrNoImports
|
return nil, fmt.Errorf("could not find imports: %w", ErrNoImports)
|
||||||
}
|
}
|
||||||
|
|
||||||
otherStart := otherRe.FindIndex(src)
|
otherStart := otherRe.FindIndex(src)
|
||||||
if otherStart != nil && otherStart[0] < importStart[0] {
|
if otherStart != nil && otherStart[0] < importStart[0] {
|
||||||
return nil, ErrNoImports
|
return nil, fmt.Errorf("found non-imports before imports: %w", ErrNoImports)
|
||||||
}
|
}
|
||||||
|
|
||||||
groups := importRe.FindStringSubmatch(string(src))
|
groups := importRe.FindStringSubmatch(string(src))
|
||||||
if groups[0] == "" {
|
if groups[0] == "" {
|
||||||
return nil, ErrNoImports
|
return nil, fmt.Errorf("could not find imports: %w", ErrNoImports)
|
||||||
}
|
}
|
||||||
|
|
||||||
imports := strings.Split(groups[1], "\n")
|
imports := strings.Split(groups[1], "\n")
|
||||||
|
|
|
@ -42,11 +42,11 @@ func (i importBlock) String() string {
|
||||||
var decl strings.Builder
|
var decl strings.Builder
|
||||||
|
|
||||||
decl.WriteString(strings.Join(importItemSlice(i.Stdlib), "\n\t"))
|
decl.WriteString(strings.Join(importItemSlice(i.Stdlib), "\n\t"))
|
||||||
if len(i.Stdlib) > 0 {
|
if len(i.Module) > 0 {
|
||||||
decl.WriteString("\n\n\t")
|
decl.WriteString("\n\n\t")
|
||||||
}
|
}
|
||||||
decl.WriteString(strings.Join(importItemSlice(i.Module), "\n\t"))
|
decl.WriteString(strings.Join(importItemSlice(i.Module), "\n\t"))
|
||||||
if len(i.Module) > 0 {
|
if len(i.Other) > 0 {
|
||||||
decl.WriteString("\n\n\t")
|
decl.WriteString("\n\n\t")
|
||||||
}
|
}
|
||||||
decl.WriteString(strings.Join(importItemSlice(i.Other), "\n\t"))
|
decl.WriteString(strings.Join(importItemSlice(i.Other), "\n\t"))
|
||||||
|
|
10
imp.go
10
imp.go
|
@ -3,6 +3,7 @@ package main
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
@ -45,15 +46,18 @@ func runImp(root, ignore string, write bool) error {
|
||||||
|
|
||||||
data, err := os.ReadFile(walkPath)
|
data, err := os.ReadFile(walkPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return fmt.Errorf("could not read file %q: %w", walkPath, err)
|
||||||
}
|
}
|
||||||
formatted, err := format.Source(data, module)
|
formatted, err := format.Source(data, module)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return fmt.Errorf("could not format file %q: %w", walkPath, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if write {
|
if write {
|
||||||
return os.WriteFile(walkPath, formatted, walkInfo.Mode())
|
if err := os.WriteFile(walkPath, formatted, walkInfo.Mode()); err != nil {
|
||||||
|
return fmt.Errorf("could not write file %q: %w", walkPath, err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if !bytes.Equal(data, formatted) {
|
if !bytes.Equal(data, formatted) {
|
||||||
|
|
Reference in New Issue