Skip to content

Commit

Permalink
Replace deprecated ioutil functions with their io or os equilvalents
Browse files Browse the repository at this point in the history
  • Loading branch information
anthonyfok committed Mar 20, 2024
1 parent e701253 commit 994f003
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 23 deletions.
10 changes: 5 additions & 5 deletions check_depends.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package main

import (
"fmt"
"golang.org/x/mod/modfile"
"golang.org/x/tools/go/vcs"
"io/ioutil"
"log"
"os"
"path/filepath"
"pault.ag/go/debian/control"
"strings"

"golang.org/x/mod/modfile"
"golang.org/x/tools/go/vcs"
"pault.ag/go/debian/control"
)

type dependency struct {
Expand Down Expand Up @@ -92,7 +92,7 @@ func execCheckDepends(args []string) {
// i.e. it returns the one defined in go.mod as well as the transitively ones
// TODO: this may not be the best way of doing thing since it requires the package to be converted to go module
func parseGoModDependencies(directory string, goBinaries map[string]string) ([]dependency, error) {
b, err := ioutil.ReadFile(filepath.Join(directory, "go.mod"))
b, err := os.ReadFile(filepath.Join(directory, "go.mod"))
if err != nil {
return nil, err
}
Expand Down
9 changes: 4 additions & 5 deletions check_depends_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"io/ioutil"
"os"
"path/filepath"
"reflect"
Expand Down Expand Up @@ -43,7 +42,7 @@ Description: read your RSS feeds from your terminal
Terminews is a terminal based application (TUI)
that allows you to manage RSS resources and display their news feeds.
`
tmpDir, err := ioutil.TempDir("", "dh-make-golang")
tmpDir, err := os.MkdirTemp("", "dh-make-golang")
if err != nil {
t.Fatalf("Could not create temp dir: %v", err)
}
Expand All @@ -52,7 +51,7 @@ Description: read your RSS feeds from your terminal
if err := os.MkdirAll(filepath.Join(tmpDir, "dummy-package", "debian"), 0750); err != nil {
t.Fatalf("Could not create dummy Debian package: %v", err)
}
if err := ioutil.WriteFile(filepath.Join(tmpDir, "dummy-package", "debian", "control"), []byte(f), 0640); err != nil {
if err := os.WriteFile(filepath.Join(tmpDir, "dummy-package", "debian", "control"), []byte(f), 0640); err != nil {
t.Fatalf("Could not create dummy Debian package: %v", err)
}

Expand Down Expand Up @@ -99,7 +98,7 @@ require (
github.com/google/go-github/v38 v38.1.0
github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79
)`
tmpDir, err := ioutil.TempDir("", "dh-make-golang")
tmpDir, err := os.MkdirTemp("", "dh-make-golang")
if err != nil {
t.Fatalf("Could not create temp dir: %v", err)
}
Expand All @@ -108,7 +107,7 @@ require (
if err := os.MkdirAll(filepath.Join(tmpDir, "dummy-package"), 0750); err != nil {
t.Fatalf("Could not create dummy Debian package: %v", err)
}
if err := ioutil.WriteFile(filepath.Join(tmpDir, "dummy-package", "go.mod"), []byte(f), 0640); err != nil {
if err := os.WriteFile(filepath.Join(tmpDir, "dummy-package", "go.mod"), []byte(f), 0640); err != nil {
t.Fatalf("Could not create dummy Debian package: %v", err)
}

Expand Down
4 changes: 2 additions & 2 deletions create_salsa_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main
import (
"flag"
"fmt"
"io/ioutil"
"io"
"log"
"net/http"
"net/url"
Expand Down Expand Up @@ -41,7 +41,7 @@ func execCreateSalsaProject(args []string) {
log.Fatalf("http post: %s", err)
}
if got, want := resp.StatusCode, http.StatusOK; got != want {
b, _ := ioutil.ReadAll(resp.Body)
b, _ := io.ReadAll(resp.Body)
log.Fatalf("unexpected HTTP status code: got %d, want %d (response: %s)", got, want, string(b))
}
}
3 changes: 1 addition & 2 deletions estimate.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"flag"
"fmt"
"go/build"
"io/ioutil"
"log"
"os"
"os/exec"
Expand Down Expand Up @@ -59,7 +58,7 @@ func removeVendor(gopath string) (found bool, _ error) {

func estimate(importpath string) error {
// construct a separate GOPATH in a temporary directory
gopath, err := ioutil.TempDir("", "dh-make-golang")
gopath, err := os.MkdirTemp("", "dh-make-golang")
if err != nil {
return fmt.Errorf("create temp dir: %w", err)
}
Expand Down
7 changes: 3 additions & 4 deletions make.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
Expand Down Expand Up @@ -174,7 +173,7 @@ func (u *upstream) tarballFromHoster() error {
}

func (u *upstream) tar(gopath, repo string) error {
f, err := ioutil.TempFile("", "dh-make-golang")
f, err := os.CreateTemp("", "dh-make-golang")
if err != nil {
return fmt.Errorf("create temp file: %w", err)
}
Expand Down Expand Up @@ -339,7 +338,7 @@ func (u *upstream) findDependencies(gopath, repo string) error {
}

func makeUpstreamSourceTarball(repo, revision string, forcePrerelease bool) (*upstream, error) {
gopath, err := ioutil.TempDir("", "dh-make-golang")
gopath, err := os.MkdirTemp("", "dh-make-golang")
if err != nil {
return nil, fmt.Errorf("create tmp dir: %w", err)
}
Expand Down Expand Up @@ -656,7 +655,7 @@ func getDebianEmail() string {
if email := strings.TrimSpace(os.Getenv("DEBEMAIL")); email != "" {
return email
}
mailname, err := ioutil.ReadFile("/etc/mailname")
mailname, err := os.ReadFile("/etc/mailname")
// By default, /etc/mailname contains "debian" which is not useful; check for ".".
if err == nil && strings.Contains(string(mailname), ".") {
if u, err := user.Current(); err == nil && u.Username != "" {
Expand Down
9 changes: 4 additions & 5 deletions version_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"io/ioutil"
"os"
"os/exec"
"path/filepath"
Expand All @@ -22,14 +21,14 @@ func TestSnapshotVersion(t *testing.T) {
os.Setenv("TZ", "UTC")
defer os.Unsetenv("TZ")

tempdir, err := ioutil.TempDir("", "dh-make-golang")
tempdir, err := os.MkdirTemp("", "dh-make-golang")
if err != nil {
t.Fatalf("Could not create temp dir: %v", err)
}
defer os.RemoveAll(tempdir)

tempfile := filepath.Join(tempdir, "test")
if err := ioutil.WriteFile(tempfile, []byte("testcase"), 0644); err != nil {
if err := os.WriteFile(tempfile, []byte("testcase"), 0644); err != nil {
t.Fatalf("Could not write temp file %q: %v", tempfile, err)
}

Expand Down Expand Up @@ -64,7 +63,7 @@ func TestSnapshotVersion(t *testing.T) {
t.Logf("got %q, want %q", got, want)
}

if err := ioutil.WriteFile(tempfile, []byte("testcase 2"), 0644); err != nil {
if err := os.WriteFile(tempfile, []byte("testcase 2"), 0644); err != nil {
t.Fatalf("Could not write temp file %q: %v", tempfile, err)
}

Expand All @@ -84,7 +83,7 @@ func TestSnapshotVersion(t *testing.T) {
t.Logf("got %q, want %q", got, want)
}

if err := ioutil.WriteFile(tempfile, []byte("testcase 3"), 0644); err != nil {
if err := os.WriteFile(tempfile, []byte("testcase 3"), 0644); err != nil {
t.Fatalf("Could not write temp file %q: %v", tempfile, err)
}

Expand Down

0 comments on commit 994f003

Please sign in to comment.