-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathsrcarchive.go
More file actions
84 lines (74 loc) · 1.94 KB
/
srcarchive.go
File metadata and controls
84 lines (74 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package srcarchive
import (
"errors"
"io"
"log"
"log/slog"
"os"
"path/filepath"
"strings"
)
var pathTransformer *ProjectLayout
func init() {
pt, err := LoadProjectLayoutFromEnv()
if err == nil {
pathTransformer = pt
if pathTransformer != nil {
slog.Info("Loaded path transformer", "from", pathTransformer.From, "to", pathTransformer.To)
}
} else {
log.Fatalf("Unable to load path transformer: %s.\n", err.Error())
}
}
// Add inserts the file with the given `path` into the source archive, returning a non-nil
// error value if it fails
func Add(path string) error {
srcArchive, err := srcArchive()
if err != nil {
return err
}
file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()
archiveFilePath := filepath.Join(srcArchive, AppendablePath(path))
err = os.MkdirAll(filepath.Dir(archiveFilePath), 0755)
if err != nil {
return err
}
archiveFile, err := os.Create(archiveFilePath)
if err != nil {
return err
}
defer archiveFile.Close()
_, err = io.Copy(archiveFile, file)
return err
}
func srcArchive() (string, error) {
srcArchive := os.Getenv("CODEQL_EXTRACTOR_GO_SOURCE_ARCHIVE_DIR")
if srcArchive == "" {
srcArchive = os.Getenv("SOURCE_ARCHIVE")
}
if srcArchive == "" {
return "", errors.New("environment variable CODEQL_EXTRACTOR_GO_SOURCE_ARCHIVE_DIR not set")
}
err := os.MkdirAll(srcArchive, 0755)
if err != nil {
return "", err
}
return srcArchive, nil
}
// TransformPath applies the transformations specified by `CODEQL_PATH_TRANSFORMER` (if any) to the
// given path
func TransformPath(path string) string {
if pathTransformer != nil {
return filepath.FromSlash(pathTransformer.Transform(filepath.ToSlash(path)))
}
return path
}
// AppendablePath transforms the given path and also replaces colons with underscores to make it
// possible to append it to a base path on Windows
func AppendablePath(path string) string {
return strings.ReplaceAll(TransformPath(path), ":", "_")
}