Skip to content

Commit

Permalink
minor refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
neelayu committed Aug 26, 2022
1 parent aecaaf2 commit b9323b4
Showing 1 changed file with 63 additions and 59 deletions.
122 changes: 63 additions & 59 deletions service/configinit.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,12 @@ func NewConfigurationInitCommand(set CollectorSettings) *cobra.Command {
log.Fatal(err)
}

serviceNode, err := getServiceNode(set.Factories, pipelinesSelected)
serviceNode, err := getServiceNode(pipelinesSelected)
if err != nil {
log.Fatal(err)
}

configYaml, err := getSampleConfig(exportersNode, processorsNode, receiversNode, extensionsNode, serviceNode)
if err != nil {
log.Fatal(err)
}
configYaml := getSampleConfig(exportersNode, processorsNode, receiversNode, extensionsNode, serviceNode)
err = outputConfiguration(configYaml)
if err != nil {
log.Fatal(err)
Expand Down Expand Up @@ -134,18 +131,18 @@ func showPrompt(set CollectorSettings) {
Default: defaultConfigFileName,
}
promptDryRun := &survey.Confirm{
Message: "Dry run(Default: false)",
Message: "Dry run(Default: N)",
Default: false,
}

survey.AskOne(promptExp, &exportersSelected)
survey.AskOne(promptProc, &processorsSelected)
survey.AskOne(promptRec, &receiversSelected)
survey.AskOne(promptExt, &extensionsSelected)
survey.AskOne(promptPipelines, &pipelinesSelected)
survey.AskOne(promptDryRun, &dryRun)
_ = survey.AskOne(promptExp, &exportersSelected)
_ = survey.AskOne(promptProc, &processorsSelected)
_ = survey.AskOne(promptRec, &receiversSelected)
_ = survey.AskOne(promptExt, &extensionsSelected)
_ = survey.AskOne(promptPipelines, &pipelinesSelected)
_ = survey.AskOne(promptDryRun, &dryRun)
if !dryRun {
survey.AskOne(promptFile, &configFileName)
_ = survey.AskOne(promptFile, &configFileName)
}

}
Expand All @@ -155,25 +152,24 @@ func filterFunc(filterValue string, optValue string, optIndex int) bool {
return strings.Contains(optValue, filterValue) && len(optValue) >= 3
}

func getSampleConfig(exportersNode, processorsNode, receiversNode, extensionsNode, serviceNode *yaml.Node) (*yaml.Node, error) {
func getSampleConfig(exportersNode, processorsNode, receiversNode, extensionsNode, serviceNode *yaml.Node) *yaml.Node {

configYml := &yaml.Node{
Kind: yaml.DocumentNode,
Content: []*yaml.Node{
{
Kind: yaml.MappingNode,
},
},
}
configYml.Content[0].Content = append(configYml.Content[0].Content, exportersNode.Content...)
configYml.Content[0].Content = append(configYml.Content[0].Content, processorsNode.Content...)
configYml.Content[0].Content = append(configYml.Content[0].Content, receiversNode.Content...)
configYml.Content[0].Content = append(configYml.Content[0].Content, extensionsNode.Content...)
configYml.Content[0].Content = append(configYml.Content[0].Content, serviceNode.Content...)
content := make([]*yaml.Node, 0)

return configYml, nil
content = append(content, exportersNode.Content...)
content = append(content, processorsNode.Content...)
content = append(content, receiversNode.Content...)
content = append(content, extensionsNode.Content...)
content = append(content, serviceNode.Content...)

configYaml := newDocumentNode(
newMappingNode(content...),
)

return configYaml
}

// getConfigFields returns a FieldInfo for a given component type and name.
func getConfigFields(factories component.Factories, compType string, compName string) (*configschema.Field, error) {
cs, err := configschema.GetCfgInfo(factories, compType, compName)
if err != nil {
Expand All @@ -183,7 +179,7 @@ func getConfigFields(factories component.Factories, compType string, compName st
if moduleSource == "core" || moduleSource == "contrib" {
modPath = defaultModPath
}
f, err := os.ReadFile(modPath)
f, err := os.ReadFile(filepath.Clean(modPath))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -216,26 +212,29 @@ func outputConfiguration(configYaml *yaml.Node) error {
return ymlEncoder.Encode(configYaml)
}

func getComponentNode(factories component.Factories, compSlice []string, compType string) (*yaml.Node, error) {
content := &yaml.Node{
Kind: yaml.MappingNode,
}
for index := range compSlice {
field, err := getConfigFields(factories, compType, compSlice[index])
// getComponentNode create a yaml.Node from the given set of factories and the correponding component type.
// For example, given the exporter factories and type exporter with [otlp, logging] as components, it will
// return an Node reprensentation equivalent of-
// exporters:
// logging:
// ...
// otlp:
// ...
func getComponentNode(factories component.Factories, components []string, componentType string) (*yaml.Node, error) {
content := newMappingNode()
for index := range components {
field, err := getConfigFields(factories, componentType, components[index])
if err != nil {
return nil, err
}
compNode := newSimpleScalarNode(compSlice[index])
compNode := newSimpleScalarNode(components[index])
content.Content = append(content.Content, compNode, fieldToYaml(field))
}
return &yaml.Node{
Kind: yaml.MappingNode,
Content: []*yaml.Node{
// plural form of the component type
newSimpleScalarNode(fmt.Sprintf("%ss", compType)),
content,
},
}, nil
return newMappingNode(
// plural form of the component type
newSimpleScalarNode(fmt.Sprintf("%ss", componentType)),
content,
), nil
}

func getExportersNode(factories component.Factories, exporters []string) (*yaml.Node, error) {
Expand All @@ -254,15 +253,10 @@ func getExtensionsNode(factories component.Factories, extensions []string) (*yam
return getComponentNode(factories, extensions, "extension")
}

func getServiceNode(factories component.Factories, pipelines []string) (*yaml.Node, error) {
func getServiceNode(pipelines []string) (*yaml.Node, error) {
acceptedPipelines := []config.DataType{config.MetricsDataType, config.TracesDataType, config.LogsDataType}
pipelineNode := newMappingNode(
newSimpleScalarNode("pipelines"),
)

pipe := &yaml.Node{
Kind: yaml.MappingNode,
}
pipeline := newMappingNode()
for index := range pipelines {
if !contains(acceptedPipelines, pipelines[index]) {
return nil, fmt.Errorf("%s is not a valid pipeline", pipelines[index])
Expand All @@ -278,19 +272,18 @@ func getServiceNode(factories component.Factories, pipelines []string) (*yaml.No
newScalarNodeWithComment("receivers", fmt.Sprintf("Specify receivers for the %s pipeline", pipelines[index])),
newSequenceNode(),
)
pipe.Content = append(pipe.Content, pipelineType, pipelineComponents)
pipeline.Content = append(pipeline.Content, pipelineType, pipelineComponents)
}

pipelineNode.Content = append(pipelineNode.Content, pipe)
// return pipelineNode, nil
serviceNode := newMappingNode(
newSimpleScalarNode("service"),
newMappingNode(
newScalarNodeWithComment("extensions", "Specify extensions for the collector"),
newSequenceNode(),
newSimpleScalarNode("pipelines"),
pipeline,
),
)
serviceNode.Content[1].Content = append(serviceNode.Content[1].Content, pipelineNode.Content...)
return serviceNode, nil
}

Expand Down Expand Up @@ -335,22 +328,31 @@ func buildKeyValueNode(field *configschema.Field) *yaml.Node {
return newMappingNode(keyNode, valueNode)
}

func newSequenceNode(children ...*yaml.Node) *yaml.Node {
// newDocumentNode creates a yaml node with kind yaml.DocumentNode
func newDocumentNode(children ...*yaml.Node) *yaml.Node {
return &yaml.Node{
Kind: yaml.SequenceNode,
Kind: yaml.DocumentNode,
Content: children,
}
}

// newMappingNode returns a new mapping node with the given children.
// newSequenceNode creates a yaml node with kind yaml.SequenceNode
// func args are ignored for now.
func newSequenceNode(...*yaml.Node) *yaml.Node {
return &yaml.Node{
Kind: yaml.SequenceNode,
}
}

// newMappingNode creates a new mapping node with the given children.
func newMappingNode(children ...*yaml.Node) *yaml.Node {
return &yaml.Node{
Kind: yaml.MappingNode,
Content: children,
}
}

// newScalarNode returns a yml node holding a scalar value
// newScalarNode creates a yml node holding a scalar value
func newScalarNode(value string, doc string, style yaml.Style) *yaml.Node {
return &yaml.Node{
Kind: yaml.ScalarNode,
Expand All @@ -360,10 +362,12 @@ func newScalarNode(value string, doc string, style yaml.Style) *yaml.Node {
}
}

// newSimpleScalarNode creates a scalar node with the given value
func newSimpleScalarNode(value string) *yaml.Node {
return newScalarNode(value, "", yaml.FlowStyle)
}

// newScalarNodeWithComment creates a new scalar node with the given value and HeadComment
func newScalarNodeWithComment(value string, doc string) *yaml.Node {
return newScalarNode(value, doc, yaml.FlowStyle)
}
Expand Down

0 comments on commit b9323b4

Please sign in to comment.