github.com/jfrog/jfrog-cli-core/v2@v2.51.0/artifactory/commands/utils/precheckrunner/repositorynamingchecker.go (about)

     1  package precheckrunner
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"time"
     7  
     8  	commandUtils "github.com/jfrog/jfrog-cli-core/v2/artifactory/commands/utils"
     9  
    10  	"github.com/jfrog/jfrog-cli-core/v2/artifactory/utils"
    11  	"github.com/jfrog/jfrog-client-go/artifactory/services"
    12  	"github.com/jfrog/jfrog-client-go/utils/log"
    13  )
    14  
    15  const (
    16  	repositoryNamingCheckName        = "Repositories naming"
    17  	illegalDockerRepositoryKeyReason = "Docker repository keys in JFrog Cloud are not allowed to include '.' or '_' characters."
    18  )
    19  
    20  type illegalRepositoryKeys struct {
    21  	RepoKey string `json:"repo_key,omitempty"`
    22  	Reason  string `json:"reason,omitempty"`
    23  }
    24  
    25  // Run repository naming check before transferring configuration from one Artifactory to another
    26  type RepositoryNamingCheck struct {
    27  	selectedRepos map[utils.RepoType][]services.RepositoryDetails
    28  }
    29  
    30  func NewRepositoryNamingCheck(selectedRepos map[utils.RepoType][]services.RepositoryDetails) *RepositoryNamingCheck {
    31  	return &RepositoryNamingCheck{selectedRepos}
    32  }
    33  
    34  func (drc *RepositoryNamingCheck) Name() string {
    35  	return repositoryNamingCheckName
    36  }
    37  
    38  func (drc *RepositoryNamingCheck) ExecuteCheck(args RunArguments) (passed bool, err error) {
    39  	results := drc.getIllegalRepositoryKeys()
    40  	if len(results) == 0 {
    41  		return true, nil
    42  	}
    43  
    44  	return false, handleFailuresInRepositoryKeysRun(results)
    45  }
    46  
    47  func (drc *RepositoryNamingCheck) getIllegalRepositoryKeys() []illegalRepositoryKeys {
    48  	var results []illegalRepositoryKeys
    49  	for _, repositoriesOfType := range drc.selectedRepos {
    50  		for _, repository := range repositoriesOfType {
    51  			if strings.ToLower(repository.PackageType) == "docker" && strings.ContainsAny(repository.Key, "_.") {
    52  				log.Debug("Found Docker repository with illegal characters:", repository.Key)
    53  				results = append(results, illegalRepositoryKeys{
    54  					RepoKey: repository.Key,
    55  					Reason:  illegalDockerRepositoryKeyReason,
    56  				})
    57  			}
    58  		}
    59  	}
    60  	return results
    61  }
    62  
    63  // Create CSV summary of all the files with illegal repository keys and log the result
    64  func handleFailuresInRepositoryKeysRun(illegalDockerRepositoryKeys []illegalRepositoryKeys) (err error) {
    65  	// Create summary
    66  	csvPath, err := commandUtils.CreateCSVFile("illegal-repository-keys", illegalDockerRepositoryKeys, time.Now())
    67  	if err != nil {
    68  		log.Error("Couldn't create the illegal repository keys CSV file", err)
    69  		return
    70  	}
    71  	// Log result
    72  	log.Info(fmt.Sprintf("Found %d illegal repository keys. Check the summary CSV file in: %s", len(illegalDockerRepositoryKeys), csvPath))
    73  	return
    74  }