Browse Source

Replace json.Unmarshal with json.Decoder().Decode()

Signed-off-by: Antonio Murdaca <me@runcom.ninja>
Antonio Murdaca 10 năm trước cách đây
mục cha
commit
26543e0309

+ 1 - 1
builder/parser/line_parsers.go

@@ -233,7 +233,7 @@ func parseString(rest string) (*Node, map[string]bool, error) {
 // parseJSON converts JSON arrays to an AST.
 func parseJSON(rest string) (*Node, map[string]bool, error) {
 	var myJson []interface{}
-	if err := json.Unmarshal([]byte(rest), &myJson); err != nil {
+	if err := json.NewDecoder(strings.NewReader(rest)).Decode(&myJson); err != nil {
 		return nil, nil, err
 	}
 

+ 4 - 4
daemon/execdriver/lxc/init.go

@@ -4,7 +4,6 @@ import (
 	"encoding/json"
 	"flag"
 	"fmt"
-	"io/ioutil"
 	"log"
 	"os"
 	"os/exec"
@@ -107,12 +106,13 @@ func getArgs() *InitArgs {
 func setupEnv(args *InitArgs) error {
 	// Get env
 	var env []string
-	content, err := ioutil.ReadFile(".dockerenv")
+	dockerenv, err := os.Open(".dockerenv")
 	if err != nil {
 		return fmt.Errorf("Unable to load environment variables: %v", err)
 	}
-	if err := json.Unmarshal(content, &env); err != nil {
-		return fmt.Errorf("Unable to unmarshal environment variables: %v", err)
+	defer dockerenv.Close()
+	if err := json.NewDecoder(dockerenv).Decode(&env); err != nil {
+		return fmt.Errorf("Unable to decode environment variables: %v", err)
 	}
 	// Propagate the plugin-specific container env variable
 	env = append(env, "container="+os.Getenv("container"))

+ 15 - 11
graph/load.go

@@ -58,22 +58,26 @@ func (s *TagStore) Load(inTar io.ReadCloser, outStream io.Writer) error {
 		}
 	}
 
-	repositoriesJson, err := ioutil.ReadFile(path.Join(tmpImageDir, "repo", "repositories"))
-	if err == nil {
-		repositories := map[string]Repository{}
-		if err := json.Unmarshal(repositoriesJson, &repositories); err != nil {
+	reposJSONFile, err := os.Open(path.Join(tmpImageDir, "repo", "repositories"))
+	if err != nil {
+		if !os.IsNotExist(err) {
 			return err
 		}
+		return nil
+	}
+	defer reposJSONFile.Close()
 
-		for imageName, tagMap := range repositories {
-			for tag, address := range tagMap {
-				if err := s.SetLoad(imageName, tag, address, true, outStream); err != nil {
-					return err
-				}
+	repositories := map[string]Repository{}
+	if err := json.NewDecoder(reposJSONFile).Decode(&repositories); err != nil {
+		return err
+	}
+
+	for imageName, tagMap := range repositories {
+		for tag, address := range tagMap {
+			if err := s.SetLoad(imageName, tag, address, true, outStream); err != nil {
+				return err
 			}
 		}
-	} else if !os.IsNotExist(err) {
-		return err
 	}
 
 	return nil

+ 3 - 2
graph/tags.go

@@ -115,11 +115,12 @@ func (store *TagStore) save() error {
 }
 
 func (store *TagStore) reload() error {
-	jsonData, err := ioutil.ReadFile(store.path)
+	f, err := os.Open(store.path)
 	if err != nil {
 		return err
 	}
-	if err := json.Unmarshal(jsonData, store); err != nil {
+	defer f.Close()
+	if err := json.NewDecoder(f).Decode(&store); err != nil {
 		return err
 	}
 	return nil

+ 1 - 2
pkg/parsers/filters/parse.go

@@ -58,8 +58,7 @@ func FromParam(p string) (Args, error) {
 	if len(p) == 0 {
 		return args, nil
 	}
-	err := json.Unmarshal([]byte(p), &args)
-	if err != nil {
+	if err := json.NewDecoder(strings.NewReader(p)).Decode(&args); err != nil {
 		return nil, err
 	}
 	return args, nil