github.com/jfrog/jfrog-cli-go@v1.22.1-0.20200318093948-4826ef344ffd/artifactory/utils/projectconfig.go (about)

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