feat: print diff

main
sudo pacman -Syu 2022-11-27 01:07:15 +07:00
parent 2e435dd37b
commit e8097aea19
No known key found for this signature in database
GPG Key ID: D6CB5C6C567C47B0
3 changed files with 27 additions and 10 deletions

1
go.mod
View File

@ -4,6 +4,7 @@ go 1.19
require ( require (
github.com/make-go-great/color-go v0.4.1 github.com/make-go-great/color-go v0.4.1
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e
github.com/urfave/cli/v2 v2.23.5 github.com/urfave/cli/v2 v2.23.5
golang.org/x/mod v0.7.0 golang.org/x/mod v0.7.0
golang.org/x/tools v0.3.0 golang.org/x/tools v0.3.0

2
go.sum
View File

@ -9,6 +9,8 @@ github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y= github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e h1:aoZm08cpOy4WuID//EZDgcC4zIxODThtZNPirFr42+A=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/urfave/cli/v2 v2.23.5 h1:xbrU7tAYviSpqeR3X4nEFWUdB/uDZ6DE+HxmRU7Xtyw= github.com/urfave/cli/v2 v2.23.5 h1:xbrU7tAYviSpqeR3X4nEFWUdB/uDZ6DE+HxmRU7Xtyw=

View File

@ -13,6 +13,7 @@ import (
"strings" "strings"
"sync" "sync"
"github.com/pkg/diff"
"golang.org/x/mod/modfile" "golang.org/x/mod/modfile"
"golang.org/x/tools/go/packages" "golang.org/x/tools/go/packages"
) )
@ -133,10 +134,25 @@ func (ft *Formatter) formatFile(path string) error {
} }
ft.log("formatFile: moduleName: %+v\n", moduleName) ft.log("formatFile: moduleName: %+v\n", moduleName)
if err := ft.formatImports(path, pathBytes, moduleName); err != nil { formattedBytes, err := ft.formatImports(path, pathBytes, moduleName)
if err != nil {
return err return err
} }
if ft.isList {
fmt.Println(path)
}
if ft.isWrite {
ft.log("TODO: write to file\n")
}
if ft.isDiff {
if err := diff.Text("before", "after", pathBytes, formattedBytes, os.Stdout); err != nil {
return fmt.Errorf("diff: failed to slices: %w", err)
}
}
ft.muFormattedPaths.Lock() ft.muFormattedPaths.Lock()
ft.formattedPaths[path] = struct{}{} ft.formattedPaths[path] = struct{}{}
ft.muFormattedPaths.Unlock() ft.muFormattedPaths.Unlock()
@ -149,7 +165,7 @@ func (ft *Formatter) formatImports(
path string, path string,
pathBytes []byte, pathBytes []byte,
moduleName string, moduleName string,
) error { ) ([]byte, error) {
// Parse ast // Parse ast
fset := token.NewFileSet() fset := token.NewFileSet()
@ -158,12 +174,12 @@ func (ft *Formatter) formatImports(
astFile, err := parser.ParseFile(fset, path, pathBytes, parserMode) astFile, err := parser.ParseFile(fset, path, pathBytes, parserMode)
if err != nil { if err != nil {
return fmt.Errorf("parser: failed to parse file [%s]: %w", path, err) return nil, fmt.Errorf("parser: failed to parse file [%s]: %w", path, err)
} }
// Ignore generated file // Ignore generated file
if isGoGenerated(astFile) { if isGoGenerated(astFile) {
return ErrGoGeneratedFile return nil, ErrGoGeneratedFile
} }
// Extract imports // Extract imports
@ -175,7 +191,7 @@ func (ft *Formatter) formatImports(
groupedImportSpecs, err := ft.groupImportSpecs(importSpecs, moduleName) groupedImportSpecs, err := ft.groupImportSpecs(importSpecs, moduleName)
if err != nil { if err != nil {
return err return nil, err
} }
formattedImportSpecs, err := ft.formatImportSpecs( formattedImportSpecs, err := ft.formatImportSpecs(
@ -183,7 +199,7 @@ func (ft *Formatter) formatImports(
groupedImportSpecs, groupedImportSpecs,
) )
if err != nil { if err != nil {
return err return nil, err
} }
ft.mustLogImportSpecs("formatImports: formattedImportSpecs: ", formattedImportSpecs) ft.mustLogImportSpecs("formatImports: formattedImportSpecs: ", formattedImportSpecs)
@ -220,12 +236,10 @@ func (ft *Formatter) formatImports(
var formattedBytes []byte var formattedBytes []byte
formattedBuffer := bytes.NewBuffer(formattedBytes) formattedBuffer := bytes.NewBuffer(formattedBytes)
if err := printer.Fprint(formattedBuffer, fset, astFile); err != nil { if err := printer.Fprint(formattedBuffer, fset, astFile); err != nil {
return err return nil, err
} }
fmt.Println(formattedBuffer.String()) return formattedBuffer.Bytes(), nil
return nil
} }
// Copy from goimports-reviser // Copy from goimports-reviser