Skip to content

Commit

Permalink
Merge pull request #2 from metal3d/develop
Browse files Browse the repository at this point in the history
Fixing source reading
  • Loading branch information
metal3d committed May 8, 2022
2 parents 97a0f88 + 605a2cc commit 189f4cc
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Makefile
Expand Up @@ -20,7 +20,7 @@ uninstall:
go clean -i ./...

dev-build:
go build -v $(CC_OPTS) ./cmd/*.go
go build -v $(CC_OPTS) -o goreorder ./cmd/goreorder/*.go

.ONESHELL:
dist: clean-dist
Expand Down
9 changes: 6 additions & 3 deletions cmd/goreorder/main.go
Expand Up @@ -87,24 +87,27 @@ func main() {
// Get all files recursively
files, err := filepath.Glob(filepath.Join(dirname, "*.go"))
if err != nil {
log.Fatal(err)
log.Fatal("Get files:", err)
}
for _, file := range files {
if strings.HasSuffix(file, "_test.go") {
if verbose {
log.Println("Skipping test file: " + file)
}
continue
}
if verbose {
log.Println(file)
}
output, err := ordering.ReorderSource(file, formatExecutable, reorderStructs, input)
if err != nil {
log.Println(err)
log.Println("Orderging:", err)
continue
}
if write {
err = ioutil.WriteFile(file, []byte(output), 0644)
if err != nil {
log.Fatal(err)
log.Fatal("Write:", err)
}
} else {
fmt.Println(output)
Expand Down
23 changes: 11 additions & 12 deletions ordering/main.go
Expand Up @@ -29,6 +29,7 @@ type GoType struct {
// Parse the given file and return the methods, constructors and structs.
func Parse(filename, formatCommand string, src interface{}) (map[string][]*GoType, map[string][]*GoType, map[string]*GoType, error) {
fset := token.NewFileSet()

f, err := parser.ParseFile(fset, filename, src, parser.ParseComments)
if err != nil {
return nil, nil, nil, err
Expand Down Expand Up @@ -202,21 +203,19 @@ func ReorderSource(filename, formatCommand string, reorderStructs bool, src inte
// get the content of the file
var content []byte
var err error

methods, constructors, structs, err := Parse(filename, formatCommand, src)

if src == nil {
var readErr error
content, readErr = ioutil.ReadFile(filename)
if readErr != nil {
return "", readErr
if src == nil || len(src.([]byte)) == 0 {
content, err = ioutil.ReadFile(filename)
if err != nil {
return "", err
}
} else {
content = src.([]byte)
}

methods, constructors, structs, err := Parse(filename, formatCommand, content)

if err != nil {
return string(content), err
return string(content), errors.New("Error parsing source: " + err.Error())
}

if len(structs) == 0 {
Expand Down Expand Up @@ -304,13 +303,13 @@ func ReorderSource(filename, formatCommand string, reorderStructs bool, src inte
// write in a temporary file and use "gofmt" to format it
tmpfile, err := ioutil.TempFile("", "")
if err != nil {
return string(content), err
return string(content), errors.New("Failed to create temp file: " + err.Error())
}
defer os.Remove(tmpfile.Name()) // clean up
defer tmpfile.Close()

if _, err := tmpfile.Write([]byte(output)); err != nil {
return string(content), err
return string(content), errors.New("Failed to write to temporary file: " + err.Error())
}

cmd := exec.Command(formatCommand, "-w", tmpfile.Name())
Expand All @@ -321,7 +320,7 @@ func ReorderSource(filename, formatCommand string, reorderStructs bool, src inte
// read the temporary file
newcontent, err := ioutil.ReadFile(tmpfile.Name())
if err != nil {
return string(content), err
return string(content), errors.New("Read Temporary File error: " + err.Error())
}

return string(newcontent), nil
Expand Down

0 comments on commit 189f4cc

Please sign in to comment.