github.com/jfrog/jfrog-cli-core@v1.12.1/artifactory/utils/projectconfig.go (about) 1 package utils 2 3 import ( 4 "fmt" 5 "path/filepath" 6 "reflect" 7 8 "github.com/jfrog/jfrog-cli-core/utils/coreutils" 9 10 "github.com/jfrog/jfrog-cli-core/utils/config" 11 "github.com/jfrog/jfrog-client-go/utils/errorutils" 12 "github.com/jfrog/jfrog-client-go/utils/io/fileutils" 13 "github.com/jfrog/jfrog-client-go/utils/log" 14 "github.com/spf13/viper" 15 ) 16 17 const ( 18 ProjectConfigResolverPrefix = "resolver" 19 ProjectConfigDeployerPrefix = "deployer" 20 ProjectConfigRepo = "repo" 21 ProjectConfigServerId = "serverId" 22 ) 23 24 type ProjectType int 25 26 const ( 27 Go ProjectType = iota 28 Pip 29 Npm 30 Yarn 31 Nuget 32 Maven 33 Gradle 34 Dotnet 35 ) 36 37 var ProjectTypes = []string{ 38 "go", 39 "pip", 40 "npm", 41 "yarn", 42 "nuget", 43 "maven", 44 "gradle", 45 "dotnet", 46 } 47 48 func (projectType ProjectType) String() string { 49 return ProjectTypes[projectType] 50 } 51 52 type Repository struct { 53 Repo string `yaml:"repo,omitempty"` 54 ServerId string `yaml:"serverId,omitempty"` 55 SnapshotRepo string `yaml:"snapshotRepo,omitempty"` 56 ReleaseRepo string `yaml:"releaseRepo,omitempty"` 57 DeployMavenDesc bool `yaml:"deployMavenDescriptors,omitempty"` 58 DeployIvyDesc bool `yaml:"deployIvyDescriptors,omitempty"` 59 IvyPattern string `yaml:"ivyPattern,omitempty"` 60 ArtifactsPattern string `yaml:"artifactPattern,omitempty"` 61 NugetV2 bool `yaml:"nugetV2,omitempty"` 62 IncludePatterns string `yaml:"includePatterns,omitempty"` 63 ExcludePatterns string `yaml:"excludePatterns,omitempty"` 64 } 65 66 type RepositoryConfig struct { 67 targetRepo string 68 serverDetails *config.ServerDetails 69 } 70 71 // If configuration file exists in the working dir or in one of its parent dirs return its path, 72 // otherwise return the global configuration file path 73 func GetProjectConfFilePath(projectType ProjectType) (confFilePath string, exists bool, err error) { 74 confFileName := filepath.Join("projects", projectType.String()+".yaml") 75 projectDir, exists, err := fileutils.FindUpstream(".jfrog", fileutils.Dir) 76 if err != nil { 77 return "", false, err 78 } 79 if exists { 80 confFilePath = filepath.Join(projectDir, ".jfrog", confFileName) 81 exists, err = fileutils.IsFileExists(confFilePath, false) 82 if err != nil { 83 return "", false, err 84 } 85 86 if exists { 87 return 88 } 89 } 90 // If missing in the root project, check in the home dir 91 jfrogHomeDir, err := coreutils.GetJfrogHomeDir() 92 if err != nil { 93 return "", exists, err 94 } 95 confFilePath = filepath.Join(jfrogHomeDir, confFileName) 96 exists, err = fileutils.IsFileExists(confFilePath, false) 97 return 98 } 99 100 func GetRepoConfigByPrefix(configFilePath, prefix string, vConfig *viper.Viper) (*RepositoryConfig, error) { 101 if !vConfig.IsSet(prefix) { 102 return nil, errorutils.CheckError(fmt.Errorf("%s information is missing within %s", prefix, configFilePath)) 103 } 104 log.Debug(fmt.Sprintf("Found %s in the config file %s", prefix, configFilePath)) 105 repo := vConfig.GetString(prefix + "." + ProjectConfigRepo) 106 if repo == "" { 107 return nil, fmt.Errorf("Missing repository for %s within %s", prefix, configFilePath) 108 } 109 serverId := vConfig.GetString(prefix + "." + ProjectConfigServerId) 110 if serverId == "" { 111 return nil, fmt.Errorf("Missing server ID for %s within %s", prefix, configFilePath) 112 } 113 rtDetails, err := config.GetSpecificConfig(serverId, false, true) 114 if err != nil { 115 return nil, err 116 } 117 return &RepositoryConfig{targetRepo: repo, serverDetails: rtDetails}, nil 118 } 119 120 func (repo *RepositoryConfig) IsServerDetailsEmpty() bool { 121 if repo.serverDetails != nil && reflect.DeepEqual(config.ServerDetails{}, repo.serverDetails) { 122 return false 123 } 124 return true 125 } 126 127 func (repo *RepositoryConfig) SetTargetRepo(targetRepo string) *RepositoryConfig { 128 repo.targetRepo = targetRepo 129 return repo 130 } 131 132 func (repo *RepositoryConfig) TargetRepo() string { 133 return repo.targetRepo 134 } 135 136 func (repo *RepositoryConfig) SetServerDetails(rtDetails *config.ServerDetails) *RepositoryConfig { 137 repo.serverDetails = rtDetails 138 return repo 139 } 140 141 func (repo *RepositoryConfig) ServerDetails() (*config.ServerDetails, error) { 142 return repo.serverDetails, nil 143 } 144 145 func GetResolutionOnlyConfiguration(projectType ProjectType) (*RepositoryConfig, error) { 146 // Get configuration file path. 147 confFilePath, exists, err := GetProjectConfFilePath(projectType) 148 if err != nil { 149 return nil, err 150 } 151 if !exists { 152 return nil, errorutils.CheckError(fmt.Errorf(projectType.String() + " Project configuration does not exist.")) 153 } 154 return ReadResolutionOnlyConfiguration(confFilePath) 155 } 156 157 func ReadResolutionOnlyConfiguration(confFilePath string) (*RepositoryConfig, error) { 158 log.Debug("Preparing to read the config file", confFilePath) 159 vConfig, err := ReadConfigFile(confFilePath, YAML) 160 if err != nil { 161 return nil, err 162 } 163 return GetRepoConfigByPrefix(confFilePath, ProjectConfigResolverPrefix, vConfig) 164 }