From 1597945eaf149b5cf77e274b57126211074716c9 Mon Sep 17 00:00:00 2001 From: Luca Comellini Date: Wed, 6 Nov 2024 11:33:14 -0800 Subject: [PATCH] Remove deprecated ioutil and unused function (#70) --- main.go | 36 +++++++++++------------------------- main_test.go | 17 ++++++++--------- 2 files changed, 19 insertions(+), 34 deletions(-) diff --git a/main.go b/main.go index e1a8060..b4ba22f 100644 --- a/main.go +++ b/main.go @@ -3,7 +3,6 @@ package main import ( "errors" "fmt" - "io/ioutil" "log" "os" "path/filepath" @@ -24,18 +23,6 @@ func getenvStr(key string) (string, error) { return v, nil } -func getenvInt(key string, def int) (int, error) { - s, err := getenvStr(key) - if err != nil { - return 0, err - } - v, err := strconv.Atoi(s) - if err != nil { - return 0, err - } - return v, nil -} - func getenvBool(key string) (bool, error) { s, err := getenvStr(key) if err != nil { @@ -76,10 +63,10 @@ func doesFileMatch(path string, include string, exclude string) bool { func findAndReplace(path string, find string, replace string, regex bool) (bool, error) { if find != replace { - read, readErr := ioutil.ReadFile(path) + read, readErr := os.ReadFile(path) check(readErr) - var newContents = "" + var newContents string if regex { re := regexp.MustCompile(find) newContents = re.ReplaceAllString(string(read), replace) @@ -88,7 +75,7 @@ func findAndReplace(path string, find string, replace string, regex bool) (bool, } if newContents != string(read) { - writeErr := ioutil.WriteFile(path, []byte(newContents), 0) + writeErr := os.WriteFile(path, []byte(newContents), 0) check(writeErr) return true, nil } @@ -99,15 +86,14 @@ func findAndReplace(path string, find string, replace string, regex bool) (bool, func setGithubEnvOutput(key string, value int) { outputFilename := os.Getenv("GITHUB_OUTPUT") - f, err := os.OpenFile(outputFilename, - os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) - if err != nil { - log.Println(err) - } - defer f.Close() - if _, err := f.WriteString(fmt.Sprintf("%s=%d\n", key, value)); err != nil { - log.Println(err) - } + f, err := os.OpenFile(outputFilename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644) + if err != nil { + log.Println(err) + } + defer f.Close() + if _, err := fmt.Fprintf(f, "%s=%d\n", key, value); err != nil { + log.Println(err) + } } func main() { diff --git a/main_test.go b/main_test.go index fedb42b..07789ff 100644 --- a/main_test.go +++ b/main_test.go @@ -1,27 +1,26 @@ package main import ( - "io/ioutil" "os" "testing" ) func TestHappyPath(t *testing.T) { - _ = os.Mkdir("out", 0755) + _ = os.Mkdir("out", 0o755) - if err := ioutil.WriteFile("out/data.txt", []byte("Hello world\n"), 0644); err != nil { + if err := os.WriteFile("out/data.txt", []byte("Hello world\n"), 0o644); err != nil { t.Error(err) } - os.Setenv("INPUT_INCLUDE", "out/data.txt") - os.Setenv("INPUT_EXCLUDE", "") - os.Setenv("INPUT_FIND", "world") - os.Setenv("INPUT_REPLACE", "there") - os.Setenv("GITHUB_OUTPUT", "out/output.txt") + t.Setenv("INPUT_INCLUDE", "out/data.txt") + t.Setenv("INPUT_EXCLUDE", "") + t.Setenv("INPUT_FIND", "world") + t.Setenv("INPUT_REPLACE", "there") + t.Setenv("GITHUB_OUTPUT", "out/output.txt") main() - data, err := ioutil.ReadFile("out/data.txt") + data, err := os.ReadFile("out/data.txt") if err != nil { t.Error(err) }