github.com/duskeagle/pop@v4.10.1-0.20190417200916-92f2b794aab5+incompatible/soda/cmd/fix.go (about)

     1  package cmd
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"path/filepath"
     7  	"strings"
     8  
     9  	"github.com/gobuffalo/pop/fix"
    10  	"github.com/pkg/errors"
    11  	"github.com/spf13/cobra"
    12  )
    13  
    14  var fixCmd = &cobra.Command{
    15  	Use:     "fix",
    16  	Aliases: []string{"f", "update"},
    17  	Short:   "Brings pop, soda, and fizz files in line with the latest APIs",
    18  	RunE: func(cmd *cobra.Command, args []string) error {
    19  		return filepath.Walk(migrationPath, func(path string, info os.FileInfo, _ error) error {
    20  			if info == nil {
    21  				return nil
    22  			}
    23  			ext := strings.ToLower(filepath.Ext(path))
    24  			if ext != ".fizz" {
    25  				return nil
    26  			}
    27  
    28  			b, err := ioutil.ReadFile(path)
    29  			if err != nil {
    30  				return errors.WithStack(err)
    31  			}
    32  
    33  			content := string(b)
    34  
    35  			fixed, err := fix.Anko(content)
    36  			if err != nil {
    37  				return errors.WithStack(err)
    38  			}
    39  			if strings.TrimSpace(fixed) != strings.TrimSpace(content) {
    40  				f, err := os.Create(path)
    41  				if err != nil {
    42  					return errors.WithStack(err)
    43  				}
    44  				if _, err := f.WriteString(fixed); err != nil {
    45  					return errors.WithStack(err)
    46  				}
    47  				if err := f.Close(); err != nil {
    48  					return errors.WithStack(err)
    49  				}
    50  			}
    51  
    52  			return nil
    53  		})
    54  	},
    55  }
    56  
    57  func init() {
    58  	RootCmd.AddCommand(fixCmd)
    59  }