in packages/package.go [216:356]
func NewPackage(logger *zap.Logger, basePath string, fsBuilder FileSystemBuilder) (*Package, error) {
p := &Package{
BasePath: basePath,
fsBuilder: fsBuilder,
logger: logger,
}
fs, err := p.fs()
if err != nil {
return nil, err
}
defer fs.Close()
manifestBody, err := ReadAll(fs, "manifest.yml")
if err != nil {
return nil, err
}
manifest, err := yaml.NewConfig(manifestBody, ucfg.PathSep("."))
if err != nil {
return nil, err
}
err = manifest.Unpack(p, ucfg.PathSep("."))
if err != nil {
return nil, err
}
p.logger = p.logger.With(zap.String("package", p.Name), zap.String("version", p.Version))
// Default for the multiple flags is true.
trueValue := true
for i := range p.PolicyTemplates {
if p.PolicyTemplates[i].Multiple == nil {
p.PolicyTemplates[i].Multiple = &trueValue
}
// Collect basic information from policy templates and store into the /search endpoint
t := p.PolicyTemplates[i]
for k, i := range p.PolicyTemplates[i].Icons {
t.Icons[k].Path = i.getPath(p)
}
// Store paths for all screenshots under each policy template
if p.PolicyTemplates[i].Screenshots != nil {
for k, s := range p.PolicyTemplates[i].Screenshots {
p.PolicyTemplates[i].Screenshots[k].Path = s.getPath(p)
}
}
// Store policy template specific README
readmePath := path.Join("docs", p.PolicyTemplates[i].Name+".md")
readme, err := fs.Stat(readmePath)
if err != nil {
if _, ok := err.(*os.PathError); !ok {
return nil, fmt.Errorf("failed to find %s file: %s", p.PolicyTemplates[i].Name+".md", err)
}
} else if readme != nil {
if readme.IsDir() {
return nil, fmt.Errorf("%s.md is a directory", p.PolicyTemplates[i].Name)
}
readmePathShort := path.Join(packagePathPrefix, p.Name, p.Version, "docs", p.PolicyTemplates[i].Name+".md")
p.PolicyTemplates[i].Readme = &readmePathShort
}
}
p.setBasePolicyTemplates()
if p.Type == "" {
p.Type = defaultType
}
// If not license is set, basic is assumed
if p.License == "" {
// Keep compatibility with deprecated license field.
if p.Conditions != nil && p.Conditions.Elastic != nil && p.Conditions.Elastic.Subscription != "" {
p.License = p.Conditions.Elastic.Subscription
} else {
p.License = DefaultLicense
}
}
err = p.setRuntimeFields()
if err != nil {
return nil, err
}
if p.Icons != nil {
for k, i := range p.Icons {
p.Icons[k].Path = i.getPath(p)
}
}
if p.Screenshots != nil {
for k, s := range p.Screenshots {
p.Screenshots[k].Path = s.getPath(p)
}
}
if p.Release == "" {
p.Release = releaseForSemVerCompat(p.versionSemVer)
}
if !IsValidRelease(p.Release) {
return nil, fmt.Errorf("invalid release: %q", p.Release)
}
readmePath := path.Join("docs", "README.md")
// Check if readme
readme, err := fs.Stat(readmePath)
if err != nil {
return nil, fmt.Errorf("no readme file found, README.md is required: %s", err)
}
if readme != nil {
if readme.IsDir() {
return nil, fmt.Errorf("README.md is a directory")
}
readmePathShort := path.Join(packagePathPrefix, p.Name, p.Version, "docs", "README.md")
p.Readme = &readmePathShort
}
// Assign download path to be part of the output
p.Download = p.GetDownloadPath()
p.Path = p.GetUrlPath()
err = p.LoadAssets()
if err != nil {
return nil, fmt.Errorf("loading package assets failed (path '%s'): %w", p.BasePath, err)
}
err = p.LoadDataSets()
if err != nil {
return nil, fmt.Errorf("loading package data streams failed (path '%s'): %w", p.BasePath, err)
}
// Read path for package signature
p.SignaturePath, err = p.getSignaturePath()
if err != nil {
return nil, fmt.Errorf("can't process the package signature: %w", err)
}
return p, nil
}