Skip to content

Commit 12b8b9a

Browse files
committed
refactor to reduce complexity
Signed-off-by: Pablo Chacin <[email protected]>
1 parent 4123181 commit 12b8b9a

File tree

1 file changed

+41
-26
lines changed

1 file changed

+41
-26
lines changed

archive.go

+41-26
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,12 @@ type archiveMetadata struct {
6060
Env map[string]string `json:"env"`
6161
}
6262

63-
//nolint:forbidigo,gocognit
63+
const maxFileSize = 1024 * 1024 * 10 // 10M
64+
65+
//nolint:forbidigo
6466
func extractArchive(dir string, input io.Reader) error {
6567
reader := tar.NewReader(input)
6668

67-
const maxFileSize = 1024 * 1024 * 10 // 10M
68-
6969
for {
7070
header, err := reader.Next()
7171

@@ -87,7 +87,7 @@ func extractArchive(dir string, input io.Reader) error {
8787
}
8888

8989
case tar.TypeReg:
90-
if ext := filepath.Ext(target); ext == ".csv" || (ext == ".json" && filepath.Base(target) != "metadata.json") {
90+
if shouldSkip(target) {
9191
continue
9292
}
9393

@@ -107,37 +107,52 @@ func extractArchive(dir string, input io.Reader) error {
107107
// if it is a link or symlink, we copy the content of the linked file to the target
108108
// we assume the linked file was already processed and exists in the directory.
109109
case tar.TypeLink, tar.TypeSymlink:
110-
if ext := filepath.Ext(target); ext == ".csv" || (ext == ".json" && filepath.Base(target) != "metadata.json") {
110+
if shouldSkip(target) {
111111
continue
112112
}
113113

114114
linkedFile := filepath.Join(dir, filepath.Clean(filepath.FromSlash(header.Linkname)))
115-
source, err := os.Open(filepath.Clean(linkedFile))
116-
if err != nil {
115+
if err := followLink(linkedFile, target); err != nil {
117116
return err
118117
}
119-
defer source.Close() //nolint:errcheck
118+
}
119+
}
120+
}
120121

121-
// we need to get the lined file info to create the target file with the same permissions
122-
info, err := source.Stat()
123-
if err != nil {
124-
return err
125-
}
122+
// indicates if the file should be skipped during extraction
123+
// we skip csv files and .json except metadata.json
124+
func shouldSkip(target string) bool {
125+
ext := filepath.Ext(target)
126+
return ext == ".csv" || (ext == ".json" && filepath.Base(target) != "metadata.json")
127+
}
126128

127-
file, err := os.OpenFile(target, os.O_CREATE|os.O_WRONLY, info.Mode())
128-
if err != nil {
129-
return err
130-
}
129+
//nolint:forbidigo
130+
func followLink(linkedFile string, target string) error {
131+
source, err := os.Open(filepath.Clean(linkedFile))
132+
if err != nil {
133+
return err
134+
}
135+
defer source.Close() //nolint:errcheck
131136

132-
_, err = io.Copy(file, source)
133-
if err != nil {
134-
return err
135-
}
137+
// we need to get the lined file info to create the target file with the same permissions
138+
info, err := source.Stat()
139+
if err != nil {
140+
return err
141+
}
136142

137-
err = file.Close()
138-
if err != nil {
139-
return err
140-
}
141-
}
143+
file, err := os.OpenFile(target, os.O_CREATE|os.O_WRONLY, info.Mode()) //nolint:gosec
144+
if err != nil {
145+
return err
142146
}
147+
148+
_, err = io.Copy(file, source)
149+
if err != nil {
150+
return err
151+
}
152+
153+
err = file.Close()
154+
if err != nil {
155+
return err
156+
}
157+
return nil
143158
}

0 commit comments

Comments
 (0)