Skip to content

Commit

Permalink
Remove deprecated ioutil and unused function
Browse files Browse the repository at this point in the history
  • Loading branch information
lucacome committed Nov 6, 2024
1 parent d79082f commit 8f1b95e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 34 deletions.
36 changes: 11 additions & 25 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
Expand All @@ -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 {
Expand Down Expand Up @@ -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 = ""
newContents := ""
if regex {
re := regexp.MustCompile(find)
newContents = re.ReplaceAllString(string(read), replace)
Expand All @@ -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
}
Expand All @@ -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() {
Expand Down
17 changes: 8 additions & 9 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
Expand Down

0 comments on commit 8f1b95e

Please sign in to comment.