github.com/wawandco/ox@v0.13.6-0.20230809142027-913b3d837f2a/plugins/tools/ox/replaceimportsfixer.go (about)

     1  package ox
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"os"
     7  	"path/filepath"
     8  )
     9  
    10  // ReplaceImportsFixer
    11  type ReplaceImportsFixer struct{}
    12  
    13  func (ef ReplaceImportsFixer) Name() string {
    14  	return "/replaceimports"
    15  }
    16  
    17  func (ef ReplaceImportsFixer) Fix(ctx context.Context, root string, args []string) error {
    18  	err := filepath.Walk(root, func(path string, info os.FileInfo, _ error) error {
    19  		if info.IsDir() || filepath.Ext(info.Name()) != ".go" {
    20  			return nil
    21  		}
    22  
    23  		cc, err := os.ReadFile(path)
    24  		if err != nil {
    25  			return err
    26  		}
    27  
    28  		cc = bytes.ReplaceAll(cc, []byte("github.com/gobuffalo/pop/v5"), []byte("github.com/gobuffalo/pop/v6"))
    29  		cc = bytes.ReplaceAll(cc, []byte("github.com/gobuffalo/suite/v3"), []byte("github.com/gobuffalo/suite/v4"))
    30  
    31  		err = os.WriteFile(path, []byte(cc), 0644)
    32  		if err != nil {
    33  			return err
    34  		}
    35  
    36  		return nil
    37  	})
    38  
    39  	return err
    40  }