Skip to content

Commit

Permalink
Refactor Fizz fixer (#504)
Browse files Browse the repository at this point in the history
  • Loading branch information
stanislas-m committed Dec 14, 2019
1 parent 6a0aa05 commit bcfc830
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 36 deletions.
5 changes: 3 additions & 2 deletions fix/anko_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fix

import (
"io"
"io/ioutil"
"strings"
"testing"
Expand All @@ -23,7 +24,7 @@ func Test_Anko(t *testing.T) {
r.NoError(err)
}

func testPass(path string, info packr.File) func(*testing.T) {
func testPass(path string, info io.Reader) func(*testing.T) {
return func(t *testing.T) {
r := require.New(t)
b, err := ioutil.ReadAll(info)
Expand All @@ -40,7 +41,7 @@ func testPass(path string, info packr.File) func(*testing.T) {
}
}

func testFail(path string, info packr.File) func(*testing.T) {
func testFail(path string, info io.Reader) func(*testing.T) {
return func(t *testing.T) {
r := require.New(t)
b, err := ioutil.ReadAll(info)
Expand Down
40 changes: 40 additions & 0 deletions fix/fix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package fix

import (
"io"
"io/ioutil"
"strings"
)

// Fizz fixes a fizz file to use the most up to date format.
// It takes the original contents from the Reader, and writes the fixed contents in the Writer.
func Fizz(r io.Reader, w io.Writer) error {
b, err := ioutil.ReadAll(r)
if err != nil {
return err
}

content := string(b)

// Old anko format
fixed, err := Anko(content)
if err != nil {
return err
}
if strings.TrimSpace(fixed) != strings.TrimSpace(content) {
content = fixed
}

// Rewrite migrations to use t.Timestamps() if necessary
fixed, err = AutoTimestampsOff(content)
if err != nil {
return err
}

if strings.TrimSpace(fixed) != strings.TrimSpace(content) {
if _, err := w.Write([]byte(fixed)); err != nil {
return err
}
}
return nil
}
37 changes: 3 additions & 34 deletions soda/cmd/fix.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cmd

import (
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -29,42 +28,12 @@ func fixFizz(path string) error {
if ext != ".fizz" {
return nil
}

b, err := ioutil.ReadFile(path)
f, err := os.Open(path)
if err != nil {
return err
}

content := string(b)

// Old anko format
fixed, err := fix.Anko(content)
if err != nil {
return err
}
if strings.TrimSpace(fixed) != strings.TrimSpace(content) {
content = fixed
}

// Rewrite migrations to use t.Timestamps() if necessary
fixed, err = fix.AutoTimestampsOff(content)
if err != nil {
return err
}

if strings.TrimSpace(fixed) != strings.TrimSpace(content) {
f, err := os.Create(path)
if err != nil {
return err
}
if _, err := f.WriteString(fixed); err != nil {
return err
}
if err := f.Close(); err != nil {
return err
}
}
return nil
defer f.Close()
return fix.Fizz(f, f)
}

func init() {
Expand Down

0 comments on commit bcfc830

Please sign in to comment.