Skip to content

Commit 2d77566

Browse files
Refactor DocsDir to use git root directory (#47614) (#47845)
This fixes packaging errors in cloudbeat: elastic/cloudbeat#3567 Change DocsDir() signature to return (string, error) instead of panicking (cherry picked from commit e65fa6d) Co-authored-by: Orestis Floros <[email protected]>
1 parent efc8564 commit 2d77566

File tree

4 files changed

+33
-12
lines changed

4 files changed

+33
-12
lines changed

dev-tools/mage/common.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -899,13 +899,12 @@ func IsUpToDate(dst string, sources ...string) bool {
899899
return err == nil && !execute
900900
}
901901

902-
func DocsDir() string {
903-
cwd := CWD()
904-
// Check if we need to correct ossDir because it's in x-pack.
905-
if parentDir := filepath.Base(filepath.Dir(cwd)); parentDir == "x-pack" {
906-
return filepath.Join(cwd, "../..", "docs")
902+
func DocsDir() (string, error) {
903+
repoInfo, err := GetProjectRepoInfo()
904+
if err != nil {
905+
return "", fmt.Errorf("failed to get project repo info: %w", err)
907906
}
908-
return filepath.Join(cwd, "..", "docs")
907+
return filepath.Join(repoInfo.RootDir, "docs"), nil
909908
}
910909

911910
// OSSBeatDir returns the OSS beat directory. You can pass paths and they will

dev-tools/mage/docs.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,12 @@ func (docsBuilder) FieldDocs(fieldsYML string) error {
8484
return err
8585
}
8686

87-
outputPath := filepath.Join(DocsDir(), "reference", BeatName)
87+
docsDir, err := DocsDir()
88+
if err != nil {
89+
return err
90+
}
91+
92+
outputPath := filepath.Join(docsDir, "reference", BeatName)
8893

8994
// TODO: Port this script to Go.
9095
log.Println(">> Generating exported-fields.md for", BeatName)

metricbeat/scripts/mage/docs_collector.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -435,8 +435,12 @@ func gatherData(modules []string) ([]moduleData, error) {
435435

436436
// writeModuleDocs writes the module-level docs
437437
func writeModuleDocs(modules []moduleData, t *template.Template) error {
438+
docsDir, err := mage.DocsDir()
439+
if err != nil {
440+
return err
441+
}
438442
for _, mod := range modules {
439-
filename := filepath.Join(mage.DocsDir(), "reference", "metricbeat", fmt.Sprintf("metricbeat-module-%s.md", mod.Base))
443+
filename := filepath.Join(docsDir, "reference", "metricbeat", fmt.Sprintf("metricbeat-module-%s.md", mod.Base))
440444
err := writeTemplate(filename, t.Lookup("moduleDoc.tmpl"), mod)
441445
if err != nil {
442446
return err
@@ -447,6 +451,10 @@ func writeModuleDocs(modules []moduleData, t *template.Template) error {
447451

448452
// writeMetricsetDocs writes the metricset-level docs
449453
func writeMetricsetDocs(modules []moduleData, t *template.Template) error {
454+
docsDir, err := mage.DocsDir()
455+
if err != nil {
456+
return err
457+
}
450458
for _, mod := range modules {
451459
for _, metricset := range mod.Metricsets {
452460
modData := struct {
@@ -456,7 +464,7 @@ func writeMetricsetDocs(modules []moduleData, t *template.Template) error {
456464
mod,
457465
metricset,
458466
}
459-
filename := filepath.Join(mage.DocsDir(), "reference", "metricbeat", fmt.Sprintf("metricbeat-metricset-%s-%s.md", mod.Base, metricset.Title))
467+
filename := filepath.Join(docsDir, "reference", "metricbeat", fmt.Sprintf("metricbeat-metricset-%s-%s.md", mod.Base, metricset.Title))
460468

461469
err := writeTemplate(filename, t.Lookup("metricsetDoc.tmpl"), modData)
462470
if err != nil {
@@ -469,14 +477,18 @@ func writeMetricsetDocs(modules []moduleData, t *template.Template) error {
469477

470478
// writeModuleList writes the module linked list
471479
func writeModuleList(modules []moduleData, t *template.Template) error {
480+
docsDir, err := mage.DocsDir()
481+
if err != nil {
482+
return err
483+
}
472484
// Turn the map into a sorted list
473485
//Normally the glob functions would do this sorting for us,
474486
//but because we mix the regular and x-pack dirs we have to sort them again.
475487
sort.Slice(modules, func(i, j int) bool {
476488
return modules[i].Base < modules[j].Base
477489
})
478490
//write and execute the template
479-
filepath := filepath.Join(mage.DocsDir(), "reference", "metricbeat", "metricbeat-modules.md")
491+
filepath := filepath.Join(docsDir, "reference", "metricbeat", "metricbeat-modules.md")
480492
return writeTemplate(filepath, t.Lookup("moduleList.tmpl"), modules)
481493

482494
}

winlogbeat/scripts/mage/docs.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ func moduleDocs() error {
4747
return fmt.Errorf("No modules found matching %v", searchPath)
4848
}
4949

50+
docsDir, err := mage.DocsDir()
51+
if err != nil {
52+
return err
53+
}
54+
5055
// Extract module name from path and copy the file.
5156
var names []string
5257
for _, f := range files {
@@ -59,12 +64,12 @@ func moduleDocs() error {
5964
modulesListTmpl += fmt.Sprintf("* [%s](/reference/winlogbeat/winlogbeat-module-%s.md)\n", strings.Title(name), name)
6065

6166
// Copy to the docs dirs.
62-
dest := filepath.Join(mage.DocsDir(), "reference", "winlogbeat", fmt.Sprintf("winlogbeat-module-%s.md", name))
67+
dest := filepath.Join(docsDir, "reference", "winlogbeat", fmt.Sprintf("winlogbeat-module-%s.md", name))
6368
if err = mage.Copy(f, mage.CreateDir(dest)); err != nil {
6469
return err
6570
}
6671
}
6772

6873
fmt.Printf(">> update:moduleDocs: Collecting module documentation for %v.\n", strings.Join(names, ", "))
69-
return os.WriteFile(filepath.Join(mage.DocsDir(), "reference", "winlogbeat", "winlogbeat-modules.md"), []byte(modulesListTmpl), 0o644)
74+
return os.WriteFile(filepath.Join(docsDir, "reference", "winlogbeat", "winlogbeat-modules.md"), []byte(modulesListTmpl), 0o644)
7075
}

0 commit comments

Comments
 (0)