@@ -3,8 +3,10 @@ package cliutils
33import (
44 "errors"
55 "fmt"
6+ "github.com/jfrog/jfrog-cli-core/v2/common/project"
67 "io"
78 "os"
9+ "path/filepath"
810 "strconv"
911 "strings"
1012
@@ -18,6 +20,13 @@ import (
1820 "github.com/jfrog/jfrog-client-go/utils/log"
1921)
2022
23+ const (
24+ JfConfigDirName = ".jfrog"
25+ JfConfigFileName = "config.yml"
26+ ApplicationRootYML = "application"
27+ Key = "key"
28+ )
29+
2130func FixWinPathBySource (path string , fromSpec bool ) string {
2231 if strings .Count (path , "/" ) > 0 {
2332 // Assuming forward slashes - not doubling backslash to allow regexp escaping
@@ -251,3 +260,39 @@ func FixWinPathsForFileSystemSourcedCmds(uploadSpec *spec.SpecFiles, specFlag, e
251260 }
252261 }
253262}
263+
264+ // Retrieves the application key from the .jfrog/config file or the environment variable.
265+ // If the application key is not found in either, returns an empty string.
266+ func ReadJFrogApplicationKeyFromConfigOrEnv () (applicationKeyValue string ) {
267+ applicationKeyValue = getApplicationKeyFromConfig ()
268+ if applicationKeyValue != "" {
269+ log .Debug ("Found application key in config file:" , applicationKeyValue )
270+ return
271+ }
272+ applicationKeyValue = os .Getenv (coreutils .ApplicationKey )
273+ if applicationKeyValue != "" {
274+ log .Debug ("Found application key in environment variable:" , applicationKeyValue )
275+ return
276+ }
277+ log .Debug ("Application key is not found in the config file or environment variable." )
278+ return ""
279+ }
280+
281+ func getApplicationKeyFromConfig () string {
282+ configFilePath := filepath .Join (JfConfigDirName , JfConfigFileName )
283+ vConfig , err := project .ReadConfigFile (configFilePath , project .YAML )
284+ if err != nil {
285+ log .Debug ("error reading config file: %v" , err )
286+ return ""
287+ }
288+
289+ application := vConfig .GetStringMapString (ApplicationRootYML )
290+ applicationKey , ok := application [Key ]
291+ if ! ok {
292+ log .Debug ("Application key is not found in the config file." )
293+ return ""
294+ }
295+
296+ log .Debug ("Found application key:" , applicationKey )
297+ return applicationKey
298+ }
0 commit comments