Skip to content

Commit

Permalink
MBT build command support "strict" parameter (#305)
Browse files Browse the repository at this point in the history
Co-authored-by: Jiang, Yu-tao <yu-tao.jiang@sap.com>
  • Loading branch information
yutaoj and JiangYuTao authored Jan 17, 2024
1 parent 754ce63 commit cc05008
Show file tree
Hide file tree
Showing 2 changed files with 312 additions and 13 deletions.
51 changes: 38 additions & 13 deletions mta/mta_services.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ package mta
import (
"crypto/sha256"
"fmt"
"github.com/pkg/errors"
"gopkg.in/yaml.v3"
"io"
"io/ioutil"
"os"
"path/filepath"

"github.com/pkg/errors"
"gopkg.in/yaml.v3"

ghodss "github.com/ghodss/yaml"
"github.com/json-iterator/go"
jsoniter "github.com/json-iterator/go"

"github.com/SAP/cloud-mta/internal/fs"
"github.com/SAP/cloud-mta/internal/logs"
Expand Down Expand Up @@ -59,6 +60,30 @@ func GetMtaFromFile(path string, extensions []string, returnMergeError bool) (mt
return mta, messages, nil
}

func GetMtaFromFileWithStrict(path string, extensions []string, returnMergeError bool, strict bool) (mta *MTA, messages []string, err error) {
mtaContent, err := fs.ReadFile(filepath.Join(path))
if err != nil {
return nil, nil, err
}
mta, err = Unmarshal(mtaContent)
if strict && err != nil {
return nil, nil, errors.Wrapf(err, UnmarshalFailsMsg, path)
}
if strict == false && err != nil {
messages = []string{errors.Wrapf(err, UnmarshalFailsMsg, path).Error()}
}

// If there is an error during the merge return the result so far and return the error as a message (or error if required).
extErr := mergeWithExtensionFiles(mta, extensions, path)
if extErr != nil {
if returnMergeError {
return mta, nil, extErr
}
messages = []string{extErr.Error()}
}
return mta, messages, nil
}

func unmarshalData(dataJSON string, o interface{}) error {
dataYaml, err := ghodss.JSONToYAML([]byte(dataJSON))
if err != nil {
Expand Down Expand Up @@ -93,7 +118,7 @@ func DeleteMta(path string) error {
return fs.DeleteDir(path)
}

//AddModule - adds a new module.
// AddModule - adds a new module.
func AddModule(path string, moduleDataJSON string, marshal func(*MTA) ([]byte, error)) ([]string, error) {
mta, messages, err := GetMtaFromFile(filepath.Join(path), nil, false)
if err != nil {
Expand All @@ -110,7 +135,7 @@ func AddModule(path string, moduleDataJSON string, marshal func(*MTA) ([]byte, e
return messages, saveMTA(path, mta, marshal)
}

//AddResource - adds a new resource.
// AddResource - adds a new resource.
func AddResource(path string, resourceDataJSON string, marshal func(*MTA) ([]byte, error)) ([]string, error) {
mta, messages, err := GetMtaFromFile(path, nil, false)
if err != nil {
Expand All @@ -127,7 +152,7 @@ func AddResource(path string, resourceDataJSON string, marshal func(*MTA) ([]byt
return messages, saveMTA(path, mta, marshal)
}

//GetModules - gets all modules.
// GetModules - gets all modules.
func GetModules(path string, extensions []string) ([]*Module, []string, error) {
mta, messages, err := GetMtaFromFile(path, extensions, false)
if err != nil {
Expand All @@ -136,7 +161,7 @@ func GetModules(path string, extensions []string) ([]*Module, []string, error) {
return mta.Modules, messages, nil
}

//GetResources - gets all resources.
// GetResources - gets all resources.
func GetResources(path string, extensions []string) ([]*Resource, []string, error) {
mta, messages, err := GetMtaFromFile(path, extensions, false)
if err != nil {
Expand Down Expand Up @@ -261,7 +286,7 @@ func UpdateResource(path string, resourceDataJSON string, marshal func(*MTA) ([]
return messages, fmt.Errorf("the '%s' resource does not exist", resource.Name)
}

//GetMtaID - gets MTA ID.
// GetMtaID - gets MTA ID.
func GetMtaID(path string) (string, []string, error) {
mta, messages, err := GetMtaFromFile(path, nil, false)
if err != nil {
Expand All @@ -270,7 +295,7 @@ func GetMtaID(path string) (string, []string, error) {
return mta.ID, messages, nil
}

//IsNameUnique - checks if the name already exists as a `module`/`resource`/`provide` name.
// IsNameUnique - checks if the name already exists as a `module`/`resource`/`provide` name.
func IsNameUnique(path string, name string) (bool, []string, error) {
mta, messages, err := GetMtaFromFile(path, nil, false)
if err != nil {
Expand All @@ -295,7 +320,7 @@ func IsNameUnique(path string, name string) (bool, []string, error) {
return false, messages, nil
}

//getBuildParameters - gets the MTA build parameters.
// getBuildParameters - gets the MTA build parameters.
func GetBuildParameters(path string, extensions []string) (*ProjectBuild, []string, error) {
mta, messages, err := GetMtaFromFile(path, extensions, false)
if err != nil {
Expand All @@ -304,7 +329,7 @@ func GetBuildParameters(path string, extensions []string) (*ProjectBuild, []stri
return mta.BuildParams, messages, nil
}

//getParameters - gets the MTA parameters.
// getParameters - gets the MTA parameters.
func GetParameters(path string, extensions []string) (*map[string]interface{}, []string, error) {
mta, messages, err := GetMtaFromFile(path, extensions, false)
if err != nil {
Expand All @@ -313,7 +338,7 @@ func GetParameters(path string, extensions []string) (*map[string]interface{}, [
return &mta.Parameters, messages, nil
}

//UpdateBuildParameters - updates the MTA build parameters.
// UpdateBuildParameters - updates the MTA build parameters.
func UpdateBuildParameters(path string, buildParamsDataJSON string) ([]string, error) {
mta, messages, err := GetMtaFromFile(path, nil, false)
if err != nil {
Expand All @@ -330,7 +355,7 @@ func UpdateBuildParameters(path string, buildParamsDataJSON string) ([]string, e
return messages, saveMTA(path, mta, Marshal)
}

//UpdateParameters - updates the MTA parameters.
// UpdateParameters - updates the MTA parameters.
func UpdateParameters(path string, paramsDataJSON string) ([]string, error) {
mta, messages, err := GetMtaFromFile(path, nil, false)
if err != nil {
Expand Down
Loading

0 comments on commit cc05008

Please sign in to comment.