Skip to content

Commit 604e2f2

Browse files
committed
address performance improvement feedback from GuardRails team
1 parent 36a4234 commit 604e2f2

File tree

4 files changed

+50
-34
lines changed

4 files changed

+50
-34
lines changed

internal/archiver/archiver.go

Lines changed: 44 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -76,44 +76,58 @@ func compress(projectPath string, filepaths []string, output io.Writer) (err err
7676
}()
7777

7878
for _, path := range filepaths {
79-
// path variables only contains relative path of the project root, so we need to append project path to get absolute path.
80-
// path/filepath packages works cross platform which will use appropriate file separator based on OS.
81-
fullpath := filepath.Join(projectPath, path)
82-
83-
file, err := os.Open(fullpath)
79+
isDir, err := compressFile(projectPath, path, tarWriter)
8480
if err != nil {
8581
return err
8682
}
87-
defer func() {
88-
closeErr := file.Close()
89-
if err == nil {
90-
err = closeErr
91-
}
92-
}()
93-
94-
fileInfo, err := file.Stat()
95-
if err != nil {
96-
return err
97-
}
98-
if fileInfo.IsDir() {
83+
if isDir {
9984
continue
10085
}
86+
}
10187

102-
header, err := tar.FileInfoHeader(fileInfo, fileInfo.Name())
103-
if err != nil {
104-
return err
105-
}
106-
header.Name = strings.Replace(strings.TrimPrefix(path, string(filepath.Separator)), "\\", "/", -1)
107-
err = tarWriter.WriteHeader(header)
108-
if err != nil {
109-
return err
110-
}
88+
return nil
89+
}
11190

112-
_, err = io.Copy(tarWriter, file)
113-
if err != nil {
114-
return err
91+
func compressFile(projectPath, path string, writer *tar.Writer) (bool, error) {
92+
// path variables only contains relative path of the project root, so we need to append project path to get absolute path.
93+
// path/filepath packages works cross platform which will use appropriate file separator based on OS.
94+
fullpath := filepath.Join(projectPath, path)
95+
96+
file, err := os.Open(fullpath)
97+
if err != nil {
98+
return false, err
99+
}
100+
101+
defer func() {
102+
closeErr := file.Close()
103+
if err == nil {
104+
err = closeErr
115105
}
106+
}()
107+
108+
fileInfo, err := file.Stat()
109+
if err != nil {
110+
return false, err
111+
}
112+
if fileInfo.IsDir() {
113+
return true, nil
116114
}
117115

118-
return nil
116+
header, err := tar.FileInfoHeader(fileInfo, fileInfo.Name())
117+
if err != nil {
118+
return false, err
119+
}
120+
121+
header.Name = strings.Replace(strings.TrimPrefix(path, string(filepath.Separator)), "\\", "/", -1)
122+
err = writer.WriteHeader(header)
123+
if err != nil {
124+
return false, err
125+
}
126+
127+
_, err = io.Copy(writer, file)
128+
if err != nil {
129+
return false, err
130+
}
131+
132+
return false, nil
119133
}

internal/client/guardrails/message.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ type GetScanDataRuleResp struct {
9595
Name string `json:"name"`
9696
Docs string `json:"docs"`
9797
} `json:"rule"`
98-
Languages []string `json:"languages"`
98+
Languages []string `json:"language"`
9999
Count *GetScanDataCountResp `json:"count"`
100100
Vulnerabilities []GetScanDataVulnerabilitiesResp `json:"vulnerabilities"`
101101
}

internal/command/scan/args.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ const (
1717
)
1818

1919
var (
20-
ErrMissingToken = errors.New("missing token, please provide your Guardrails CLI token via -—token option or GUARDRAILS_CLI_TOKEN environment variable")
20+
ErrMissingToken = errors.New("missing token, please provide your Guardrails CLI token via -—token option or GUARDRAILS_CLI_TOKEN environment variable")
21+
ErrInvalidFormatParam = errors.New("failed to parse format value")
2122
)
2223

2324
// Args provides arguments for scan command handler.
@@ -49,7 +50,7 @@ func (args *Args) SetDefault() error {
4950
func isFormatAllowed(value interface{}) error {
5051
input, ok := value.(string)
5152
if !ok {
52-
return errors.New("failed to parse format value")
53+
return ErrInvalidFormatParam
5354
}
5455

5556
allowedFormat := []string{FormatJSON, FormatCSV, FormatSARIF, FormatPretty}

internal/repository/repository.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ var (
1717
ErrNotAValidGitRepo = func(path string) error {
1818
return fmt.Errorf("%s is not a valid git repository", path)
1919
}
20+
ErrGitRemoteURLNotFound = errors.New("repository doesn't have remote URLs")
2021
)
2122

2223
//go:generate mockgen -destination=mock/repository.go -package=mockrepository . Repository
@@ -71,7 +72,7 @@ func (r *repository) GetMetadataFromRemoteURL() (*Metadata, error) {
7172
// TODO: currently we only take first remote URL from origin. It could be expanded later since git can have multiple remote urls.
7273
remoteURLs := cfg.Remotes["origin"].URLs
7374
if len(remoteURLs) == 0 {
74-
return nil, errors.New("repository doesn't have remote URLs")
75+
return nil, ErrGitRemoteURLNotFound
7576
}
7677

7778
remoteURL := remoteURLs[0]

0 commit comments

Comments
 (0)