Skip to content

Commit 5e87111

Browse files
committed
Stop using deprecate io/ioutil package
1 parent 1e2bdd8 commit 5e87111

File tree

7 files changed

+16
-21
lines changed

7 files changed

+16
-21
lines changed

go/extractor/cli/go-autobuilder/go-autobuilder.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package main
33
import (
44
"bufio"
55
"fmt"
6-
"io/ioutil"
76
"log"
87
"net/url"
98
"os"
@@ -278,7 +277,7 @@ func fixGoVendorIssues(modMode ModMode, depMode DependencyInstallerMode, goDirec
278277
if depMode == GoGetWithModules {
279278
if !goDirectiveFound {
280279
// if the go.mod does not contain a version line
281-
modulesTxt, err := ioutil.ReadFile("vendor/modules.txt")
280+
modulesTxt, err := os.ReadFile("vendor/modules.txt")
282281
if err != nil {
283282
log.Println("Failed to read vendor/modules.txt to check for mismatched Go version")
284283
} else if explicitRe := regexp.MustCompile("(?m)^## explicit$"); !explicitRe.Match(modulesTxt) {
@@ -365,7 +364,7 @@ type moveGopathInfo struct {
365364
func moveToTemporaryGopath(srcdir string, importpath string) moveGopathInfo {
366365
// a temporary directory where everything is moved while the correct
367366
// directory structure is created.
368-
scratch, err := ioutil.TempDir(srcdir, "scratch")
367+
scratch, err := os.MkdirTemp(srcdir, "scratch")
369368
if err != nil {
370369
log.Fatalf("Failed to create temporary directory %s in directory %s: %s\n",
371370
scratch, srcdir, err.Error())
@@ -431,7 +430,7 @@ func createPathTransformerFile(newdir string) *os.File {
431430

432431
// set up SEMMLE_PATH_TRANSFORMER to ensure paths in the source archive and the snapshot
433432
// match the original source location, not the location we moved it to
434-
pt, err := ioutil.TempFile("", "path-transformer")
433+
pt, err := os.CreateTemp("", "path-transformer")
435434
if err != nil {
436435
log.Fatalf("Unable to create path transformer file: %s.", err.Error())
437436
}
@@ -506,7 +505,7 @@ func buildWithCustomCommands(inst string) {
506505
ext = ".sh"
507506
header = "#! /bin/bash\nset -xe +u\n"
508507
}
509-
script, err := ioutil.TempFile("", "go-build-command-*"+ext)
508+
script, err := os.CreateTemp("", "go-build-command-*"+ext)
510509
if err != nil {
511510
log.Fatalf("Unable to create temporary script holding custom build commands: %s\n", err.Error())
512511
}
@@ -619,7 +618,7 @@ func installDependenciesAndBuild() {
619618
}
620619
if depMode == GoGetWithModules {
621620
versionRe := regexp.MustCompile(`(?m)^go[ \t\r]+([0-9]+\.[0-9]+)$`)
622-
goMod, err := ioutil.ReadFile("go.mod")
621+
goMod, err := os.ReadFile("go.mod")
623622
if err != nil {
624623
log.Println("Failed to read go.mod to check for missing Go version")
625624
} else {

go/extractor/cli/go-bootstrap/go-bootstrap.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package main
22

33
import (
44
"fmt"
5-
"io/ioutil"
65
"log"
76
"os"
87
"regexp"
@@ -22,7 +21,7 @@ func main() {
2221
buildSteps := os.Args[2]
2322

2423
haveRepo := false
25-
content, err := ioutil.ReadFile(vars)
24+
content, err := os.ReadFile(vars)
2625
if err != nil {
2726
log.Fatal(err)
2827
}
@@ -34,7 +33,7 @@ func main() {
3433
additionalVars += "SEMMLE_REPO_URL=${repository}\n"
3534
}
3635
content = append(content, []byte(additionalVars)...)
37-
err = ioutil.WriteFile(vars, content, 0644)
36+
err = os.WriteFile(vars, content, 0644)
3837
if err != nil {
3938
log.Fatal(err)
4039
}
@@ -47,7 +46,7 @@ func main() {
4746
<build export="%s">${semmle_dist}/language-packs/go/tools/platform/${semmle_platform}/bin/go-autobuilder</build>
4847
</autoupdate>
4948
`, export))
50-
err = ioutil.WriteFile(buildSteps, content, 0644)
49+
err = os.WriteFile(buildSteps, content, 0644)
5150
if err != nil {
5251
log.Fatal(err)
5352
}

go/extractor/cli/go-tokenizer/go-tokenizer.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"fmt"
77
"go/scanner"
88
"go/token"
9-
"io/ioutil"
109
"log"
1110
"os"
1211
"strings"
@@ -20,7 +19,7 @@ func main() {
2019
defer csv.Flush()
2120

2221
for _, fileName := range flag.Args() {
23-
src, err := ioutil.ReadFile(fileName)
22+
src, err := os.ReadFile(fileName)
2423
if err != nil {
2524
log.Fatalf("Unable to read file %s.", fileName)
2625
}

go/extractor/extractor.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"go/token"
1111
"go/types"
1212
"io"
13-
"io/ioutil"
1413
"log"
1514
"os"
1615
"path/filepath"
@@ -1807,7 +1806,7 @@ func extractNumLines(tw *trap.Writer, fileName string, ast *ast.File) {
18071806

18081807
// count lines of code by tokenizing
18091808
linesOfCode := 0
1810-
src, err := ioutil.ReadFile(fileName)
1809+
src, err := os.ReadFile(fileName)
18111810
if err != nil {
18121811
log.Fatalf("Unable to read file %s.", fileName)
18131812
}

go/extractor/gomodextractor.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ package extractor
22

33
import (
44
"fmt"
5-
"golang.org/x/mod/modfile"
6-
"io/ioutil"
5+
"io"
76
"log"
87
"os"
98
"path/filepath"
109
"strings"
1110

11+
"golang.org/x/mod/modfile"
12+
1213
"github.com/github/codeql-go/extractor/dbscheme"
1314
"github.com/github/codeql-go/extractor/srcarchive"
1415
"github.com/github/codeql-go/extractor/trap"
@@ -45,7 +46,7 @@ func (extraction *Extraction) extractGoMod(path string) error {
4546
if err != nil {
4647
return fmt.Errorf("failed to open go.mod file %s: %s", path, err.Error())
4748
}
48-
data, err := ioutil.ReadAll(file)
49+
data, err := io.ReadAll(file)
4950
if err != nil {
5051
return fmt.Errorf("failed to read go.mod file %s: %s", path, err.Error())
5152
}

go/extractor/srcarchive/projectlayout_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
package srcarchive
22

33
import (
4-
"io/ioutil"
54
"os"
65
"testing"
76
)
87

98
func mkProjectLayout(projectLayoutSource string, t *testing.T) (*ProjectLayout, error) {
10-
pt, err := ioutil.TempFile("", "path-transformer")
9+
pt, err := os.CreateTemp("", "path-transformer")
1110
if err != nil {
1211
t.Fatalf("Unable to create temporary file for project layout: %s", err.Error())
1312
}

go/extractor/trap/trapwriter.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"fmt"
88
"go/ast"
99
"go/types"
10-
"io/ioutil"
1110
"os"
1211
"path/filepath"
1312
"unicode/utf8"
@@ -51,7 +50,7 @@ func NewWriter(path string, pkg *packages.Package) (*Writer, error) {
5150
if err != nil {
5251
return nil, err
5352
}
54-
tmpFile, err := ioutil.TempFile(trapFileDir, filepath.Base(trapFilePath))
53+
tmpFile, err := os.CreateTemp(trapFileDir, filepath.Base(trapFilePath))
5554
if err != nil {
5655
return nil, err
5756
}

0 commit comments

Comments
 (0)