Skip to content
Merged
Changes from 1 commit
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
Next Next commit
add support for version-based question prompts in data stream creation
  • Loading branch information
teresaromero committed Sep 15, 2025
commit 95d84fa17dcf25ac2653924a39e732047e57c77c
20 changes: 15 additions & 5 deletions cmd/create_data_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"slices"

"github.com/AlecAivazis/survey/v2"
"github.com/Masterminds/semver/v3"

"github.com/spf13/cobra"

Expand All @@ -19,6 +20,8 @@ import (
"github.com/elastic/elastic-package/internal/surveyext"
)

var semver3_2_0 = semver.MustParse("3.2.0")

const createDataStreamLongDescription = `Use this command to create a new data stream.

The command can extend the package with a new data stream using embedded data stream template and wizard.`
Expand Down Expand Up @@ -80,14 +83,21 @@ func createDataStreamCommandAction(cmd *cobra.Command, args []string) error {
},
Validate: survey.Required,
},
{
}

sv, err := semver.NewVersion(manifest.SpecVersion)
if err != nil {
return fmt.Errorf("failed to obtain spec version from package manifest in \"%s\"", packageRoot)
}
if !sv.LessThan(semver3_2_0) {
qs = append(qs, &survey.Question{
Name: "subobjects",
Prompt: &survey.Confirm{
Message: "Enable creation of subobjects for fields with dots in their names?",
Default: false,
},
Validate: survey.Required,
},
})
}
var answers newDataStreamAnswers
err = survey.Ask(qs, &answers)
Expand Down Expand Up @@ -173,7 +183,7 @@ func createDataStreamCommandAction(cmd *cobra.Command, args []string) error {
}
}

descriptor := createDataStreamDescriptorFromAnswers(answers, packageRoot)
descriptor := createDataStreamDescriptorFromAnswers(answers, packageRoot, sv)
err = archetype.CreateDataStream(descriptor)
if err != nil {
return fmt.Errorf("can't create new data stream: %w", err)
Expand All @@ -183,14 +193,14 @@ func createDataStreamCommandAction(cmd *cobra.Command, args []string) error {
return nil
}

func createDataStreamDescriptorFromAnswers(answers newDataStreamAnswers, packageRoot string) archetype.DataStreamDescriptor {
func createDataStreamDescriptorFromAnswers(answers newDataStreamAnswers, packageRoot string, specVersion *semver.Version) archetype.DataStreamDescriptor {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we could unit test this function, checking the manifest it generates for given answers.

Further testing could be added to internal/packages/archetype/data_stream_test.go, but we wouldn't be really testing if the behaviour changes for the format version used in the package.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added some unit testing as suggested, also for the survey questions i've encapsulated this logic to at least check that the question is added or not based on the version.

manifest := packages.DataStreamManifest{
Name: answers.Name,
Title: answers.Title,
Type: answers.Type,
}

if !answers.Subobjects {
if !specVersion.LessThan(semver3_2_0) && !answers.Subobjects {
manifest.Elasticsearch = &packages.Elasticsearch{
IndexTemplate: &packages.ManifestIndexTemplate{
Mappings: &packages.ManifestMappings{
Expand Down