/
nuget.go
88 lines (79 loc) 路 2.57 KB
/
nuget.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
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
package core
import (
"bufio"
"fmt"
"github.com/JetBrains/qodana-cli/v2023/cloud"
log "github.com/sirupsen/logrus"
"os"
"path/filepath"
"regexp"
"strings"
)
const (
nugetConfigName = "nuget.config"
nugetConfigNamePascalCase = "NuGet.Config"
)
func unsetNugetVariables() {
variables := []string{qodanaNugetUser, qodanaNugetPassword, qodanaNugetName, qodanaNugetUrl}
for _, variable := range variables {
if err := os.Unsetenv(variable); err != nil {
log.Fatal("couldn't unset env variable ", err.Error())
}
}
}
func warnIfPrivateFeedDetected(projectPath string) {
if Prod.Code != QDNET && Prod.Code != QDNETC || qodanaNugetVarsSet() {
return
}
configFileNames := []string{nugetConfigName, nugetConfigNamePascalCase}
for _, fileName := range configFileNames {
if _, err := os.Stat(filepath.Join(projectPath, fileName)); err == nil {
if checkForPrivateFeed(filepath.Join(projectPath, fileName)) {
_, _ = fmt.Fprintf(os.Stderr, "\nWarning: private NuGet feed detected. Please set %s, %s, %s and %s (optional) environment variables to provide credentials for the private feed.\n",
qodanaNugetUser, qodanaNugetPassword, qodanaNugetUrl, qodanaNugetName)
}
}
}
}
func checkForPrivateFeed(fileName string) bool {
file, err := os.Open(fileName)
if err != nil {
fmt.Println("Failed to open file:", fileName)
return false
}
defer func(file *os.File) {
err := file.Close()
if err != nil {
log.Error("couldn't close file ", err.Error())
}
}(file)
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
if strings.Contains(line, "<add ") {
match, _ := regexp.MatchString(`http(s)?://`, line)
return match
}
}
return false
}
func isNugetConfigNeeded() bool {
return IsContainer() && qodanaNugetVarsSet()
}
func qodanaNugetVarsSet() bool {
return os.Getenv(qodanaNugetUrl) != "" && os.Getenv(qodanaNugetUser) != "" && os.Getenv(qodanaNugetPassword) != ""
}
func prepareNugetConfig(userPath string) {
nugetConfig := filepath.Join(userPath, ".nuget", "NuGet")
if _, err := os.Stat(nugetConfig); err != nil {
// mkdir -p ~/.nuget/NuGet
if err := os.MkdirAll(nugetConfig, os.ModePerm); err != nil {
log.Fatal("couldn't create a directory ", err.Error())
}
}
nugetConfig = filepath.Join(nugetConfig, "NuGet.Config")
config := nugetWithPrivateFeed(cloud.GetEnvWithDefault(qodanaNugetName, "qodana"), os.Getenv(qodanaNugetUrl), os.Getenv(qodanaNugetUser), os.Getenv(qodanaNugetPassword))
if err := os.WriteFile(nugetConfig, []byte(config), 0644); err != nil {
log.Fatal("couldn't create a file ", err.Error())
}
}