Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
TamirHadad committed Jul 11, 2017
2 parents 76711c7 + 974eb44 commit 4e98831
Show file tree
Hide file tree
Showing 81 changed files with 1,887 additions and 659 deletions.
425 changes: 299 additions & 126 deletions artifactory/cli.go

Large diffs are not rendered by default.

20 changes: 11 additions & 9 deletions artifactory/commands/buildpublish.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ func createBuildInfo(buildName, buildNumber string, buildInfoRawData utils.Build
if len(env) != 0 {
buildInfo.Properties = env
}
buildInfo.artifactoryPrincipal = flags.ArtDetails.User
if vcs != (utils.Vcs{}) {
buildInfo.VcsRevision = vcs.VcsRevision
buildInfo.VcsUrl = vcs.VcsUrl
Expand Down Expand Up @@ -134,15 +135,16 @@ func createModule(buildName string, artifacts []utils.ArtifactsBuildInfo, depend
}

type BuildInfo struct {
Name string `json:"name,omitempty"`
Number string `json:"number,omitempty"`
Agent *CliAgent `json:"agent,omitempty"`
BuildAgent *CliAgent `json:"buildAgent,omitempty"`
Modules []*Modules `json:"modules,omitempty"`
Started string `json:"started,omitempty"`
Properties utils.BuildEnv `json:"properties,omitempty"`
VcsUrl string `json:"vcsUrl,omitempty"`
VcsRevision string `json:"vcsRevision,omitempty"`
Name string `json:"name,omitempty"`
Number string `json:"number,omitempty"`
Agent *CliAgent `json:"agent,omitempty"`
BuildAgent *CliAgent `json:"buildAgent,omitempty"`
Modules []*Modules `json:"modules,omitempty"`
Started string `json:"started,omitempty"`
Properties utils.BuildEnv `json:"properties,omitempty"`
artifactoryPrincipal string `json:"artifactoryPrincipal,omitempty"`
VcsUrl string `json:"vcsUrl,omitempty"`
VcsRevision string `json:"vcsRevision,omitempty"`
}

type CliAgent struct {
Expand Down
53 changes: 39 additions & 14 deletions artifactory/commands/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ shouldEncPassword bool, serverId string) (*config.ArtifactoryDetails, error) {
return nil, err
}
}
serverId = resolveServerId(serverId, details, defaultDetails)
err = checkSingleAuthMethod(details)
if err != nil {
return nil, err
Expand All @@ -37,11 +38,21 @@ shouldEncPassword bool, serverId string) (*config.ArtifactoryDetails, error) {
return nil, err
}
}
copyDetails(details, defaultDetails)
populateConfigDetails(serverId, details, defaultDetails)
err = config.SaveArtifactoryConf(configurations)
return details, err
}

func populateConfigDetails(serverId string, details, defaultDetails *config.ArtifactoryDetails) {
if defaultDetails == nil {
defaultDetails = new(config.ArtifactoryDetails)
}
isDefault := defaultDetails.IsDefault
*defaultDetails = *details
defaultDetails.IsDefault = isDefault
defaultDetails.ServerId = serverId
}

func prepareConfigurationData(serverId string, details, defaultDetails *config.ArtifactoryDetails) (*config.ArtifactoryDetails, *config.ArtifactoryDetails, []*config.ArtifactoryDetails, error) {
configurations, err := config.GetAllArtifactoryConfigs()
if err != nil {
Expand All @@ -61,6 +72,24 @@ func prepareConfigurationData(serverId string, details, defaultDetails *config.A
return details, defaultDetails, configurations, err
}

/// Returning the first non empty value:
// 1. The serverId argument sent.
// 2. details.ServerId
// 3. defaultDetails.ServerId
// 4. config.DefaultServerId
func resolveServerId(serverId string, details *config.ArtifactoryDetails, defaultDetails *config.ArtifactoryDetails) string {
if serverId != "" {
return serverId
}
if details.ServerId != "" {
return details.ServerId
}
if defaultDetails.ServerId != "" {
return defaultDetails.ServerId
}
return config.DefaultServerId
}

func handleEmptyDefaultDetails(serverId string, configurations []*config.ArtifactoryDetails, details *config.ArtifactoryDetails) (*config.ArtifactoryDetails, []*config.ArtifactoryDetails, *config.ArtifactoryDetails, error) {
var defaultDetails *config.ArtifactoryDetails
var err error
Expand All @@ -77,8 +106,6 @@ func handleEmptyDefaultDetails(serverId string, configurations []*config.Artifac
if defaultDetails.IsEmpty() {
defaultDetails.IsDefault = len(configurations) == 0
configurations = append(configurations, defaultDetails)
// The new server details should have the serverId the user configured
details.ServerId = serverId
}
}
return defaultDetails, configurations, details, err
Expand Down Expand Up @@ -107,15 +134,6 @@ func getConfigurationFromUser(details, defaultDetails *config.ArtifactoryDetails
return nil
}

func copyDetails(src, dst *config.ArtifactoryDetails) {
if dst == nil {
dst = new(config.ArtifactoryDetails)
}
isDefault := dst.IsDefault
*dst = *src
dst.IsDefault = isDefault
}

func readSshKeyPathFromConsole(details, savedDetails *config.ArtifactoryDetails) error {
if details.SshKeyPath == "" {
ioutils.ScanFromConsole("SSH key file path", &details.SshKeyPath, savedDetails.SshKeyPath)
Expand Down Expand Up @@ -197,6 +215,7 @@ func DeleteConfig(serverName string) error {
if isFoundName {
return config.SaveArtifactoryConf(configurations)
}
log.Info("\"" + serverName + "\" configuration could not be found.\n")
return nil
}

Expand Down Expand Up @@ -228,7 +247,13 @@ func Use(serverName string) error {
return nil
}

func ClearConfig() {
func ClearConfig(interactive bool) {
if interactive {
confirmed := cliutils.InteractiveConfirm("Are you sure you want to delete all the configurations?")
if !confirmed {
return
}
}
config.SaveArtifactoryConf(make([]*config.ArtifactoryDetails, 0))
}

Expand Down Expand Up @@ -261,7 +286,7 @@ func encryptPassword(details *config.ArtifactoryDetails) (*config.ArtifactoryDet

func checkSingleAuthMethod(details *config.ArtifactoryDetails) (err error) {
boolArr := []bool{details.User != "" && details.Password != "", details.ApiKey != "", details.SshKeyPath != ""}
if (cliutils.SumTrueValues(boolArr) > 1) {
if cliutils.SumTrueValues(boolArr) > 1 {
err = cliutils.CheckError(errors.New("Only one authentication method is allowd: Username/Password, API key or RSA tokens."))
}
return
Expand Down
10 changes: 5 additions & 5 deletions artifactory/commands/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"errors"
)

func Delete(deleteSpec *utils.SpecFiles, flags utils.CommonFlag) (err error) {
func Delete(deleteSpec *utils.SpecFiles, flags utils.CommonFlags) (err error) {
err = utils.PreCommandSetup(flags)
if err != nil {
return
Expand All @@ -25,14 +25,14 @@ func Delete(deleteSpec *utils.SpecFiles, flags utils.CommonFlag) (err error) {
return
}

func GetPathsToDelete(deleteSpec *utils.SpecFiles, flags utils.CommonFlag) ([]utils.AqlSearchResultItem, error) {
func GetPathsToDelete(deleteSpec *utils.SpecFiles, flags utils.CommonFlags) ([]utils.AqlSearchResultItem, error) {
if err := utils.PreCommandSetup(flags); err != nil {
return nil, err
}
return getPathsToDeleteInternal(deleteSpec, flags)
}

func getPathsToDeleteInternal(deleteSpec *utils.SpecFiles, flags utils.CommonFlag) (resultItems []utils.AqlSearchResultItem, err error) {
func getPathsToDeleteInternal(deleteSpec *utils.SpecFiles, flags utils.CommonFlags) (resultItems []utils.AqlSearchResultItem, err error) {
log.Info("Searching artifacts...")
for i := 0; i < len(deleteSpec.Files); i++ {
currentSpec := deleteSpec.Get(i)
Expand Down Expand Up @@ -60,14 +60,14 @@ func getPathsToDeleteInternal(deleteSpec *utils.SpecFiles, flags utils.CommonFla
return
}

func DeleteFiles(resultItems []utils.AqlSearchResultItem, flags utils.CommonFlag) error {
func DeleteFiles(resultItems []utils.AqlSearchResultItem, flags utils.CommonFlags) error {
if err := utils.PreCommandSetup(flags); err != nil {
return err
}
return deleteFiles(resultItems, flags)
}

func deleteFiles(resultItems []utils.AqlSearchResultItem, flags utils.CommonFlag) error {
func deleteFiles(resultItems []utils.AqlSearchResultItem, flags utils.CommonFlags) error {
for _, v := range resultItems {
fileUrl, err := utils.BuildArtifactoryUrl(flags.GetArtifactoryDetails().Url, v.GetFullUrl(), make(map[string]string))
if err != nil {
Expand Down
14 changes: 7 additions & 7 deletions artifactory/commands/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"path/filepath"
"os"
"sort"
"github.com/jfrogdev/jfrog-cli-go/errors/httperrors"
)

func Download(downloadSpec *utils.SpecFiles, flags *DownloadFlags) (err error) {
Expand Down Expand Up @@ -80,7 +81,7 @@ func prepareTasks(producer parallel.Runner, downloadSpec *utils.SpecFiles, fileC
return
}

err = produceTasks(resultItems, fileSpec, producer, fileContextHandler, errorsQueue)
err = produceTasks(resultItems, fileSpec, producer, fileContextHandler, errorsQueue)
if err != nil {
errorsQueue.AddError(err)
return
Expand Down Expand Up @@ -228,7 +229,8 @@ func downloadFile(downloadFileDetails *DownloadFileDetails, logMsgPrefix string,
}
if bulkDownload {
resp, err := httputils.DownloadFile(downloadFileDetails.DownloadPath, downloadFileDetails.LocalPath, downloadFileDetails.LocalFileName, httpClientsDetails)
if err != nil {
// Ignore response status errors to continue downloading
if err != nil && !httperrors.IsResponseStatusError(err) {
return err
}
log.Debug(logMsgPrefix, "Artifactory response:", resp.Status)
Expand Down Expand Up @@ -268,10 +270,7 @@ func shouldDownloadFile(localFilePath, md5, sha1 string) (bool, error) {
if err != nil {
return false, err
}
if localFileDetails.Md5 != md5 || localFileDetails.Sha1 != sha1 {
return true, nil
}
return false, nil
return localFileDetails.Checksum.Md5 != md5 || localFileDetails.Checksum.Sha1 != sha1, nil
}

func removeIfSymlink(localSymlinkPath string) error {
Expand Down Expand Up @@ -338,6 +337,7 @@ func getArtifactSymlinkChecksum(properties []utils.Property) string {
}

type fileHandlerFunc func(DownloadData) parallel.TaskFunc

func createFileHandlerFunc(buildDependencies [][]utils.DependenciesBuildInfo, flags *DownloadFlags) fileHandlerFunc {
return func(downloadData DownloadData) parallel.TaskFunc {
return func(threadId int) error {
Expand Down Expand Up @@ -378,7 +378,7 @@ func createFileHandlerFunc(buildDependencies [][]utils.DependenciesBuildInfo, fl
}

func downloadFileIfNeeded(downloadPath, localPath, localFileName, logMsgPrefix string, downloadData DownloadData, flags *DownloadFlags) error {
shouldDownload, e := shouldDownloadFile(path.Join(localPath, downloadData.Dependency.Name), downloadData.Dependency.Actual_Md5, downloadData.Dependency.Actual_Sha1)
shouldDownload, e := shouldDownloadFile(path.Join(localPath, localFileName), downloadData.Dependency.Actual_Md5, downloadData.Dependency.Actual_Sha1)
if e != nil {
return e
}
Expand Down
46 changes: 14 additions & 32 deletions artifactory/commands/gitlfsclean.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,41 +19,35 @@ import (
"gopkg.in/src-d/go-git.v4/plumbing/object"
)

func GitLfsClean(gitPath string, flags *GitLfsCleanFlags) error {
func PrepareGitLfsClean(gitPath string, flags *GitLfsCleanFlags) ([]utils.AqlSearchResultItem, *GitLfsCleanFlags, error) {
var err error
repo := flags.Repo
if gitPath == "" {
gitPath, err = os.Getwd()
if err != nil {
return cliutils.CheckError(err)
return nil, nil, cliutils.CheckError(err)
}
}
if len(repo) <= 0 {
repo, err = detectRepo(gitPath, flags.ArtDetails.Url)
if err != nil {
return err
return nil, nil, err
}
}
log.Info("Searching files from Artifactory repository", repo, "...")
refsRegex := getRefsRegex(flags.Refs)
artifactoryLfsFiles, err := searchLfsFilesInArtifactory(repo, flags)
if err != nil {
return cliutils.CheckError(err)
return nil, nil, cliutils.CheckError(err)
}
log.Info("Collecting files to preserve from Git references matching the pattern", flags.Refs, "...")
gitLfsFiles, err := getLfsFilesFromGit(gitPath, refsRegex)
if err != nil {
return cliutils.CheckError(err)
return nil, nil, cliutils.CheckError(err)
}
filesToDelete := findFilesToDelete(artifactoryLfsFiles, gitLfsFiles)
log.Info("Found", len(gitLfsFiles), "files to keep, and", len(filesToDelete), "to clean")
if confirmDelete(filesToDelete, flags.Quiet) {
err = deleteLfsFilesFromArtifactory(repo, filesToDelete, flags)
if err != nil {
return cliutils.CheckError(err)
}
}
return nil
return filesToDelete, flags, nil
}

func lfsConfigUrlExtractor (conf *gitconfig.Config) (*url.URL, error) {
Expand Down Expand Up @@ -121,7 +115,7 @@ func getRefsRegex(refs string) string {
return replacer.Replace(regexp.QuoteMeta(refs))
}

func searchLfsFilesInArtifactory(repo string, flags utils.CommonFlag) ([]utils.AqlSearchResultItem, error) {
func searchLfsFilesInArtifactory(repo string, flags utils.CommonFlags) ([]utils.AqlSearchResultItem, error) {
err := utils.PreCommandSetup(flags)
if err != nil {
return nil, err
Expand All @@ -130,9 +124,13 @@ func searchLfsFilesInArtifactory(repo string, flags utils.CommonFlag) ([]utils.A
return utils.AqlSearchDefaultReturnFields(spec.Get(0), flags)
}

func deleteLfsFilesFromArtifactory(repo string, files []utils.AqlSearchResultItem, flags utils.CommonFlag) error {
log.Info("Deleting", len(files), "files from", repo, "...")
return DeleteFiles(files, flags)
func DeleteLfsFilesFromArtifactory(files []utils.AqlSearchResultItem, flags *GitLfsCleanFlags) error {
log.Info("Deleting", len(files), "files from", flags.Repo, "...")
err := DeleteFiles(files, flags)
if err != nil {
return cliutils.CheckError(err)
}
return nil
}

func findFilesToDelete(artifactoryLfsFiles []utils.AqlSearchResultItem, gitLfsFiles map[string]struct{}) []utils.AqlSearchResultItem {
Expand Down Expand Up @@ -215,22 +213,6 @@ func collectLfsFileFromGit(results map[string]struct{}, file *object.File) error
return nil
}

func confirmDelete(files []utils.AqlSearchResultItem, quiet bool) bool {
if len(files) < 1 {
return false
}
if quiet {
return true
}
for _, v := range files {
fmt.Println(" " + v.Name)
}
var confirm string
fmt.Print("Are you sure you want to delete the above files? (y/n): ")
fmt.Scanln(&confirm)
return cliutils.ConfirmAnswer(confirm)
}

type GitLfsCleanFlags struct {
ArtDetails *config.ArtifactoryDetails
Refs string
Expand Down
Loading

0 comments on commit 4e98831

Please sign in to comment.