Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 5 additions & 6 deletions dev-tools/mage/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -899,13 +899,12 @@ func IsUpToDate(dst string, sources ...string) bool {
return err == nil && !execute
}

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

// OSSBeatDir returns the OSS beat directory. You can pass paths and they will
Expand Down
7 changes: 6 additions & 1 deletion dev-tools/mage/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,12 @@
return err
}

outputPath := filepath.Join(DocsDir(), "reference", BeatName)
docsDir, err := DocsDir()
if err != nil {
return err
}

outputPath := filepath.Join(docsDir, "reference", BeatName)

// TODO: Port this script to Go.
log.Println(">> Generating exported-fields.md for", BeatName)
Expand Down Expand Up @@ -176,7 +181,7 @@
}

func (docsBuilder) servePreview(dir string) *http.Server {
server := &http.Server{

Check failure on line 184 in dev-tools/mage/docs.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

G112: Potential Slowloris Attack because ReadHeaderTimeout is not configured in the http.Server (gosec)
Addr: net.JoinHostPort("localhost", EnvOr("PREVIEW_PORT", "8000")),
Handler: http.FileServer(http.Dir(dir)),
}
Expand Down
18 changes: 15 additions & 3 deletions metricbeat/scripts/mage/docs_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import (
"encoding/json"
"fmt"
"io/ioutil"

Check failure on line 23 in metricbeat/scripts/mage/docs_collector.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

SA1019: "io/ioutil" has been deprecated since Go 1.19: As of Go 1.16, the same functionality is now provided by package [io] or package [os], and those implementations should be preferred in new code. See the specific function documentation for details. (staticcheck)
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -102,7 +102,7 @@
"getBeatName": func() string {
return mage.BeatName
},
"title": strings.Title,

Check failure on line 105 in metricbeat/scripts/mage/docs_collector.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

SA1019: strings.Title has been deprecated since Go 1.18 and an alternative has been available since Go 1.0: The rule Title uses for word boundaries does not handle Unicode punctuation properly. Use golang.org/x/text/cases instead. (staticcheck)
}

// checkXpack checks to see if the module belongs to x-pack.
Expand All @@ -127,7 +127,7 @@
case "ga", "beta", "experimental":
return rel, nil
case "":
return "", fmt.Errorf("Missing a release string")

Check failure on line 130 in metricbeat/scripts/mage/docs_collector.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

ST1005: error strings should not be capitalized (staticcheck)
default:
return "ga", fmt.Errorf("unknown release tag %s", rel)
}
Expand All @@ -141,7 +141,7 @@
// testIfDocsInDir tests for a `_meta/docs.md` in a given directory
func testIfDocsInDir(moduleDir string) bool {
_, err := os.Stat(filepath.Join(moduleDir, "_meta/docs.md"))
if err != nil {

Check failure on line 144 in metricbeat/scripts/mage/docs_collector.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

S1008: should use 'return err == nil' instead of 'if err != nil { return false }; return true' (staticcheck)
return false
}
return true
Expand All @@ -167,7 +167,7 @@
for _, dir := range runpaths {
rawMap, err := sh.OutCmd("go", append(cmd, dir)...)()
if err != nil {
return nil, fmt.Errorf("Error running subcommand to get metricsets: %w", err)

Check failure on line 170 in metricbeat/scripts/mage/docs_collector.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

ST1005: error strings should not be capitalized (staticcheck)
}
var msetMap = make(map[string][]string)
err = json.Unmarshal([]byte(rawMap), &msetMap)
Expand Down Expand Up @@ -199,7 +199,7 @@
if err != nil {
return mod[0], fmt.Errorf("file %s is missing a release string: %w", file, err)
}
applies_to, err := getVersion(fd)

Check failure on line 202 in metricbeat/scripts/mage/docs_collector.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

ineffectual assignment to err (ineffassign)

module.Release = rel
module.Applies_to = applies_to
Expand Down Expand Up @@ -240,7 +240,7 @@
Path string
}
var rel []metricset
yaml.Unmarshal(raw, &rel)

Check failure on line 243 in metricbeat/scripts/mage/docs_collector.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

Error return value of `yaml.Unmarshal` is not checked (errcheck)
// Build the applies_to string: a comma-separated list
// of all available lifecycles and versions
// NOTE: There's almost certainly a more efficient way
Expand Down Expand Up @@ -325,7 +325,7 @@
return nil, err
}
metricsetName := filepath.Base(metricset)
release, err := getReleaseState(filepath.Join(metricset, "_meta/fields.yml"))

Check failure on line 328 in metricbeat/scripts/mage/docs_collector.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

ineffectual assignment to err (ineffassign)
raw, err := os.ReadFile(filepath.Join(metricset, "_meta/fields.yml"))
if err != nil {
return nil, err
Expand Down Expand Up @@ -435,8 +435,12 @@

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

// writeMetricsetDocs writes the metricset-level docs
func writeMetricsetDocs(modules []moduleData, t *template.Template) error {
docsDir, err := mage.DocsDir()
if err != nil {
return err
}
for _, mod := range modules {
for _, metricset := range mod.Metricsets {
modData := struct {
Expand All @@ -456,7 +464,7 @@
mod,
metricset,
}
filename := filepath.Join(mage.DocsDir(), "reference", "metricbeat", fmt.Sprintf("metricbeat-metricset-%s-%s.md", mod.Base, metricset.Title))
filename := filepath.Join(docsDir, "reference", "metricbeat", fmt.Sprintf("metricbeat-metricset-%s-%s.md", mod.Base, metricset.Title))

err := writeTemplate(filename, t.Lookup("metricsetDoc.tmpl"), modData)
if err != nil {
Expand All @@ -469,14 +477,18 @@

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

}
Expand Down
9 changes: 7 additions & 2 deletions winlogbeat/scripts/mage/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@
return fmt.Errorf("No modules found matching %v", searchPath)
}

docsDir, err := mage.DocsDir()
if err != nil {
return err
}

// Extract module name from path and copy the file.
var names []string
for _, f := range files {
Expand All @@ -59,12 +64,12 @@
modulesListTmpl += fmt.Sprintf("* [%s](/reference/winlogbeat/winlogbeat-module-%s.md)\n", strings.Title(name), name)

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

fmt.Printf(">> update:moduleDocs: Collecting module documentation for %v.\n", strings.Join(names, ", "))

Check failure on line 73 in winlogbeat/scripts/mage/docs.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

use of `fmt.Printf` forbidden by pattern `fmt.Print.*` (forbidigo)
return os.WriteFile(filepath.Join(mage.DocsDir(), "reference", "winlogbeat", "winlogbeat-modules.md"), []byte(modulesListTmpl), 0o644)
return os.WriteFile(filepath.Join(docsDir, "reference", "winlogbeat", "winlogbeat-modules.md"), []byte(modulesListTmpl), 0o644)
}
Loading