Skip to content

Commit

Permalink
replaced deprecated ioutil
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube committed Apr 26, 2022
1 parent 71e4cb7 commit ae981bf
Show file tree
Hide file tree
Showing 25 changed files with 51 additions and 66 deletions.
17 changes: 8 additions & 9 deletions cmd/generate/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -74,8 +73,8 @@ func executeCompleter(completer string) {
os.Exit(1)
}

ioutil.WriteFile(root+"/cmd/carapace/cmd/completers.go", []byte("//go:build !release\n\n"+content), 0644)
ioutil.WriteFile(root+"/cmd/carapace/cmd/completers_release.go", []byte("//go:build release\n\n"+strings.Replace(content, "/completers/", "/completers_release/", -1)), 0644)
os.WriteFile(root+"/cmd/carapace/cmd/completers.go", []byte("//go:build !release\n\n"+content), 0644)
os.WriteFile(root+"/cmd/carapace/cmd/completers_release.go", []byte("//go:build release\n\n"+strings.Replace(content, "/completers/", "/completers_release/", -1)), 0644)
os.RemoveAll(root + "/completers_release")
exec.Command("cp", "-r", root+"/completers", root+"/completers_release").Run()

Expand All @@ -86,17 +85,17 @@ func executeCompleter(completer string) {
for _, file := range files {
if !file.IsDir() && strings.HasSuffix(file.Name(), ".go") {
path := fmt.Sprintf("%v/completers_release/%v_completer/cmd/%v", root, name, file.Name())
content, err := ioutil.ReadFile(path)
content, err := os.ReadFile(path)
if err == nil && strings.Contains(string(content), "func init() {") {
patched := strings.Replace(string(content), "func init() {", fmt.Sprintf("func init_%v() {", strings.TrimSuffix(file.Name(), ".go")), 1)
ioutil.WriteFile(path, []byte(patched), os.ModePerm)
os.WriteFile(path, []byte(patched), os.ModePerm)
initFuncs = append(initFuncs, fmt.Sprintf(" init_%v()", strings.TrimSuffix(file.Name(), ".go")))
}
}
}

path := fmt.Sprintf("%v/completers_release/%v_completer/cmd/root.go", root, name)
content, err := ioutil.ReadFile(path)
content, err := os.ReadFile(path)
if err == nil {
patched := make([]string, 0)
for _, line := range strings.Split(string(content), "\n") {
Expand All @@ -105,7 +104,7 @@ func executeCompleter(completer string) {
patched = append(patched, initFuncs...)
}
}
ioutil.WriteFile(path, []byte(strings.Join(patched, "\n")), os.ModePerm)
os.WriteFile(path, []byte(strings.Join(patched, "\n")), os.ModePerm)
}
}
}
Expand All @@ -122,7 +121,7 @@ func readCompleters() ([]string, map[string]string) {
names := make([]string, 0)
descriptions := make(map[string]string)
if root, err := rootDir(); err == nil {
if files, err := ioutil.ReadDir(root + "/completers/"); err == nil {
if files, err := os.ReadDir(root + "/completers/"); err == nil {
for _, file := range files {
if file.IsDir() && strings.HasSuffix(file.Name(), "_completer") {
name := strings.TrimSuffix(file.Name(), "_completer")
Expand All @@ -137,7 +136,7 @@ func readCompleters() ([]string, map[string]string) {
}

func readDescription(root string, completer string) string {
if content, err := ioutil.ReadFile(fmt.Sprintf("%v/completers/%v/cmd/root.go", root, completer)); err == nil {
if content, err := os.ReadFile(fmt.Sprintf("%v/completers/%v/cmd/root.go", root, completer)); err == nil {
re := regexp.MustCompile("^\tShort: +\"(?P<description>.*)\",$")
for _, line := range strings.Split(string(content), "\n") {
if re.MatchString(line) {
Expand Down
4 changes: 2 additions & 2 deletions completers/ant_completer/cmd/action/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package action

import (
"encoding/xml"
"io/ioutil"
"os"

"github.com/rsteube/carapace"
)
Expand All @@ -19,7 +19,7 @@ func ActionTargets(path string) carapace.Action {
if path == "" {
path = "build.xml"
}
content, err := ioutil.ReadFile(path)
content, err := os.ReadFile(path)
if err != nil {
return carapace.ActionMessage(err.Error())
}
Expand Down
5 changes: 2 additions & 3 deletions completers/cargo_completer/cmd/action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package action
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"regexp"
"strings"
Expand Down Expand Up @@ -61,7 +60,7 @@ func parseManifest(cmd *cobra.Command) (m manifestToml, err error) {
var content []byte
var path string
if path, err = manifestLocation(cmd); err == nil {
if content, err = ioutil.ReadFile(path); err == nil {
if content, err = os.ReadFile(path); err == nil {
err = toml.Unmarshal(content, &m)
}
}
Expand Down Expand Up @@ -195,7 +194,7 @@ type config struct {

func parseConfig(path string) (c config, err error) {
var content []byte
if content, err = ioutil.ReadFile(path); err == nil {
if content, err = os.ReadFile(path); err == nil {
err = toml.Unmarshal(content, &c)
}
return
Expand Down
5 changes: 2 additions & 3 deletions completers/flutter_completer/cmd/action/sample.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package action

import (
"encoding/json"
"io/ioutil"
"os"
"time"

Expand All @@ -16,7 +15,7 @@ type sample struct {

func ActionSamples() carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
tmpfile, err := ioutil.TempFile(os.TempDir(), "carapace-flutter-samples.json")
tmpfile, err := os.CreateTemp(os.TempDir(), "carapace-flutter-samples.json")
if err != nil {
return carapace.ActionMessage(err.Error())
}
Expand All @@ -30,7 +29,7 @@ func ActionSamples() carapace.Action {
}
defer os.Remove(tmpfile.Name()) // remove after parsing content
return carapace.ActionExecCommand("flutter", "create", "--suppress-analytics", "--list-samples", tmpfile.Name())(func(output []byte) carapace.Action {
content, err := ioutil.ReadFile(tmpfile.Name())
content, err := os.ReadFile(tmpfile.Name())
if err != nil {
return carapace.ActionMessage(err.Error())
}
Expand Down
4 changes: 2 additions & 2 deletions completers/gh_completer/cmd/action/config/config_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package config
import (
"errors"
"fmt"
"io/ioutil"
"io"
"os"
"path/filepath"
"syscall"
Expand Down Expand Up @@ -100,7 +100,7 @@ var ReadConfigFile = func(filename string) ([]byte, error) {
}
defer f.Close()

data, err := ioutil.ReadAll(f)
data, err := io.ReadAll(f)
if err != nil {
return nil, err
}
Expand Down
3 changes: 1 addition & 2 deletions completers/glab_completer/cmd/action/alias.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package action

import (
"io/ioutil"
"os"

"github.com/rsteube/carapace"
Expand All @@ -12,7 +11,7 @@ func LoadAliases() (aliases map[string]string, err error) {
var dir string
if dir, err = os.UserConfigDir(); err == nil {
var content []byte
if content, err = ioutil.ReadFile(dir + "/glab-cli/aliases.yml"); err == nil {
if content, err = os.ReadFile(dir + "/glab-cli/aliases.yml"); err == nil {
err = yaml.Unmarshal(content, &aliases)
}
}
Expand Down
3 changes: 1 addition & 2 deletions completers/glab_completer/cmd/action/config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package action

import (
"io/ioutil"
"os"

"github.com/rsteube/carapace"
Expand Down Expand Up @@ -39,7 +38,7 @@ func loadConfig() (config *glabConfig, err error) {
var dir string
if dir, err = os.UserConfigDir(); err == nil {
var content []byte
if content, err = ioutil.ReadFile(dir + "/glab-cli/config.yml"); err == nil {
if content, err = os.ReadFile(dir + "/glab-cli/config.yml"); err == nil {
err = yaml.Unmarshal(content, &config)
}
}
Expand Down
5 changes: 2 additions & 3 deletions completers/helm_completer/cmd/action/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package action

import (
"fmt"
"io/ioutil"
"os"

"github.com/rsteube/carapace"
Expand All @@ -23,7 +22,7 @@ func ActionRepositories() carapace.Action {
return carapace.ActionMessage(err.Error())
}

content, err := ioutil.ReadFile(dir + "/helm/repositories.yaml")
content, err := os.ReadFile(dir + "/helm/repositories.yaml")
if err != nil {
return carapace.ActionMessage(err.Error())
}
Expand Down Expand Up @@ -56,7 +55,7 @@ func loadIndex(repo string) (index *index, err error) {
return nil, err
}

content, err := ioutil.ReadFile(fmt.Sprintf("%v/helm/repository/%v-index.yaml", dir, repo))
content, err := os.ReadFile(fmt.Sprintf("%v/helm/repository/%v-index.yaml", dir, repo))
if err != nil {
return nil, err
}
Expand Down
3 changes: 1 addition & 2 deletions completers/hugo_completer/cmd/action/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package action

import (
"encoding/json"
"io/ioutil"
"os"
"path/filepath"

Expand Down Expand Up @@ -36,7 +35,7 @@ func loadConfig() (c config, err error) {
var path string
if path, err = configPath(); err == nil {
var content []byte
if content, err = ioutil.ReadFile(path); err == nil {
if content, err = os.ReadFile(path); err == nil {
switch filepath.Ext(path) {
case ".json":
err = json.Unmarshal(content, &c)
Expand Down
3 changes: 1 addition & 2 deletions completers/hugo_completer/cmd/action/theme.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package action

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"

Expand All @@ -18,7 +17,7 @@ type theme struct {

func loadTheme(path string) (t theme, err error) {
var content []byte
if content, err = ioutil.ReadFile(path); err == nil {
if content, err = os.ReadFile(path); err == nil {
err = toml.Unmarshal(content, &t)
}
return
Expand Down
3 changes: 1 addition & 2 deletions completers/java_completer/cmd/action/classes.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package action

import (
"fmt"
"io/ioutil"
"os"
"strings"

Expand Down Expand Up @@ -33,7 +32,7 @@ func ActionClasspathClasses(cmd *cobra.Command) carapace.Action {
if !f.IsDir() {
files = append(files, path)
} else {
if fileInfos, err := ioutil.ReadDir(path); err == nil {
if fileInfos, err := os.ReadDir(path); err == nil {
for _, file := range fileInfos {
files = append(files, fmt.Sprintf("%v/%v", path, file.Name()))
}
Expand Down
6 changes: 3 additions & 3 deletions completers/mvn_completer/cmd/action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"archive/zip"
"encoding/xml"
"fmt"
"io/ioutil"
"io"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -167,7 +167,7 @@ func locatePom(file string) (pom string) {

func loadProject(file string) (project *Project, err error) {
var content []byte
if content, err = ioutil.ReadFile(locatePom(file)); err == nil {
if content, err = os.ReadFile(locatePom(file)); err == nil {
err = xml.Unmarshal(content, &project)
}
return
Expand All @@ -180,7 +180,7 @@ func loadPlugin(file string) (plugin *Plugin) {
if f.Name == "META-INF/maven/plugin.xml" {
if pluginFile, err := f.Open(); err == nil {
defer pluginFile.Close()
if content, err := ioutil.ReadAll(pluginFile); err == nil {
if content, err := io.ReadAll(pluginFile); err == nil {
_ = xml.Unmarshal(content, &plugin)
}
}
Expand Down
3 changes: 1 addition & 2 deletions completers/ng_completer/cmd/action/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package action

import (
"encoding/json"
"io/ioutil"
"os"

"github.com/rsteube/carapace"
Expand All @@ -27,7 +26,7 @@ func actionConfig(f func(cfg config) carapace.Action) carapace.Action {
if err != nil {
return carapace.ActionMessage(err.Error())
}
content, err := ioutil.ReadFile(path)
content, err := os.ReadFile(path)
if err != nil {
return carapace.ActionMessage(err.Error())
}
Expand Down
3 changes: 1 addition & 2 deletions completers/npm_completer/cmd/action/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package action
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"strings"

Expand Down Expand Up @@ -94,7 +93,7 @@ func loadPackageJson() (pj packageJson, err error) {
if wd, err = os.Getwd(); err == nil {
if packageFile, err = util.FindReverse(wd, "package.json"); err == nil {
var content []byte
if content, err = ioutil.ReadFile(packageFile); err == nil {
if content, err = os.ReadFile(packageFile); err == nil {
err = json.Unmarshal(content, &pj)
}
}
Expand Down
3 changes: 1 addition & 2 deletions completers/toitpkg_completer/cmd/action/dependency.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package action

import (
"io/ioutil"
"os"

"github.com/rsteube/carapace"
Expand Down Expand Up @@ -34,7 +33,7 @@ func ActionDependencies(cmd *cobra.Command) carapace.Action {
return carapace.ActionMessage(err.Error())
}

content, err := ioutil.ReadFile(location)
content, err := os.ReadFile(location)
if err != nil {
return carapace.ActionMessage(err.Error())
}
Expand Down
3 changes: 1 addition & 2 deletions completers/vagrant_completer/cmd/action/provisioner.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package action

import (
"io/ioutil"
"os"
"regexp"
"strings"
Expand All @@ -22,7 +21,7 @@ func ActionProvisioners() carapace.Action {
return carapace.ActionMessage(err.Error())
}

content, err := ioutil.ReadFile(path)
content, err := os.ReadFile(path)
if err != nil {
return carapace.ActionMessage(err.Error())
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/actions/fs/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
package fs

import (
"io/ioutil"
"os"
"strings"
"unicode"

Expand All @@ -14,7 +14,7 @@ import (
// subdir/subsubder2
func ActionSubDirectories(path string) carapace.Action {
return carapace.ActionMultiParts("/", func(c carapace.Context) carapace.Action {
if files, err := ioutil.ReadDir(path + "/" + strings.Join(c.Parts, "/") + "/"); err != nil {
if files, err := os.ReadDir(path + "/" + strings.Join(c.Parts, "/") + "/"); err != nil {
return carapace.ActionValues()
} else {
dirs := make([]string, 0)
Expand Down
4 changes: 2 additions & 2 deletions pkg/actions/fs/mount.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package fs

import (
"io/ioutil"
"os"
"strings"

"github.com/rsteube/carapace"
Expand All @@ -13,7 +13,7 @@ import (
// /dev (dev)
func ActionMounts() carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
content, err := ioutil.ReadFile("/proc/mounts")
content, err := os.ReadFile("/proc/mounts")
if err != nil {
return carapace.ActionMessage(err.Error())
}
Expand Down
Loading

0 comments on commit ae981bf

Please sign in to comment.