Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add yaml nodes #38

Merged
merged 1 commit into from
Jul 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified momentum-core/momentum-core
Binary file not shown.
2 changes: 1 addition & 1 deletion momentum-core/services/application-service.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func (as *ApplicationService) AddApplication(request *models.ApplicationCreateRe
return nil, errors.New("unable to find kustomization resources for repository of new application")
}

err = repositoryKustomizationResources.AddValue(request.Name, 0)
err = repositoryKustomizationResources.AddYamlValue(request.Name, 0)
if err != nil {
config.LOGGER.LogWarning("failed adding application to resources", err, traceId)
return nil, err
Expand Down
9 changes: 2 additions & 7 deletions momentum-core/services/deployment-service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"errors"
"momentum-core/config"
"momentum-core/models"
"momentum-core/tree"
"momentum-core/utils"
)

Expand Down Expand Up @@ -108,14 +107,12 @@ func (ds *DeploymentService) AddDeployment(request *models.DeploymentCreateReque
}

if parentStageKustomizationResources != nil {
err = parentStageKustomizationResources.AddValue(deploymentYamlDestinationName, 0)
err = parentStageKustomizationResources.AddYamlValue(deploymentYamlDestinationName, 0)
if err != nil {
config.LOGGER.LogWarning("failed adding deployment to resources", err, traceId)
return nil, err
}

tree.Print(parentStageKustomizationResources)

err = parentStageKustomizationResources.Write(true)
if err != nil {
config.LOGGER.LogError("failed writing deployment to resources", err, traceId)
Expand All @@ -128,14 +125,12 @@ func (ds *DeploymentService) AddDeployment(request *models.DeploymentCreateReque
return nil, err
}

err = parentStageKustomization.AddSequence("resources", []string{deploymentYamlDestinationName}, 0)
err = parentStageKustomization.AddYamlSequence("resources", []string{deploymentYamlDestinationName}, 0)
if err != nil {
config.LOGGER.LogError("unable to add resources sequence to parents stage kustomization", err, traceId)
return nil, err
}

tree.Print(parentStageKustomization)

err = parentStageKustomization.Write(true)
if err != nil {
config.LOGGER.LogError("failed writing deployment to resources", err, traceId)
Expand Down
4 changes: 2 additions & 2 deletions momentum-core/services/stage-service.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func (s *StageService) AddStage(request *models.StageCreateRequest, traceId stri
return nil, err
}

err = kustomizationFileNode.AddSequence("resources", []string{request.Name}, 0)
err = kustomizationFileNode.AddYamlSequence("resources", []string{request.Name}, 0)
if err != nil {
config.LOGGER.LogError(err.Error(), err, traceId)
return nil, err
Expand All @@ -137,7 +137,7 @@ func (s *StageService) AddStage(request *models.StageCreateRequest, traceId stri
return nil, err
}
} else {
err = parentKustomizationResources.AddValue(request.Name, 0)
err = parentKustomizationResources.AddYamlValue(request.Name, 0)
if err != nil {
config.LOGGER.LogWarning("failed adding stage to resources", err, traceId)
return nil, err
Expand Down
2 changes: 0 additions & 2 deletions momentum-core/tree/momentum-tree.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package tree

import (
"fmt"
"momentum-core/config"
"strings"
)
Expand Down Expand Up @@ -44,7 +43,6 @@ func (n *Node) stages() []*Node {

nodesStages := make([]*Node, 0)
for _, possibleStage := range n.Directories() {
fmt.Println(possibleStage.Id, ":", possibleStage.FullPath())
if possibleStage.Kind == Directory && !strings.HasPrefix(possibleStage.Path, META_PREFIX) {
childStages := possibleStage.stages()
nodesStages = append([]*Node{possibleStage}, childStages...)
Expand Down
68 changes: 0 additions & 68 deletions momentum-core/tree/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,74 +214,6 @@ func (n *Node) FileMapping() (*Node, error) {
return nil, errors.New("file has no mapping node")
}

func (n *Node) AddSequence(key string, values []string, style yaml.Style) error {

if len(values) < 1 {
return errors.New("sequence must have at least one value")
}

yamlSequenceNode := CreateSequenceNode(key, style)
sequenceNode := NewNode(Sequence, key, "", nil, nil, yamlSequenceNode)

for _, val := range values {
err := sequenceNode.AddValue(val, 0)
if err != nil {
return err
}
}

return AddContent(n, sequenceNode)
}

func (n *Node) AddMapping(key string, style yaml.Style) (*Node, error) {

return nil, errors.New("AddMapping is not yet ready!! see FILESYSTEMTEST_TestAddMapping")

if n.Kind != File && n.Kind != Mapping && n.Kind != Sequence {
return nil, errors.New("can only add mapping to type of kind file mapping or sequence")
}

yamlMappingNode := CreateMappingNode(key, style)
mappingNode := NewNode(Mapping, key, "", nil, nil, yamlMappingNode)

err := AddContent(n, mappingNode)
if err != nil {
return nil, err
}

return mappingNode, nil
}

func (n *Node) AddProperty(key string, value string, valueTag string, style yaml.Style) error {

if n.Kind != Mapping || n.YamlNode.Kind != yaml.MappingNode {
return errors.New("properties can only be added to mapping nodes")
}

yamlKeyNode, yamlValueNode := CreatePropertyNodes(key, value, valueTag, style)

mappingNode := NewNode(Property, key, value, n, nil, yamlValueNode)

n.YamlNode.Content = append(n.YamlNode.Content, yamlKeyNode, yamlValueNode)
n.AddChild(mappingNode)

return nil
}

func (n *Node) AddValue(value string, style yaml.Style) error {

if n.Kind != Sequence || n.YamlNode.Kind != yaml.SequenceNode {
return errors.New("can only add sequence value to node of type sequence")
}

sequenceValue := CreateScalarNode(value, StrTag, style)
n.YamlNode.Content = append(n.YamlNode.Content, sequenceValue)
momentumNode := NewNode(Value, "", value, n, nil, sequenceValue)
n.AddChild(momentumNode)

return nil
}

func idMatch(expect string, n *Node) bool {

id, err := utils.GenerateId(n.FullPath())
Expand Down
91 changes: 71 additions & 20 deletions momentum-core/tree/yaml-node-factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,35 +19,86 @@ const (
mergeTag = "!!merge"
)

func AddContent(parent *Node, content *Node) error {
func (n *Node) AddYamlSequence(key string, values []string, style yaml.Style) error {

if parent == nil || content == nil {
return errors.New("parent or content is nil")
if len(values) < 1 {
return errors.New("sequence must have at least one value")
}

if parent.YamlNode.Kind != yaml.DocumentNode &&
parent.YamlNode.Kind != yaml.MappingNode &&
parent.YamlNode.Kind != yaml.SequenceNode {
return errors.New("can only add content to document mapping and sequence yaml nodes")
var err error = nil
var anchor *Node = n
if n.Kind == File {
anchor, err = n.FileMapping()
if err != nil {
return errors.New("failed retrieving file mapping of file node")
}
}

if parent.Kind != File &&
parent.Kind != Mapping &&
parent.Kind != Sequence {
return errors.New("can only add content to file mapping and sequence nodes")
yamlSequenceName := CreateScalarNode(key, StrTag, 0)
yamlSequenceNode := CreateSequenceNode(style)
anchor.YamlNode.Content = append(anchor.YamlNode.Content, yamlSequenceName, yamlSequenceNode)

sequenceNode := NewNode(Sequence, key, "", nil, nil, yamlSequenceNode)
anchor.AddChild(sequenceNode)

for _, val := range values {
err := sequenceNode.AddYamlValue(val, 0)
if err != nil {
return err
}
}

return nil
}

func (n *Node) AddYamlMapping(key string, style yaml.Style) (*Node, error) {

var err error = nil
p := parent
if parent.Kind == File {
p, err = parent.FileMapping()
var anchor *Node = n
if n.Kind == File {
anchor, err = n.FileMapping()
if err != nil {
return errors.New("unable to retrieve parent files mapping")
return nil, err
}
}

p.YamlNode.Content = append(p.YamlNode.Content, content.YamlNode)
p.AddChild(content)
yamlMappingName := CreateScalarNode(key, StrTag, 0)
yamlMappingNode := CreateMappingNode(key, style)

anchor.YamlNode.Content = append(anchor.YamlNode.Content, yamlMappingName, yamlMappingNode)

mappingNode := NewNode(Mapping, key, "", nil, nil, yamlMappingNode)
anchor.AddChild(mappingNode)

return mappingNode, nil
}

func (n *Node) AddYamlProperty(key string, value string, valueTag string, style yaml.Style) error {

if n.Kind != Mapping || n.YamlNode.Kind != yaml.MappingNode {
return errors.New("properties can only be added to mapping nodes")
}

yamlKeyNode, yamlValueNode := CreatePropertyNodes(key, value, valueTag, style)

mappingNode := NewNode(Property, key, value, n, nil, yamlValueNode)

n.YamlNode.Content = append(n.YamlNode.Content, yamlKeyNode, yamlValueNode)
n.AddChild(mappingNode)

return nil
}

func (n *Node) AddYamlValue(value string, style yaml.Style) error {

if n.Kind != Sequence || n.YamlNode.Kind != yaml.SequenceNode {
return errors.New("can only add sequence value to node of type sequence")
}

sequenceValue := CreateScalarNode(value, StrTag, style)
n.YamlNode.Content = append(n.YamlNode.Content, sequenceValue)
momentumNode := NewNode(Value, "", value, n, nil, sequenceValue)
n.AddChild(momentumNode)

return nil
}
Expand All @@ -60,13 +111,13 @@ func CreatePropertyNodes(key string, value string, valueTag string, style yaml.S
return keyNode, valueNode
}

func CreateSequenceNode(key string, style yaml.Style) *yaml.Node {
func CreateSequenceNode(style yaml.Style) *yaml.Node {

n := new(yaml.Node)

n.Kind = yaml.SequenceNode
n.Tag = seqTag
n.Value = key
n.Value = ""
n.Style = style

return n
Expand All @@ -78,7 +129,7 @@ func CreateMappingNode(key string, style yaml.Style) *yaml.Node {

n.Kind = yaml.MappingNode
n.Tag = mapTag
n.Value = key
n.Value = ""
n.Style = style

return n
Expand Down
Loading