-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathutils.go
More file actions
99 lines (77 loc) · 2.14 KB
/
Copy pathutils.go
File metadata and controls
99 lines (77 loc) · 2.14 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
Copyright (c) nexB Inc. and others. All rights reserved.
ScanCode is a trademark of nexB Inc.
SPDX-License-Identifier: Apache-2.0
See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
See https://github.com/nexB/dependency-inspector for support or download.
See https://aboutcode.org for more information about nexB OSS projects.
*/
package internal
import (
"fmt"
"os"
"os/exec"
"path/filepath"
)
func CreateLockFile(lockFiles []string, cmdArgs []string, lockGenCmd []string, outputFileName string, forced bool) {
path := "."
if len(cmdArgs) > 0 {
path = cmdArgs[0]
}
absPath, err := filepath.Abs(path)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: Failed to retrieve absolute path: %v\n", err)
os.Exit(1)
}
if !forced {
for _, lockFile := range lockFiles {
lockFileAbsPath := filepath.Join(absPath, lockFile)
if res := DoesFileExists(lockFileAbsPath); !res {
continue
}
return
}
}
genLock(lockGenCmd, absPath, outputFileName)
}
func DoesFileExists(absPath string) bool {
if _, err := os.Stat(absPath); err == nil {
cwd, err := os.Getwd()
if err != nil {
fmt.Fprintln(os.Stderr, "Error:", err)
os.Exit(1)
}
relPath, err := filepath.Rel(cwd, absPath)
if err != nil {
fmt.Fprintln(os.Stderr, "Error:", err)
os.Exit(1)
}
fmt.Printf("Lockfile '%s' already present.\n", relPath)
return true
}
return false
}
func genLock(lockGenCmd []string, absPath string, outputFileName string) {
fmt.Printf("Generating lockfile using '%s'\n", lockGenCmd)
// #nosec G204
command := exec.Command(lockGenCmd[0], lockGenCmd[1:]...)
command.Dir = absPath
command.Stderr = os.Stderr
command.Stdout = os.Stdout
if outputFileName != "" {
outputPath := filepath.Join(absPath, outputFileName)
// #nosec G304
outputFile, err := os.Create(outputPath)
if err != nil {
fmt.Fprintln(os.Stderr, "Error: failed to create output file: ", err)
os.Exit(1)
}
defer outputFile.Close()
command.Stdout = outputFile
}
if err := command.Run(); err != nil {
fmt.Fprintln(os.Stderr, "Error: Failed to generate lockfile: ", err)
return
}
fmt.Println("Lock file generated successfully.")
}