-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinit.go
62 lines (52 loc) · 1.42 KB
/
init.go
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
package init
import (
"errors"
"io"
"os"
"path/filepath"
)
// Run function performs the copying of specific files and symlink creation
func Run(srcDir, destDir string) error {
// Copy the orchestrator binary
orchestratorSrc := filepath.Join(srcDir, "orchestrator")
orchestratorDest := filepath.Join(destDir, "orchestrator")
if err := copyFile(orchestratorSrc, orchestratorDest); err != nil {
return err
}
// Copy the task agent binary
agentSrc := filepath.Join(srcDir, "circleci-agent")
agentDest := filepath.Join(destDir, "circleci-agent")
if err := copyFile(agentSrc, agentDest); err != nil {
return err
}
// Create symbolic link from "circleci-agent" to "circleci"
if err := os.Symlink(agentDest, filepath.Join(destDir, "circleci")); err != nil {
return err
}
return nil
}
func copyFile(srcPath, destPath string) (err error) {
closeFile := func(f *os.File) {
err = errors.Join(err, f.Close())
}
srcFile, err := os.Open(srcPath) //#nosec:G304 // this is trusted input
if err != nil {
return err
}
defer closeFile(srcFile)
// Get the file info to preserve the permissions
info, err := srcFile.Stat()
if err != nil {
return err
}
//#nosec:G304 // this is trusted output
destFile, err := os.OpenFile(destPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, info.Mode())
if err != nil {
return err
}
defer closeFile(destFile)
if _, err = io.Copy(destFile, srcFile); err != nil {
return err
}
return err
}