github.com/gobuffalo/pop/v6@v6.1.2-0.20230426125638-01ebd5b92a24/fix/fix.go (about)

     1  package fix
     2  
     3  import (
     4  	"io"
     5  	"io/ioutil"
     6  	"strings"
     7  )
     8  
     9  // Fizz fixes a fizz file to use the most up to date format.
    10  // It takes the original contents from the Reader, and writes the fixed contents in the Writer.
    11  func Fizz(r io.Reader, w io.Writer) error {
    12  	b, err := ioutil.ReadAll(r)
    13  	if err != nil {
    14  		return err
    15  	}
    16  
    17  	content := string(b)
    18  
    19  	// Old anko format
    20  	fixed, err := Anko(content)
    21  	if err != nil {
    22  		return err
    23  	}
    24  	if strings.TrimSpace(fixed) != strings.TrimSpace(content) {
    25  		content = fixed
    26  	}
    27  
    28  	// Rewrite migrations to use t.Timestamps() if necessary
    29  	fixed, err = AutoTimestampsOff(content)
    30  	if err != nil {
    31  		return err
    32  	}
    33  
    34  	if strings.TrimSpace(fixed) != strings.TrimSpace(content) {
    35  		if _, err := w.Write([]byte(fixed)); err != nil {
    36  			return err
    37  		}
    38  	}
    39  	return nil
    40  }