Skip to content

Commit

Permalink
replace iotuil with os
Browse files Browse the repository at this point in the history
  • Loading branch information
xorpaul committed Sep 20, 2024
1 parent dbe5523 commit fa16938
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 21 deletions.
3 changes: 1 addition & 2 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"bufio"
"io/ioutil"
"os"
"path/filepath"
"reflect"
Expand All @@ -21,7 +20,7 @@ var (
// readConfigfile creates the ConfigSettings struct from the g10k config file
func readConfigfile(configFile string) ConfigSettings {
Debugf("Trying to read g10k config file: " + configFile)
data, err := ioutil.ReadFile(configFile)
data, err := os.ReadFile(configFile)
if err != nil {
Fatalf("readConfigfile(): There was an error parsing the config file " + configFile + ": " + err.Error())
}
Expand Down
9 changes: 4 additions & 5 deletions forge.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"encoding/hex"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
Expand All @@ -32,7 +31,7 @@ func checkDeprecation(fm ForgeModule, lastCheckedFile string) bool {
} else if fm.cacheTTL > 0 && fileInfo.ModTime().Add(fm.cacheTTL).Before(time.Now()) {
return false
} else {
json, err := ioutil.ReadFile(lastCheckedFile)
json, err := os.ReadFile(lastCheckedFile)
if err != nil {
Fatalf("doModuleInstallOrNothing(): Error while reading Forge API result from file " + lastCheckedFile + err.Error())
}
Expand Down Expand Up @@ -202,7 +201,7 @@ func queryForgeAPI(fm ForgeModule) ForgeResult {

if resp.StatusCode == http.StatusOK {
// need to get latest version
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
Fatalf("queryForgeAPI(): Error while reading response body for Forge module " + fm.name + " from " + url + ": " + err.Error())
}
Expand Down Expand Up @@ -306,7 +305,7 @@ func getMetadataForgeModule(fm ForgeModule) ForgeModule {
defer resp.Body.Close()

if resp.StatusCode == http.StatusOK {
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)

if err != nil {
Fatalf("getMetadataForgeModule(): Error while reading response body for Forge module " + fm.name + " from " + url + ": " + err.Error())
Expand Down Expand Up @@ -454,7 +453,7 @@ func downloadForgeModule(name string, version string, fm ForgeModule, retryCount

// readModuleMetadata returns the Forgemodule struct of the given module file path
func readModuleMetadata(file string) ForgeModule {
content, _ := ioutil.ReadFile(file)
content, _ := os.ReadFile(file)

before := time.Now()
name := gjson.Get(string(content), "name").String()
Expand Down
17 changes: 8 additions & 9 deletions g10k_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
Expand Down Expand Up @@ -560,19 +559,19 @@ func spinUpFakeForge(t *testing.T, metadataFile string) *httptest.Server {
// spin up HTTP test server to serve fake/invalid Forge module metadata
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/v3/releases/puppetlabs-ntp-6.0.0" {
body, err := ioutil.ReadFile(metadataFile)
body, err := os.ReadFile(metadataFile)
if err != nil {
t.Error(err)
}
fmt.Fprint(w, string(body))
} else if r.URL.Path == "/v3/modules/puppetlabs-ntp" {
body, err := ioutil.ReadFile("tests/fake-forge/latest-puppetlabs-ntp-metadata.json")
body, err := os.ReadFile("tests/fake-forge/latest-puppetlabs-ntp-metadata.json")
if err != nil {
t.Error(err)
}
fmt.Fprint(w, string(body))
} else if r.URL.Path == "/v3/files/puppetlabs-ntp-6.0.0.tar.gz" {
body, err := ioutil.ReadFile("tests/fake-forge/fake-puppetlabs-ntp-6.0.0.tar.gz")
body, err := os.ReadFile("tests/fake-forge/fake-puppetlabs-ntp-6.0.0.tar.gz")
if err != nil {
t.Error(err)
}
Expand Down Expand Up @@ -1753,7 +1752,7 @@ func TestLastCheckedFile(t *testing.T) {
}

fm := ForgeModule{version: "latest", name: "inifile", author: "puppetlabs", fileSize: 0, cacheTTL: 0}
json, _ := ioutil.ReadFile(lastCheckedFile)
json, _ := os.ReadFile(lastCheckedFile)
latestForgeModules.m = make(map[string]string)

result := parseForgeAPIResult(string(json), fm)
Expand All @@ -1779,7 +1778,7 @@ func TestLastCheckedFile(t *testing.T) {

branchParam = "single_cache"
resolvePuppetEnvironment(false, "")
json, _ = ioutil.ReadFile(lastCheckedFile)
json, _ = os.ReadFile(lastCheckedFile)
result = parseForgeAPIResult(string(json), fm)
result2 = queryForgeAPI(fm)

Expand Down Expand Up @@ -1860,7 +1859,7 @@ func TestPostrunCommand(t *testing.T) {
t.Errorf("postrun logfile file missing: %s", postrunLogfile)
}

content, _ := ioutil.ReadFile(postrunLogfile)
content, _ := os.ReadFile(postrunLogfile)

expectedLines := []string{
"postrun command wrapper script received argument: example_master",
Expand Down Expand Up @@ -1909,7 +1908,7 @@ func TestPostrunCommandDirs(t *testing.T) {
t.Errorf("postrun logfile file missing: %s", postrunLogfile)
}

content, _ := ioutil.ReadFile(postrunLogfile)
content, _ := os.ReadFile(postrunLogfile)

expectedLines := []string{
"postrun command wrapper script received argument: /tmp/example/example_master",
Expand Down Expand Up @@ -2681,7 +2680,7 @@ func TestCloneGitModules(t *testing.T) {
// check
for _, expectedDir := range expectedDirs {
headFile := filepath.Join(expectedDir, "HEAD")
content, err := ioutil.ReadFile(headFile)
content, err := os.ReadFile(headFile)
if err != nil {
t.Errorf("Error while reading content of file " + headFile + " Error: " + err.Error())
}
Expand Down
3 changes: 1 addition & 2 deletions git.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -208,7 +207,7 @@ func syncToModuleDir(gitModule GitModule, srcDir string, targetDir string, corre
}
}
} else {
targetHashByte, _ := ioutil.ReadFile(hashFile)
targetHashByte, _ := os.ReadFile(hashFile)
targetHash := string(targetHashByte)
Debugf("string content of " + hashFile + " is: " + targetHash)
if targetHash == commitHash {
Expand Down
5 changes: 2 additions & 3 deletions helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
Expand Down Expand Up @@ -315,7 +314,7 @@ func writeStructJSONFile(file string, v interface{}) {
Warnf("Could not encode JSON file " + file + " " + err.Error())
}

err = ioutil.WriteFile(file, content, 0644)
err = os.WriteFile(file, content, 0644)
if err != nil {
Warnf("Could not write JSON file " + file + " " + err.Error())
}
Expand All @@ -331,7 +330,7 @@ func readDeployResultFile(file string) DeployResult {
}
defer jsonFile.Close()

byteValue, err := ioutil.ReadAll(jsonFile)
byteValue, err := io.ReadAll(jsonFile)
if err != nil {
Warnf("Could not read JSON file " + file + " " + err.Error())
}
Expand Down

0 comments on commit fa16938

Please sign in to comment.