Skip to content

Commit

Permalink
Support for json sources
Browse files Browse the repository at this point in the history
  • Loading branch information
andrii-codefresh committed Feb 6, 2024
1 parent 00e2d0c commit f1c3297
Showing 1 changed file with 25 additions and 11 deletions.
36 changes: 25 additions & 11 deletions reposerver/repository/app_version.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package repository

import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"reflect"
Expand All @@ -22,23 +24,35 @@ type Result struct {
Dependencies DependenciesMap `json:"dependencies"`
}

func getVersionFromYaml(appPath, jsonPathExpression string) (*string, error) {
func getVersionFromFile(appPath, jsonPathExpression string) (*string, error) {
content, err := os.ReadFile(appPath)
if err != nil {
return nil, err
}

log.Infof("AppVersion source content: %s", string(content))
log.Infof("AppVersion source content was read from %s", appPath)

var obj interface{}
if err := yaml.Unmarshal(content, &obj); err != nil {
return nil, err
}
var jsonObj interface{}

// Convert YAML to Map[interface{}]interface{}
jsonObj, err := convertToJSONCompatible(obj)
if err != nil {
return nil, err
// Determine the file type and unmarshal accordingly
switch filepath.Ext(appPath) {
case ".yaml", ".yml":
if err := yaml.Unmarshal(content, &obj); err != nil {
return nil, err
}
// Convert YAML to Map[interface{}]interface{}
jsonObj, err = convertToJSONCompatible(obj)
if err != nil {
return nil, err
}
case ".json":
if err := json.Unmarshal(content, &obj); err != nil {
return nil, err
}
jsonObj = obj
default:
return nil, fmt.Errorf("Unsupported file format of %s", appPath)
}

jp := jsonpath.New("jsonpathParser")
Expand All @@ -54,7 +68,7 @@ func getVersionFromYaml(appPath, jsonPathExpression string) (*string, error) {
}

appVersion := buf.String()
log.Infof("AppVersion source content appVersion: %s", appVersion)
log.Infof("Extracted appVersion: %s", appVersion)
return &appVersion, nil
}

Expand Down Expand Up @@ -91,7 +105,7 @@ func getAppVersions(appPath string, resourceName string, jsonPathExpression stri
}

// Get version of root
appVersion, err := getVersionFromYaml(filepath.Join(appPath, resourceName), jsonPathExpression)
appVersion, err := getVersionFromFile(filepath.Join(appPath, resourceName), jsonPathExpression)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit f1c3297

Please sign in to comment.