@@ -19,7 +19,9 @@ import (
1919 "gopkg.in/yaml.v3"
2020
2121 "github.com/elastic/elastic-package/internal/common"
22+ "github.com/elastic/elastic-package/internal/logger"
2223 "github.com/elastic/elastic-package/internal/multierror"
24+ "github.com/elastic/elastic-package/internal/packages"
2325 "github.com/elastic/elastic-package/internal/packages/buildmanifest"
2426)
2527
@@ -39,7 +41,7 @@ type Validator struct {
3941 allowedCIDRs []* net.IPNet
4042}
4143
42- // ValidatorOption represents an optional flag that can be passed to CreateValidatorForDataStream .
44+ // ValidatorOption represents an optional flag that can be passed to CreateValidatorForDirectory .
4345type ValidatorOption func (* Validator ) error
4446
4547// WithDefaultNumericConversion configures the validator to accept defined keyword (or constant_keyword) fields as numeric-type.
@@ -78,8 +80,8 @@ func WithEnabledAllowedIPCheck() ValidatorOption {
7880 }
7981}
8082
81- // CreateValidatorForDataStream function creates a validator for the data stream .
82- func CreateValidatorForDataStream ( dataStreamRootPath string , opts ... ValidatorOption ) (v * Validator , err error ) {
83+ // CreateValidatorForDirectory function creates a validator for the directory .
84+ func CreateValidatorForDirectory ( fieldsParentDir string , opts ... ValidatorOption ) (v * Validator , err error ) {
8385 v = new (Validator )
8486 for _ , opt := range opts {
8587 if err := opt (v ); err != nil {
@@ -89,16 +91,28 @@ func CreateValidatorForDataStream(dataStreamRootPath string, opts ...ValidatorOp
8991
9092 v .allowedCIDRs = initializeAllowedCIDRsList ()
9193
92- v .Schema , err = loadFieldsForDataStream (dataStreamRootPath )
94+ fieldsDir := filepath .Join (fieldsParentDir , "fields" )
95+ v .Schema , err = loadFieldsFromDir (fieldsDir )
9396 if err != nil {
94- return nil , errors .Wrapf (err , "can't load fields for data stream (path: %s)" , dataStreamRootPath )
97+ return nil , errors .Wrapf (err , "can't load fields from directory (path: %s)" , fieldsDir )
9598 }
9699
97100 if v .disabledDependencyManagement {
98101 return v , nil
99102 }
100103
101- packageRoot := filepath .Dir (filepath .Dir (dataStreamRootPath ))
104+ packageRoot , found , err := packages .FindPackageRoot ()
105+ if err != nil {
106+ return nil , errors .Wrap (err , "can't find package root" )
107+ }
108+ // As every command starts with approximating where is the package root, it isn't required to return an error in case the root is missing.
109+ // This is also useful for testing purposes, where we don't have a real package, but just "fields" directory. The package root is always absent.
110+ if ! found {
111+ logger .Debug ("Package root not found, dependency management will be disabled." )
112+ v .disabledDependencyManagement = true
113+ return v , nil
114+ }
115+
102116 bm , ok , err := buildmanifest .ReadBuildManifest (packageRoot )
103117 if err != nil {
104118 return nil , errors .Wrap (err , "can't read build manifest" )
@@ -132,8 +146,7 @@ func initializeAllowedCIDRsList() (cidrs []*net.IPNet) {
132146 return cidrs
133147}
134148
135- func loadFieldsForDataStream (dataStreamRootPath string ) ([]FieldDefinition , error ) {
136- fieldsDir := filepath .Join (dataStreamRootPath , "fields" )
149+ func loadFieldsFromDir (fieldsDir string ) ([]FieldDefinition , error ) {
137150 files , err := filepath .Glob (filepath .Join (fieldsDir , "*.yml" ))
138151 if err != nil {
139152 return nil , errors .Wrapf (err , "reading directory with fields failed (path: %s)" , fieldsDir )
0 commit comments