Skip to content
8 changes: 8 additions & 0 deletions common/cliutils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import (
"github.com/jfrog/jfrog-cli-core/v2/common/commands"
"github.com/jfrog/jfrog-cli-core/v2/common/spec"
"github.com/jfrog/jfrog-cli-core/v2/utils/config"
coreConfig "github.com/jfrog/jfrog-cli-core/v2/utils/config"
"github.com/jfrog/jfrog-cli-core/v2/utils/coreutils"
"github.com/jfrog/jfrog-cli-core/v2/utils/ioutils"
"github.com/jfrog/jfrog-client-go/utils"
clientUtils "github.com/jfrog/jfrog-client-go/utils"
"github.com/jfrog/jfrog-client-go/utils/errorutils"
"github.com/jfrog/jfrog-client-go/utils/log"
Expand Down Expand Up @@ -251,3 +253,9 @@ func FixWinPathsForFileSystemSourcedCmds(uploadSpec *spec.SpecFiles, specFlag, e
}
}
}

func PlatformToLifecycleUrls(lcDetails *coreConfig.ServerDetails) {
lcDetails.ArtifactoryUrl = utils.AddTrailingSlashIfNeeded(lcDetails.Url) + "artifactory/"
lcDetails.LifecycleUrl = utils.AddTrailingSlashIfNeeded(lcDetails.Url) + "lifecycle/"
lcDetails.Url = ""
}
78 changes: 78 additions & 0 deletions plugins/common/utils.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package common

import (
"errors"
artifactoryUtils "github.com/jfrog/jfrog-cli-core/v2/artifactory/utils"
"github.com/jfrog/jfrog-cli-core/v2/utils/coreutils"
clientutils "github.com/jfrog/jfrog-client-go/utils"
"os"
"sort"
"strconv"
"strings"
Expand All @@ -13,6 +16,13 @@ import (
"golang.org/x/exp/slices"
)

const (
minSplit = "min-split"
DownloadMinSplitKb = 5120
DownloadSplitCount = 3
DownloadMaxSplitCount = 15
)

func GetStringsArrFlagValue(c *components.Context, flagName string) (resultArray []string) {
if c.IsFlagSet(flagName) {
resultArray = append(resultArray, strings.Split(c.GetStringFlagValue(flagName), ";")...)
Expand Down Expand Up @@ -124,3 +134,71 @@ func getCiValue() bool {
}
return ci
}

// Get project key from flag or environment variable
func GetProject(c *components.Context) string {
projectKey := c.GetStringFlagValue("project")
return getOrDefaultEnv(projectKey, coreutils.Project)
}

// Return argument if not empty or retrieve from environment variable
func getOrDefaultEnv(arg, envKey string) string {
if arg != "" {
return arg
}
return os.Getenv(envKey)
}

func CreateDownloadConfiguration(c *components.Context) (downloadConfiguration *artifactoryUtils.DownloadConfiguration, err error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some of these aren't really plugins utils, I suggest placing them in jfrog-cli-artifactory.
In addition, since they duplicate existing functions in jfrog-cli (only differ in context), let's try to make a common function out of them or somehow overcome the context difference

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree moved lifecycle functions to jfrog-cli-artifactory

downloadConfiguration = new(artifactoryUtils.DownloadConfiguration)
downloadConfiguration.MinSplitSize, err = getMinSplit(c, DownloadMinSplitKb)
if err != nil {
return nil, err
}
downloadConfiguration.SplitCount, err = getSplitCount(c, DownloadSplitCount, DownloadMaxSplitCount)
if err != nil {
return nil, err
}
downloadConfiguration.Threads, err = GetThreadsCount(c)
if err != nil {
return nil, err
}
downloadConfiguration.SkipChecksum = c.GetBoolFlagValue("skip-checksum")
downloadConfiguration.Symlink = true
return
}

func getMinSplit(c *components.Context, defaultMinSplit int64) (minSplitSize int64, err error) {
minSplitSize = defaultMinSplit
if c.GetStringFlagValue(minSplit) != "" {
minSplitSize, err = strconv.ParseInt(c.GetStringFlagValue(minSplit), 10, 64)
if err != nil {
err = errors.New("The '--min-split' option should have a numeric value. " + GetDocumentationMessage())
return 0, err
}
}

return minSplitSize, nil
}

func GetDocumentationMessage() string {
return "You can read the documentation at " + coreutils.JFrogHelpUrl + "jfrog-cli"
}

func getSplitCount(c *components.Context, defaultSplitCount, maxSplitCount int) (splitCount int, err error) {
splitCount = defaultSplitCount
err = nil
if c.GetStringFlagValue("split-count") != "" {
splitCount, err = strconv.Atoi(c.GetStringFlagValue("split-count"))
if err != nil {
err = errors.New("The '--split-count' option should have a numeric value. " + GetDocumentationMessage())
}
if splitCount > maxSplitCount {
err = errors.New("The '--split-count' option value is limited to a maximum of " + strconv.Itoa(maxSplitCount) + ".")
}
if splitCount < 0 {
err = errors.New("the '--split-count' option cannot have a negative value")
}
}
return
}
35 changes: 35 additions & 0 deletions plugins/components/commandcomp.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,19 @@ func (c *Context) GetStringFlagValue(flagName string) string {
}

func (c *Context) AddStringFlag(key, value string) {
if c.stringFlags == nil {
c.stringFlags = make(map[string]string)
}
c.stringFlags[key] = value
}

func (c *Context) AddBoolFlag(key string, value bool) {
if c.boolFlags == nil {
c.boolFlags = make(map[string]bool)
}
c.boolFlags[key] = value
}

func (c *Context) GetIntFlagValue(flagName string) (value int, err error) {
parsed, err := strconv.ParseInt(c.GetStringFlagValue(flagName), 0, 64)
if err != nil {
Expand All @@ -74,6 +84,19 @@ func (c *Context) GetIntFlagValue(flagName string) (value int, err error) {
return
}

func (c *Context) GetDefaultIntFlagValueIfNotSet(flagName string, defaultValue int) (value int, err error) {
if c.IsFlagSet(flagName) {
parsed, err := strconv.ParseInt(c.GetStringFlagValue(flagName), 0, 64)
if err != nil {
err = fmt.Errorf("can't parse int flag '%s': %w", flagName, err)
return value, err
}
value = int(parsed)
return value, err
}
return defaultValue, nil
}

func (c *Context) GetBoolFlagValue(flagName string) bool {
return c.boolFlags[flagName]
}
Expand Down Expand Up @@ -183,6 +206,18 @@ func WithBoolDefaultValueFalse() BoolFlagOption {
}
}

func WithHiddenTrue() StringFlagOption {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A similar func already exists - SetHiddenStrFlag

return func(f *StringFlag) {
f.Hidden = true
}
}

func WithBoolDefaultValueTrue() BoolFlagOption {
return func(f *BoolFlag) {
f.DefaultValue = true
}
}

type BoolFlag struct {
BaseFlag
DefaultValue bool
Expand Down
Loading