Skip to content

Commit

Permalink
Small cleanups and simplifications
Browse files Browse the repository at this point in the history
  • Loading branch information
petergtz committed May 9, 2023
1 parent d453b7c commit b4ac288
Showing 1 changed file with 12 additions and 18 deletions.
30 changes: 12 additions & 18 deletions pegomock/util/io.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package util

import (
"io/ioutil"
"os"
)
import "os"

// WithinWorkingDir changes the current working directory temporarily and
// executes cb within that context.
Expand All @@ -12,26 +9,23 @@ func WithinWorkingDir(targetPath string, cb func(workingDir string)) {
PanicOnError(e)
e = os.Chdir(targetPath)
PanicOnError(e)
defer func() { os.Chdir(origWorkingDir) }()
defer func() { _ = os.Chdir(origWorkingDir) }()
cb(targetPath)
}

func WriteFileIfChanged(outputFilepath string, output []byte) bool {
existingFileContent, err := ioutil.ReadFile(outputFilepath)
if err != nil {
if os.IsNotExist(err) {
err = ioutil.WriteFile(outputFilepath, output, 0664)
PanicOnError(err)
return true
} else {
panic(err)
}
existingFileContent, e := os.ReadFile(outputFilepath)
if os.IsNotExist(e) {
e = os.WriteFile(outputFilepath, output, 0664)
PanicOnError(e)
return true
}
PanicOnError(e)

if string(existingFileContent) == string(output) {
return false
} else {
err = ioutil.WriteFile(outputFilepath, output, 0664)
PanicOnError(err)
return true
}
e = os.WriteFile(outputFilepath, output, 0664)
PanicOnError(e)
return true
}

0 comments on commit b4ac288

Please sign in to comment.