feat: replace ioutil.ReadAll with os pkg

main
Tran Hau 2021-03-17 04:29:51 +00:00
parent b203f057bc
commit abac054448
1 changed files with 9 additions and 13 deletions

View File

@ -4,7 +4,6 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
) )
@ -32,19 +31,14 @@ type Path struct {
// Load config from file // Load config from file
func LoadConfig(path string) (result Config, err error) { func LoadConfig(path string) (result Config, err error) {
configPath := getConfigPath(path) configPath := getConfigPath(path)
f, err := os.Open(configPath) bytes, err := os.ReadFile(configPath)
if err != nil { if err != nil {
// https://github.com/golang/go/blob/3e1e13ce6d1271f49f3d8ee359689145a6995bad/src/os/error.go#L90-L91
if errors.Is(err, os.ErrNotExist) { if errors.Is(err, os.ErrNotExist) {
err = fmt.Errorf("file not exist %s: %w", configPath, err) err = fmt.Errorf("file not exist %s: %w", configPath, err)
return return
} }
}
defer f.Close()
bytes, err := ioutil.ReadAll(f) err = fmt.Errorf("failed to read file%s: %w", configPath, err)
if err != nil {
err = fmt.Errorf("failed to read %s: %w", configPath, err)
return return
} }
@ -95,25 +89,27 @@ func (c *Config) Update() error {
} }
func (c *Config) Clean() error { func (c *Config) Clean() error {
fileInfos, err := ioutil.ReadDir(configDirPath) files, err := os.ReadDir(configDirPath)
if err != nil { if err != nil {
return fmt.Errorf("failed to read dir %s: %w", configDirPath, err) return fmt.Errorf("failed to read dir %s: %w", configDirPath, err)
} }
// get all dirs inside config dir
unusedDirs := make(map[string]struct{}) unusedDirs := make(map[string]struct{})
for _, fileInfo := range fileInfos { for _, file := range files {
if fileInfo.Name() == configFile { if file.Name() == configFile {
continue continue
} }
unusedDirs[fileInfo.Name()] = struct{}{} unusedDirs[file.Name()] = struct{}{}
} }
// removed used apps // removed used dirs
for name := range c.Apps { for name := range c.Apps {
delete(unusedDirs, name) delete(unusedDirs, name)
} }
// delete ununsed dirs to save some space
for dir := range unusedDirs { for dir := range unusedDirs {
dirPath := filepath.Join(configDirPath, dir) dirPath := filepath.Join(configDirPath, dir)
if err := os.RemoveAll(dirPath); err != nil { if err := os.RemoveAll(dirPath); err != nil {