From e8097aea196395e84c9cd3345bda3278a56bcb4f Mon Sep 17 00:00:00 2001 From: Hau Nguyen Date: Sun, 27 Nov 2022 01:07:15 +0700 Subject: [PATCH] feat: print diff --- go.mod | 1 + go.sum | 2 ++ internal/imports/formatter.go | 34 ++++++++++++++++++++++++---------- 3 files changed, 27 insertions(+), 10 deletions(-) diff --git a/go.mod b/go.mod index 8b513d4..c6acc46 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.19 require ( 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 golang.org/x/mod v0.7.0 golang.org/x/tools v0.3.0 diff --git a/go.sum b/go.sum index 7941c86..68014f8 100644 --- a/go.sum +++ b/go.sum @@ -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.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y= 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/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/urfave/cli/v2 v2.23.5 h1:xbrU7tAYviSpqeR3X4nEFWUdB/uDZ6DE+HxmRU7Xtyw= diff --git a/internal/imports/formatter.go b/internal/imports/formatter.go index d224059..b16b6e1 100644 --- a/internal/imports/formatter.go +++ b/internal/imports/formatter.go @@ -13,6 +13,7 @@ import ( "strings" "sync" + "github.com/pkg/diff" "golang.org/x/mod/modfile" "golang.org/x/tools/go/packages" ) @@ -133,10 +134,25 @@ func (ft *Formatter) formatFile(path string) error { } 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 } + 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.formattedPaths[path] = struct{}{} ft.muFormattedPaths.Unlock() @@ -149,7 +165,7 @@ func (ft *Formatter) formatImports( path string, pathBytes []byte, moduleName string, -) error { +) ([]byte, error) { // Parse ast fset := token.NewFileSet() @@ -158,12 +174,12 @@ func (ft *Formatter) formatImports( astFile, err := parser.ParseFile(fset, path, pathBytes, parserMode) 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 if isGoGenerated(astFile) { - return ErrGoGeneratedFile + return nil, ErrGoGeneratedFile } // Extract imports @@ -175,7 +191,7 @@ func (ft *Formatter) formatImports( groupedImportSpecs, err := ft.groupImportSpecs(importSpecs, moduleName) if err != nil { - return err + return nil, err } formattedImportSpecs, err := ft.formatImportSpecs( @@ -183,7 +199,7 @@ func (ft *Formatter) formatImports( groupedImportSpecs, ) if err != nil { - return err + return nil, err } ft.mustLogImportSpecs("formatImports: formattedImportSpecs: ", formattedImportSpecs) @@ -220,12 +236,10 @@ func (ft *Formatter) formatImports( var formattedBytes []byte formattedBuffer := bytes.NewBuffer(formattedBytes) if err := printer.Fprint(formattedBuffer, fset, astFile); err != nil { - return err + return nil, err } - fmt.Println(formattedBuffer.String()) - - return nil + return formattedBuffer.Bytes(), nil } // Copy from goimports-reviser