-
Notifications
You must be signed in to change notification settings - Fork 53
/
projectconfig.go
222 lines (197 loc) · 5.84 KB
/
projectconfig.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package project
import (
"errors"
"fmt"
"os"
"path/filepath"
"reflect"
"github.com/jfrog/jfrog-cli-core/v2/utils/config"
"github.com/jfrog/jfrog-cli-core/v2/utils/coreutils"
"github.com/jfrog/jfrog-client-go/utils/errorutils"
"github.com/jfrog/jfrog-client-go/utils/io/fileutils"
"github.com/jfrog/jfrog-client-go/utils/log"
"github.com/spf13/viper"
)
const (
ProjectConfigResolverPrefix = "resolver"
ProjectConfigDeployerPrefix = "deployer"
ProjectConfigRepo = "repo"
ProjectConfigReleaseRepo = "releaseRepo"
ProjectConfigServerId = "serverId"
)
type ProjectType int
const (
Go ProjectType = iota
Pip
Pipenv
Poetry
Npm
Pnpm
Yarn
Nuget
Maven
Gradle
Dotnet
Build
Terraform
)
type ConfigType string
const (
YAML ConfigType = "yaml"
PROPERTIES ConfigType = "properties"
)
var ProjectTypes = []string{
"go",
"pip",
"pipenv",
"poetry",
"npm",
"pnpm",
"yarn",
"nuget",
"maven",
"gradle",
"dotnet",
"build",
"terraform",
}
func (projectType ProjectType) String() string {
return ProjectTypes[projectType]
}
type MissingResolverErr struct {
message string
}
func (mre *MissingResolverErr) Error() string {
return mre.message
}
type Repository struct {
Repo string `yaml:"repo,omitempty"`
ServerId string `yaml:"serverId,omitempty"`
SnapshotRepo string `yaml:"snapshotRepo,omitempty"`
ReleaseRepo string `yaml:"releaseRepo,omitempty"`
DeployMavenDesc bool `yaml:"deployMavenDescriptors,omitempty"`
DeployIvyDesc bool `yaml:"deployIvyDescriptors,omitempty"`
IvyPattern string `yaml:"ivyPattern,omitempty"`
ArtifactsPattern string `yaml:"artifactPattern,omitempty"`
NugetV2 bool `yaml:"nugetV2,omitempty"`
IncludePatterns string `yaml:"includePatterns,omitempty"`
ExcludePatterns string `yaml:"excludePatterns,omitempty"`
}
type RepositoryConfig struct {
targetRepo string
serverDetails *config.ServerDetails
}
// If configuration file exists in the working dir or in one of its parent dirs return its path,
// otherwise return the global configuration file path
func GetProjectConfFilePath(projectType ProjectType) (confFilePath string, exists bool, err error) {
confFileName := filepath.Join("projects", projectType.String()+".yaml")
projectDir, exists, err := fileutils.FindUpstream(".jfrog", fileutils.Dir)
if err != nil {
return
}
if exists {
filePath := filepath.Join(projectDir, ".jfrog", confFileName)
exists, err = fileutils.IsFileExists(filePath, false)
if err != nil {
return
}
if exists {
confFilePath = filePath
return
}
}
// If missing in the root project, check in the home dir
jfrogHomeDir, err := coreutils.GetJfrogHomeDir()
if err != nil {
return
}
filePath := filepath.Join(jfrogHomeDir, confFileName)
exists, err = fileutils.IsFileExists(filePath, false)
if exists {
confFilePath = filePath
}
return
}
func GetRepoConfigByPrefix(configFilePath, prefix string, vConfig *viper.Viper) (repoConfig *RepositoryConfig, err error) {
defer func() {
if err != nil {
err = errors.Join(err, fmt.Errorf("please run 'jf %s-config' with your %s repository information", vConfig.GetString("type"), prefix))
}
}()
if !vConfig.IsSet(prefix) {
err = &MissingResolverErr{fmt.Sprintf("the %s repository is missing from the config file (%s)", prefix, configFilePath)}
return
}
log.Debug(fmt.Sprintf("Found %s in the config file %s", prefix, configFilePath))
repo := vConfig.GetString(prefix + "." + ProjectConfigRepo)
if repo == "" {
// In the maven.yaml config, there's a resolver repository field named "releaseRepo"
if repo = vConfig.GetString(prefix + "." + ProjectConfigReleaseRepo); repo == "" {
err = errorutils.CheckErrorf("missing repository for %s within %s", prefix, configFilePath)
return
}
}
serverId := vConfig.GetString(prefix + "." + ProjectConfigServerId)
if serverId == "" {
err = errorutils.CheckErrorf("missing server ID for %s within %s", prefix, configFilePath)
return
}
rtDetails, err := config.GetSpecificConfig(serverId, false, true)
if err != nil {
return
}
repoConfig = &RepositoryConfig{targetRepo: repo, serverDetails: rtDetails}
return
}
func (repo *RepositoryConfig) IsServerDetailsEmpty() bool {
if repo.serverDetails != nil && reflect.DeepEqual(config.ServerDetails{}, repo.serverDetails) {
return false
}
return true
}
func (repo *RepositoryConfig) SetTargetRepo(targetRepo string) *RepositoryConfig {
repo.targetRepo = targetRepo
return repo
}
func (repo *RepositoryConfig) TargetRepo() string {
return repo.targetRepo
}
func (repo *RepositoryConfig) SetServerDetails(rtDetails *config.ServerDetails) *RepositoryConfig {
repo.serverDetails = rtDetails
return repo
}
func (repo *RepositoryConfig) ServerDetails() (*config.ServerDetails, error) {
return repo.serverDetails, nil
}
func GetResolutionOnlyConfiguration(projectType ProjectType) (*RepositoryConfig, error) {
// Get configuration file path.
confFilePath, exists, err := GetProjectConfFilePath(projectType)
if err != nil {
return nil, err
}
if !exists {
return nil, errorutils.CheckErrorf(projectType.String() + " Project configuration does not exist.")
}
return ReadResolutionOnlyConfiguration(confFilePath)
}
func ReadConfigFile(configPath string, configType ConfigType) (config *viper.Viper, err error) {
config = viper.New()
config.SetConfigType(string(configType))
f, err := os.Open(configPath)
if err != nil {
return config, errorutils.CheckError(err)
}
defer func() {
err = errorutils.CheckError(f.Close())
}()
err = config.ReadConfig(f)
return config, errorutils.CheckError(err)
}
func ReadResolutionOnlyConfiguration(confFilePath string) (*RepositoryConfig, error) {
log.Debug("Preparing to read the config file", confFilePath)
vConfig, err := ReadConfigFile(confFilePath, YAML)
if err != nil {
return nil, err
}
return GetRepoConfigByPrefix(confFilePath, ProjectConfigResolverPrefix, vConfig)
}